大家 一定要搞清楚trajan 与 tarjan的区别 233
这道题直接"trajan" 求出出度为0的点
就可以了
#include <map>
#include <stack>
#include <cstdio>
#include <cstring>
using namespace std;
int p,be[10001],out[10001];
int n,m,u,v,t,ans;
struct edge {
int v,next,u;
}e[50001];
int head[10001];
int cnt;
int dfn[10001],low[10001],idx,ins[10001];
stack<int>s;
int min (int a,int b) {
if(a > b)
return b;
return a;
}
void adde (int u,int v) {
e[++cnt].v = v;
e[cnt].u = u;
e[cnt].next = head[u];
head[u] = cnt;
}
void readdata () {
memset(head,-1,sizeof head);
scanf("%d%d",&n,&m);
for(int i = 1;i <= m;i++) {
scanf("%d%d",&u,&v);
adde(u,v);
}
}
void tarjan (int u) {
dfn[u] = low[u] = ++idx;
for(int i = head[u];i != -1;i = e[i].next) {
int v = e[i].v;
if(dfn[v] && ins[v]) {
low[u] = min(dfn[v],low[u]);
}
else {
if(!dfn[v]) {
ins[v] = 1;
s.push(v);
tarjan(v);
low[u] = min(low[u],low[v]);
}
}
}
if(dfn[u] == low[u]) {
p++;
while(!s.empty()) {
t = s.top();
s.pop();
ins[t] = 0;
be[t] = p;
if(t == u)
break;
}
}
}
int main () {
readdata();
for(int i = 1;i <= n;i++) {
if(!dfn[i]) {
ins[i] = 1;
s.push(i);
tarjan(i);
}
}
for(int i = 1;i <= cnt;i++) {
if(be[e[i].u] != be[e[i].v]) {
out[be[e[i].u]]++;
}
}
int f;
for(int i = 1;i <= p;i++)
if(!out[i])
f = i;
for(int i = 1;i <= n;i++)
if(be[i] == f)
ans ++;
printf("%d\n",ans);
}