# poj2186Popular Cows【scc+缩点】

Language:
Popular Cows
 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 27987 Accepted: 11275

Description

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.

Source

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stack>
#include<vector>
#define inf 0x3f3f3f3f3f
using namespace std;
const int maxn=100010;
int dfs_clock,scc_cnt;
bool instack[maxn];
int low[maxn],dfn[maxn];
int sccno[maxn];
int in[maxn],out[maxn];
stack<int>S;
vector<int>G[maxn];
vector<int>scc[maxn];
struct Node{
int from,to,next;
}A[maxn];
void init(){
dfs_clock=scc_cnt=0;
memset(low,0,sizeof(low));
memset(dfn,0,sizeof(dfn));
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
memset(sccno,0,sizeof(sccno));
memset(instack,false,sizeof(instack));
}
int MIN(int a,int b){
return a<b?a:b;
}
void tarjan(int u,int pre){
low[u]=dfn[u]=++dfs_clock;
S.push(u);instack[u]=true;
int v;
v=A[k].to;
if(!dfn[v]){
tarjan(v,u);
low[u]=MIN(low[u],low[v]);
}
else if(instack[v]){
low[u]=MIN(low[u],dfn[v]);
}
}
if(low[u]==dfn[u]){
scc_cnt++;
G[scc_cnt].clear();
scc[scc_cnt].clear();
while(1){
v=S.top();S.pop();
instack[v]=false;
sccno[v]=scc_cnt;
scc[scc_cnt].push_back(v);
if(u==v)break;
}
}
}
void suodian(int m){
for(int i=0;i<m;++i){
int u=sccno[A[i].from];
int v=sccno[A[i].to];
if(u!=v){
G[u].push_back(v);
in[v]++;out[u]++;
}
}
}
int main()
{
int n,m,i,j,k;
while(scanf("%d%d",&n,&m)!=EOF){
init();
for(i=0;i<m;++i){
scanf("%d%d",&A[i].from,&A[i].to);
}
for(i=1;i<=n;++i){
if(!dfn[i])tarjan(i,-1);
}
suodian(m);
int ans=0,pos;
for(i=1;i<=scc_cnt;++i){
if(!out[i]){
ans++;
pos=i;
}
}
if(scc_cnt==1){
printf("%d\n",n);
}
else if(ans>1){
printf("%d\n",0);
}
else printf("%d\n",scc[pos].size());
}
return 0;
}

• 本文已收录于以下专栏：

## poj2186Popular Cows(强连通+缩点)

Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19647   ...
• ophunter
• 2013年07月20日 10:31
• 822

## poj--2186--Popular Cows (scc+缩点）

Popular Cows Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) To...
• qq_29963431
• 2015年10月23日 21:27
• 176

## POJ2186Popular Cows(强连通分量+缩点)

Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24614   Accepted: 10...
• u014028317
• 2015年04月02日 22:55
• 379

## 【连通图|强连通+缩点】POJ-2186 Popular Cows

N个奶牛有M对仰慕关系，给出u、v表示u仰慕v，问有几头奶牛受到其余所有奶牛的仰慕。（仰慕关系具有传递性）...
• u012325552
• 2014年12月27日 15:43
• 700

## poj2186Popular Cows

#include #include #include #include #include #include #include #include #include #include ...
• coder_coder_coder
• 2014年08月21日 22:51
• 252

## POJ2186Popular Cows

• wangwenhao00
• 2013年12月01日 22:55
• 829

## hdoj--2767--Proving Equivalences （scc+缩点）

Proving Equivalences Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Oth...
• qq_29963431
• 2015年10月23日 20:38
• 179

## hdoj-1827-Summer Holiday(scc+缩点)

Summer Holiday Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)...
• qq_29963431
• 2015年10月23日 17:49
• 178

## hdoj2767Proving Equivalences【scc+缩点】

Proving Equivalences Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot...
• R1986799047
• 2015年11月20日 21:21
• 293

## POJ2186Popular Cows(强连通分量)

• Rain722
• 2017年04月03日 14:40
• 178

举报原因： 您举报文章：poj2186Popular Cows【scc+缩点】 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)