【无聊放个模板系列】HDU 1269 (SCC)
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<iostream> 5 #include<algorithm> 6 #include<queue> 7 #include<cmath> 8 #include<stack> 9 using namespace std; 10 #define Maxn 10010 11 #define Maxm 100010 12 13 struct node 14 { 15 int x,y,next; 16 }t[Maxm];int len; 17 int first[Maxn]; 18 19 void ins(int x,int y) 20 { 21 t[++len].x=x;t[len].y=y; 22 t[len].next=first[x];first[x]=len; 23 } 24 25 int mymin(int x,int y) {return x<y?x:y;} 26 27 int dfn[Maxn],low[Maxn],cnt,scc; 28 stack<int > s; 29 bool inst[Maxn]; 30 31 void dfs(int x,int f) 32 { 33 dfn[x]=low[x]=++cnt; 34 inst[x]=1;s.push(x); 35 for(int i=first[x];i;i=t[i].next) if(t[i].y!=f) 36 { 37 int y=t[i].y; 38 if(dfn[y]==0) 39 { 40 dfs(y,x); 41 low[x]=mymin(low[x],low[y]); 42 } 43 else if(inst[y]) low[x]=mymin(low[x],dfn[y]); 44 } 45 if(low[x]==dfn[x]) 46 { 47 int z;scc++; 48 while(1) 49 { 50 z=s.top();s.pop(); 51 inst[z]=0; 52 if(z==x) break; 53 } 54 } 55 } 56 57 int main() 58 { 59 while(1) 60 { 61 int n,m; 62 scanf("%d%d",&n,&m); 63 if(n==0&&m==0) break; 64 len=0; 65 memset(first,0,sizeof(first)); 66 for(int i=1;i<=m;i++) 67 { 68 int x,y; 69 scanf("%d%d",&x,&y); 70 ins(x,y); 71 } 72 cnt=0;scc=0; 73 memset(dfn,0,sizeof(dfn)); 74 memset(inst,0,sizeof(inst)); 75 for(int i=1;i<=n;i++) if(dfn[i]==0) dfs(i,0); 76 if(scc==1) printf("Yes\n"); 77 else printf("No\n"); 78 } 79 return 0; 80 }
SCC
2016-11-17 20:56:27