poj2186 强连通缩点

时间:2021-09-12 19:10:02
Popular Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 29773   Accepted: 12080

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

题意:

有n头牛,每头牛之间有仰慕关系。有m个关系对(A,B)表示A仰慕B,并且具有传递性。 问被所有除了本身以外的牛仰慕的牛有几个。

思路:

由于强连通中的牛都是相互仰慕的,所以可以先缩点。然后判断出度为0的个数,如果出度为0,说明这个牛肯定是被人仰慕的。如果出度为0

的点个数大于1,说明是不可能的。因为着几个点之间不会有关系。 如果出度为1的点个数只有1个,说明存在这样的牛,并且就是这个缩点后该带点表示的个数。

/*
* Author: sweat123
* Created Time: 2016/6/25 13:45:46
* File Name: main.cpp
*/
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<time.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1<<30
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define pi acos(-1.0)
using namespace std;
const int MAXN = ;
struct node{
int from;
int to;
int next;
}edge[MAXN*];
int pre[MAXN],vis[MAXN],dfn[MAXN],low[MAXN],n,m,ind;
int f[MAXN],siz[MAXN],num,dep,out[MAXN];
stack<int>s;
void add(int x,int y){
edge[ind].from = x;
edge[ind].to = y;
edge[ind].next = pre[x];
pre[x] = ind ++;
}
void dfs(int rt){
dfn[rt] = low[rt] = ++dep;
vis[rt] = ;
s.push(rt);
for(int i = pre[rt]; i != -; i = edge[i].next){
int t = edge[i].to;
if(!dfn[t]){
dfs(t);
low[rt] = min(low[rt],low[t]);
} else if(vis[t]){
low[rt] = min(low[rt],dfn[t]);
}
}
if(low[rt] == dfn[rt]){
++num;
while(!s.empty()){
int tp = s.top();
s.pop();
vis[tp] = ;
f[tp] = num;
siz[num] ++;
if(tp == rt)break;
}
}
}
void setcc(){
num = ;
dep = ;
memset(out,,sizeof(out));
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
for(int i = ; i <= n; i++){
if(!dfn[i]){
dfs(i);
}
}
memset(pre,-,sizeof(pre));
int ret = ind;
for(int i = ; i < ret; i++){
int x = f[edge[i].from];
int y = f[edge[i].to];
if(x == y)continue;
add(x,y);
out[x] ++;
}
int ans = ;
int flag = ;
for(int i = ; i <= num; i++){
if(out[i] == ){
if(flag == ){flag = ;break;}
flag = ;
ans += siz[i];
}
}
if(flag == ){
ans = ;
}
printf("%d\n",ans);
}
int main(){
while(~scanf("%d%d",&n,&m)){
ind = ;
memset(pre,-,sizeof(pre));
while(!s.empty())s.pop();
memset(f,-,sizeof(f));
memset(siz,,sizeof(siz));
for(int i = ; i <= m; i++){
int x,y;
scanf("%d%d",&x,&y);
add(x,y);
}
setcc();
}
return ;
}