HDU 1269 迷宫城堡 scc
判断强联通scc是否为1
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int MAXN = 20010;//点数 4 const int MAXM = 200100;//边数 5 struct Edge { 6 int to,next; 7 }edge[MAXM]; 8 int head[MAXN],tot; 9 int Low[MAXN],DFN[MAXN],Stack[MAXN],Belong[MAXN];//Belong数组的值是1~scc 10 int Index,top; int scc;//强连通分量的个数 11 12 bool Instack[MAXN]; 13 int num[MAXN];//各个强连通分量包含点的个数,数组编号1~scc //num数组不一定需要,结合实际情况 14 15 void addedge(int u,int v){ 16 edge[tot].to = v; 17 edge[tot].next = head[u]; 18 head[u] = tot++; 19 } 20 void Tarjan(int u){ 21 int v; 22 Low[u] = DFN[u] = ++Index; 23 Stack[top++] = u; 24 Instack[u] = true; 25 for(int i = head[u];i != -1;i = edge[i].next) { 26 v = edge[i].to; 27 if( !DFN[v] ){ 28 Tarjan(v); 29 if( Low[u] > Low[v] ) 30 Low[u] = Low[v]; 31 } 32 else if(Instack[v] && Low[u] > DFN[v]) 33 Low[u] = DFN[v]; 34 } 35 if(Low[u] == DFN[u]){ 36 scc++; 37 do{ 38 v = Stack[--top]; 39 Instack[v] = false; 40 Belong[v] = scc; 41 num[scc]++; 42 }while( v != u); 43 } 44 } 45 void solve(int N){ 46 memset(DFN,0,sizeof(DFN)); 47 memset(Instack,false,sizeof(Instack)); 48 memset(num,0,sizeof(num)); 49 Index = scc = top = 0; 50 for(int i = 1;i <= N;i++) 51 if(!DFN[i]) 52 Tarjan(i); 53 } 54 void init(){ 55 tot = 0; 56 memset(head,-1,sizeof(head)); 57 } 58 int main(){ 59 int n,m,u,v; 60 while(scanf("%d%d",&n,&m)!=EOF){ 61 if(!n&&!m) break; 62 init(); 63 for(int i=1;i<=m;i++){ 64 scanf("%d%d",&u,&v); 65 addedge(u,v); 66 } 67 solve(n); 68 bool flag=true; 69 // cout<<scc<<endl; 70 if(scc==1) cout<<"Yes"<<endl; 71 else cout<<"No"<<endl; 72 } 73 return 0; 74 }