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

时间:2023-03-08 23:15:34
poj 2186 Popular Cows (强连通分量+缩点)

http://poj.org/problem?id=2186

Popular Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 20191   Accepted: 8193

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. 
【题解】:
    
有N(N<=10000)头牛,每头牛都想成为most poluler的牛,给出M(M<=50000)个关系,如(1,2)代表1欢迎2,关系可以传递,但是不可以相互,即1欢迎2不代表2欢迎1,但是如果2也欢迎3那么1也欢迎3.
给出N,M和M个欢迎关系,求被所有牛都欢迎的牛的数量。
用强联通分量做,求连通分量我用的是tarjan算法。
首先求出联通分量的个数,然后依次求各个联通分量的出度,如果仅有一个连通分量出度为0则这个联通分量内的点的个数就是答案;如果有多于一个的联通分量的出度为0,则说明此有向图肯定不连通。因此直接输出0。
缩点的意思就是说把求得的强连通分量的点集看成一个点
【code】:
 /**
Judge Status:Accepted Memory:2404K
Time:532MS Language:G++
Code Length:1971B Author:cj
*/
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stack>
#include<vector>
#include<algorithm> #define N 10010
using namespace std; vector<int> G[N];
stack<int> stk;
int pre[N],lowlink[N],sccno[N],scc_cnt,dfn_clock,out[N],counter[N]; void DFN(int u) //tarjan算法
{
lowlink[u] = pre[u] = ++dfn_clock;
stk.push(u);
int i;
for(i=;i<G[u].size();i++)
{
int v = G[u][i];
if(!pre[v])
{
DFN(v);
lowlink[u] = min(lowlink[u],lowlink[v]);
}
else if(!sccno[v])
{
lowlink[u] = min(lowlink[u],pre[v]);
}
}
if(lowlink[u]==pre[u])
{
scc_cnt++; //强连通图的个数标记
while()
{
int x = stk.top();
stk.pop();
sccno[x] = scc_cnt;
if(x==u) break;
}
}
} void findscc(int n)
{
int i;
scc_cnt = dfn_clock = ;
memset(pre,,sizeof(pre));
memset(lowlink,,sizeof(lowlink));
memset(sccno,,sizeof(sccno));
for(i=;i<=n;i++)
if(!pre[i])
DFN(i);
} int main()
{
int n,m;
scanf("%d%d",&n,&m);
int i;
for(i=;i<m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
G[a].push_back(b); // 得到图
}
findscc(n); //查找强连通图
int j;
memset(out,,sizeof(out));
memset(counter,,sizeof(counter)); for(i=;i<=n;i++) //遍历一边图,查找统计个点缩点后的出度
{
// cout<<sccno[i]<<" ";
for(j=;j<G[i].size();j++)
{
int v = G[i][j];
if(sccno[i]!=sccno[v])
{
out[sccno[i]]++; //出度
}
}
} for(i=;i<=n;i++)
{
counter[sccno[i]]++; //统计各个强连通分量中的节点个数
} int cnt =,ans = ;
for(i=;i<=scc_cnt;i++)
{
if(!out[i]) //出度为0的强连通分量
{
cnt++;
ans = counter[i]; //答案即为其中的点集数
}
} if(cnt==) printf("%d\n",ans);
else puts(""); return ;
}