poj2186Popular Cows(强连通分量)

时间:2023-03-08 16:46:12
poj2186Popular Cows(强连通分量)

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

用tarjan算出强连通分量的个数 将其缩点 连成一棵树  则题目所求即变成求出度为0 的那个节点 在树中是唯一的 即树根

 #include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<stack>
using namespace std;
#define M 50010
#define N 10010
struct node
{
int u,v,next;
}edge[M*];
int head[N],lowlink[N],pre[N],t,sccno[N],scc,dep,dout[N];
stack<int>s;
void init()
{
t = ;
memset(head,-,sizeof(head));
memset(lowlink,,sizeof(lowlink));
memset(pre,,sizeof(pre));
memset(sccno,,sizeof(sccno));
dep=;scc=;
}
void add(int u,int v)
{
edge[t].u = v;
edge[t].v = v;
edge[t].next = head[u];
head[u] = t++;
}
void dfs(int u)
{
lowlink[u] = pre[u] = ++dep;
s.push(u);
for(int i = head[u] ; i != - ; i = edge[i].next)
{
int v = edge[i].v;
if(!pre[v])
{
dfs(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++;
for(;;)
{
int x = s.top();s.pop();
sccno[x] = scc;
if(x==u) break;
}
}
}
void find_scc(int n)
{
for(int i = ; i <= n ; i++)
if(!pre[i]) dfs(i);
}
int main()
{
int i,j,n,m,a,b;
while(cin>>n>>m)
{
init();
while(m--)
{
cin>>a>>b;
add(a,b);
}
find_scc(n);
for(i = ; i <= scc ; i++)
dout[i] = ;
for(i = ; i <= n ; i++)
for(j = head[i] ; j!=- ; j = edge[j].next)
{
int v = edge[j].v;
if(sccno[i]!=sccno[v])
dout[sccno[i]] = ;
}
int num=,o,w=;
for(i = ; i <= scc ; i++)
if(dout[i]==)
{
o = i;
num++;
}
if(num==)
cout<<n<<endl;
else if(num==)
{
for(i = ; i <= n ; i++)
if(sccno[i]==o)
{
w++;
}
cout<<w<<endl;
}
else
cout<<"0\n";
}
return ;
}