Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
人生第一次TARJAN,发帖,纪念哈!#include<stdio.h> #include<iostream> #include<vector> using namespace std; int Ctc,n,m,id,st[10001],ctd[10001],ict[10001],ist[10001],top,DFN[10001],LOW[10001]; vector<int> Gr[10001]; vector<int> Ct[10001]; void tarjan(int i) {int j,k; ist[i]=1; st[++top]=i; DFN[i]=LOW[i]=id++; for(k=0;k<Gr[i].size();k++) {j=Gr[i][k]; if(DFN[j]==-1) {tarjan(j); LOW[i]=min(LOW[i],LOW[j]); } else if(ist[j]) LOW[i]=min(LOW[i],DFN[j]); } if(LOW[i]==DFN[i]) {Ctc++; do {j=st[top--]; ist[j]=0; ict[j]=Ctc; Ct[Ctc].push_back(j); }while(j!=i); } } int main() {freopen("poj2186.in","r",stdin); freopen("poj2186.out","w",stdout); cin>>n>>m; int i,j,x,y,k,loc,t=0; for(i=1;i<=m;i++) {scanf("%d%d",&x,&y); Gr[x].push_back(y); } memset(DFN,-1,sizeof(DFN)); memset(LOW,-1,sizeof(LOW)); for(i=1;i<=n;i++) if(DFN[i]==-1) tarjan(i); for(i=1;i<=n;i++) {for(k=0;k<Gr[i].size();k++) {j=Gr[i][k]; if(ict[i]!=ict[j]) ctd[ict[i]]++; } } for(i=1;i<=Ctc;i++) if(ctd[i]==0){t++;loc=i;} if(t==1) printf("%d",Ct[loc].size()); else cout<<0; return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator