Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <= M <= 50,000) ordered pairs of the form (A, B) that tell you that cow A thinks that cow B is popular. Since popularity is transitive, if A thinks B is popular and B thinks C is popular, then A will also think that C is 
popular, even if this is not explicitly specified by an ordered pair in the input. Your task is to compute the number of cows that are considered popular by every other cow. 

Input

* Line 1: Two space-separated integers, N and M

* Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular.

Output

* Line 1: A single integer that is the number of cows who are considered popular by every other cow. 

Sample Input

3 3
1 2
2 1
2 3

Sample Output

1

Hint

Cow 3 is the only cow of high popularity. 
 
题解:给你M组u,v代表v是受u欢迎的,并且欢迎具有传递性;让你求最多有多少人是相互受欢迎的;
SCC + 缩点;强连通分量跑一边,然后缩点,就可转化为DAG图;然后记录每个 “ 点 ” 的出度;如果有超过一个,那么输出0即可;
如果为一个,则输出其所在点集的大小;
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long LL;
const int INF=0x3f3f3f3f;
#define eps 1e-8
typedef pair<int,int> P;
const int maxn=5e4+;
int N,M,u[maxn],v[maxn],tot,times,blocks;
int head[maxn],dfn[maxn],lowv[maxn];
int ins[maxn],outd[maxn],belong[maxn],sz[maxn];
struct Node{
int v,nxt;
} edge[maxn];
stack<int> st;
void Init()
{
memset(head,-,sizeof head);
memset(dfn,,sizeof dfn);
memset(lowv,,sizeof lowv);
memset(ins,,sizeof ins);
memset(outd,,sizeof outd);
memset(sz,,sizeof sz);
memset(belong,,sizeof belong);
tot=times=blocks=;
while(!st.empty()) st.pop();
} void Addedge(int u,int v)
{
edge[tot].v=v;
edge[tot].nxt=head[u];
head[u]=tot++;
} void Tarjan(int u)
{
dfn[u]=lowv[u]=++times;
st.push(u);
ins[u]=;
for(int i=head[u];~i;i=edge[i].nxt)
{
int v=edge[i].v;
if(!dfn[v]) Tarjan(v),lowv[u]=min(lowv[u],lowv[v]);
else if(ins[v]) lowv[u]=min(lowv[u],dfn[v]);
} if(dfn[u]==lowv[u])
{
++blocks;
int v;
do
{
v=st.top(); st.pop();
belong[v]=blocks;
sz[blocks]++;
ins[v]=;
} while(u!=v);
} } int main()
{
ios::sync_with_stdio(false);
cin>>N>>M;
Init();
for(int i=;i<=M;i++)
{
cin>>u[i]>>v[i];
Addedge(u[i],v[i]);
}
int cnt=,flag;
for(int i=;i<=N;i++) if(!dfn[i]) Tarjan(i);
for(int i=;i<=M;i++) if(belong[u[i]]!=belong[v[i]]) outd[belong[u[i]]]++;
for(int i=;i<=blocks;i++) if(outd[i]==) cnt++,flag=i; if(cnt!=) cout<<<<endl;
else cout<<sz[flag]<<endl;
return ;
}

POJ 2186 Popular cows(SCC 缩点)的更多相关文章

  1. POJ 2186 Popular Cows tarjan缩点算法

    题意:给出一个有向图代表牛和牛喜欢的关系,且喜欢关系具有传递性,求出能被所有牛喜欢的牛的总数(除了它自己以外的牛,或者它很自恋). 思路:这个的难处在于这是一个有环的图,对此我们可以使用tarjan算 ...

  2. 强连通分量分解 Kosaraju算法 (poj 2186 Popular Cows)

    poj 2186 Popular Cows 题意: 有N头牛, 给出M对关系, 如(1,2)代表1欢迎2, 关系是单向的且能够传递, 即1欢迎2不代表2欢迎1, 可是假设2也欢迎3那么1也欢迎3. 求 ...

  3. poj 2186 Popular Cows (强连通分量+缩点)

    http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissi ...

  4. tarjan缩点练习 洛谷P3387 【模板】缩点+poj 2186 Popular Cows

    缩点练习 洛谷 P3387 [模板]缩点 缩点 解题思路: 都说是模板了...先缩点把有环图转换成DAG 然后拓扑排序即可 #include <bits/stdc++.h> using n ...

  5. POJ 2186 Popular Cows (强联通)

    id=2186">http://poj.org/problem? id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 655 ...

  6. poj 2186 Popular Cows 【强连通分量Tarjan算法 + 树问题】

    题目地址:http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Sub ...

  7. poj 2186 Popular Cows【tarjan求scc个数&&缩点】【求一个图中可以到达其余所有任意点的点的个数】

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27698   Accepted: 11148 De ...

  8. POJ 2186 Popular Cows(Targin缩点)

    传送门 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31808   Accepted: 1292 ...

  9. poj 2186 Popular Cows

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 29908   Accepted: 12131 De ...

随机推荐

  1. 怎样用JS给,option添加“选中”属性

    <html> <head> <script> window.onload = function(){ var opts = document.getElementB ...

  2. nyoj 44-子串和(子串和最大问题)

    44-子串和 内存限制:64MB 时间限制:5000ms Special Judge: No accepted:12 submit:48 题目描述: 给定一整型数列{a1,a2...,an},找出连续 ...

  3. nyoj 84-阶乘的0 (规律题)

    84-阶乘的0 内存限制:64MB 时间限制:3000ms 特判: No 通过数:7 提交数:9 难度:3 题目描述: 计算n!的十进制表示最后有多少个0 输入描述: 第一行输入一个整数N表示测试数据 ...

  4. 力扣(LeetCode)最后一个单词的长度 个人题解

    给定一个仅包含大小写字母和空格 ' ' 的字符串,返回其最后一个单词的长度. 如果不存在最后一个单词,请返回 0 . 说明:一个单词是指由字母组成,但不包含任何空格的字符串. 示例: 输入: &quo ...

  5. windwos 10 安装flask

    1 安装python2.7.13 安装文件为:python-2.7.13.amd64.msi,因为python2.7.13中已经包含了pip. 在安装过程中选中[Add python.exe to P ...

  6. Extjs导入Excel文件之后grid自动刷新显示刚插入的数据

    var winUpload = new Ext.Window({ title: '导入excel文件', width: 400, height:200, listeners: { close: fun ...

  7. <meta name="viewport" content="width=device-width,initial-scale=1.0">的意思

    content属性值 :      width:可视区域的宽度,值可为数字或关键词device-width      height同理width      intial-scale:页面首次被显示是可 ...

  8. alpha week 2/2 Scrum立会报告+燃尽图 01

    此作业要求参见https://edu.cnblogs.com/campus/nenu/2019fall/homework/9798 一.小组情况 队名:扛把子 组长:迟俊文 组员:宋晓丽 梁梦瑶 韩昊 ...

  9. 2019 牛客网 第七场 H pair

     题目链接:https://ac.nowcoder.com/acm/contest/887/H  题意: 给定A,B,C问在[1,A]和[1,B]中有多少对x,y满足x&y>C或者x^y ...

  10. 关于JMeter原子性相关探究

    1.背景 最近宝路遇到个项目,在使用JMeter过程中引发了一些思考,宝路尝试用各种方式去验证,进而有了今天"JMeter原子性"相关主题. 2.目的 探究JMeter的事务的原子 ...