Water problem, direct sticker code.
POJ 3180//sep9 #include <iostream> #include <stack> using namespace std;
const int maxn=10024;
const int maxm=50048;
int SUM[MAXN];
int HEAD[MAXN],DFN[MAXN],LOW[MAXN],INS[MAXN],BELONG[MAXN];
Stack<int> s; struct edge{int v,next;}
EDGE[MAXM];
int n,m,e,t,cnt;
void Tarjan (int u) {dfn[u]=low[u]=++t;
S.push (U);
Ins[u]=1;
for (int i=head[u];i!=-1;i=edge[i].next) {int v=edge[i].v;
if (dfn[v]==0) {Tarjan (v);
Low[u]=min (Low[u],low[v]);
}else if (ins[v]==1) low[u]=min (Low[u],dfn[v]);
} int k;
if (Low[u]==dfn[u]) {++cnt;
do{K=s.top ();
S.pop ();
ins[k]=0;
belong[k]=cnt;
}while (Low[k]!=dfn[k]);
} return;
} int main () {int n,m;
scanf ("%d%d", &n,&m);
e=0;
memset (head,-1,sizeof (head));
memset (dfn,0,sizeof (DFN));
memset (ins,0,sizeof (INS));
while (m--) {int A, B;
scanf ("%d%d", &a,&b);
edge[e].v=b;edge[e].next=head[a];head[a]=e++;
} t=cnt=0; for (int i=0;i<n;++i) if (!dfn[i]) Tarjan (i);
memset (sum,0,sizeof (sum));
for (int i=0;i<n;++i) ++sum[belong[i]];
int ans=0;
for (int i=1;i<=cnt;++i) if (sum[i]>1) ++ans;
printf ("%d", ans);
return 0; }