Tarjan the indent point. Judge if only one does not have an out edge. Yes, the point count is output
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include < stack>using namespace std; #define REP (i,n) for (int i=1;i<=n;i++) #define CLR (x,c) memset (x,c,sizeof (x)) #define REP (i,s,t) for (int i=s;i<=t;i++) #define OP () CLR (head,0);p t=edges, #define QWQ (x) for (Edge *o=head[x];o;o=o-> Next) int read () {int X=0;char c=getchar (); bool F=true;while (!isdigit (c)) {if (c== '-') F=false;c=getchar ();} while (IsDigit (c)) x=x*10+c-' 0 ', C=getchar (); return f?x:-x;} const int NMAX=10005;CONST int Maxn=50005;const int inf=0x7f7f7f7f;struct edge{int to;edge *next;}; Edge Edges[maxn],*pt,*head[nmax];int Pre[nmax],sccno[nmax],out[nmax],scc_cnt,dfs_clock;stack<int>s;void Adde (int u,int v) {pt->to=v;pt->next=head[u];head[u]=pt++;} int dfs (int x) {int lowu=pre[x]=++dfs_clock;s.push (x); Qwq (x) {int to=o->to;if (!pre[to]) lowu=min (Lowu,dfs (to)); else if (!sccno[to]) lowu=min (Lowu,pre[to]);} if (Lowu==pre[x]) {scc_cnt++;while (1) {int x=s.top (); S.pop (); sccno[x]=scc_Cnt;if (x==x) break;}} return LOWU;} void init () {op (); CLR (out,false); CLR (pre,0); CLR (sccno,0);d fs_clock=scc_cnt=0;} int main () {init (); int n=read (), M=read (), U,v,ans=0,cur,cnt=0;rep (i,m) U=read (), V=read (), Adde (U,V), Rep (i,n) if (!pre[ I]) Dfs (i); Rep (i,n) qwq (i) if (Sccno[i]!=sccno[o->to]) out[sccno[i]]=true;rep (i,scc_cnt) if (!out[i]) cnt++,cur=i; if (cnt==1) rep (i,n) if (sccno[i]==cur) ans++;p rintf ("%d\n", ans);}
1051: [HAOI2006] Popular cow time limit:10 Sec Memory limit:162 MB
submit:4144 solved:2219
[Submit] [Status] [Discuss] Description the desire of every cow is to become one of the most popular cows. Now there are n cows, give you m-pairs of integers (a, a, b), indicating that cow A is considered to be popular. This relationship is transitive, and if a thinks B is popular, B thinks C is popular, then bull a also thinks that Ox C is popular. Your task is to find out how many cows are considered popular by all cows. Input first row two number n,m. Next m line, two numbers per line, A, B, meaning that A is considered a welcome (the information given may be repeated, that is, there may be multiple, b) Output
A number, that is, how many cows are considered popular by all cows.
Sample Input3 3
1 2
2 1
2 3Sample Output1HINT
100% of Data n<=10000,m<=50000
Source [Submit] [Status] [Discuss]
bzoj1051: [HAOI2006] Popular Cattle