Maze Castle
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 11070 Accepted Submission (s): 4948
Problem description in order to train the sense of direction, Gardon built a large castle, there are N rooms (n<=10000) and M-channel (m<=100000), each channel is one-way, That is, if a channel is said to be connected to room A and room B, only the room B can be reached by this passage, but it does not mean that it can be reached by Room B. Gardon need to ask you to write a procedure to confirm whether any two rooms are interconnected, namely: for arbitrary I and J, there is at least one path can be from room I to room J, there is a path can be from room J to room I.
The input input contains more than one set of data, the first line of inputs has two numbers: N and M, and the next m row has two numbers a and B, indicating that a passage can come from room A to room B. The file ends with two 0.
Output for each set of data entered, if any two rooms are connected to each other, outputs "Yes", otherwise output "No".
Sample Input3 31 22 33 13 31 22 33 20 0
Sample Outputyesno have a template title template for the graph indent 1:kosaraju
#include"Cstdio"#include"CString"#include"Vector"using namespacestd;Const intmaxn=10005; Vector<int>G[maxn];vector<int>Rg[maxn];vector<int>vs;intv,e;intVis[maxn];inlineintMaxintAintb) { returna > B?a:b;}voidDfsintu) {Vis[u]=1; for(intI=0; I<g[u].size (); i++) if(!Vis[g[u][i]]) DFS (g[u][i]); Vs.push_back (u);}voidRdfsintu) {Vis[u]=1; for(intI=0; I<rg[u].size (); i++) if(!Vis[rg[u][i]]) Rdfs (Rg[u][i]);}BOOLSCC () {memset (Vis,0,sizeof(VIS)); for(intI=1; i<=v;i++) if(!Vis[i]) DFS (i); memset (Vis,0,sizeof(VIS)); intk=0; for(intI=vs.size ()-1; i>=0; i--) if(!Vis[vs[i]]) {Rdfs (vs[i]); K++; if(k>1)return false; } return true;}intMain () { while(SCANF ("%d%d", &v,&e)!=eof&&V) {vs.clear (); for(intI=1; i<=v;i++) {g[i].clear (); Rg[i].clear (); } for(intI=0; i<e;i++) { intu,v; scanf ("%d%d",&u,&v); G[u].push_back (v); Rg[v].push_back (U); } if(SCC ()) {printf ("yes\n"); } Else{printf ("no\n"); } } return 0;}
Template 2:tarjan
#include"Cstdio"#include"CString"using namespacestd;Const intmaxn=100005;structedge{intTo,next;} ES[MAXN];intv,e;inthead[maxn],ant;voidAdd_edge (intUintv) {es[ant].to=v; Es[ant].next=Head[u]; Head[u]=ant++;}intindex;intDFN[MAXN],LOW[MAXN];intStack[maxn],top;intCnt;inlineintMinintAintb) { returna > B?b:a;}voidTarjan (intu) {Stack[top++]=u; Dfn[u]=low[u]=++index; for(inti=head[u];i!=-1; i=Es[i].next) { intto=es[i].to; if(!Dfn[to]) {Tarjan (to); Low[u]=min (low[u],low[to]); } Elselow[u]=min (low[u],dfn[to]); } if(dfn[u]==Low[u]) { intv; CNT++; Do{v=stack[--top]; } while(u!=v); }}intMain () { while(SCANF ("%d%d", &v,&e)!=eof&&V) {memset (head,-1,sizeof(head)); Ant=0; memset (DFN,0,sizeof(DFN)); memset (Low,0,sizeof(low)); Top=0; CNT=0; Index=0; for(intI=0; i<e;i++) { intu,v; scanf ("%d%d",&u,&v); Add_edge (U,V); } for(intI=1; i<=v;i++) if(!Dfn[i]) {Tarjan (i); if(cnt>1) Break; } if(cnt==1) printf ("yes\n"); Elseprintf ("no\n"); } return 0;}
The topic can also be used and checked for collection.
#include"Cstdio"#include"CString"using namespacestd;Const intmaxn=100005;intv,e;intpar[2][MAXN];//PAR[0][MAXN] Record forward Edge, PAR[1][MAXN] record reverse edgeintFndintXinttype) { if(par[type][x]==x) {returnx; } returnpar[type][x]=fnd (Par[type][x],type);}voidUniteintXinty) { if(x>1) par[0][x]=fnd (Y,0);//Node 1 as the root node if(y>1) par[1][Y]=FND (x,1);}BOOLjudge () { for(intI=2; i<=v;i++) if(Fnd (I,0)!=1|| FND (I,1)!=1)return false;//if all nodes (except 1) can reach 1 nodes along the forward edge and can reach 1 nodes after the opposite side, then the graph is strongly connected . return true;}intMain () { while(SCANF ("%d%d", &v,&e)!=eof&&V) { for(intI=1; i<=v;i++) par[0][i]=par[1][i]=i; for(intI=0; i<e;i++) { intu,v; scanf ("%d%d",&u,&v); Unite (U,V); } if(judge ()) printf ("yes\n"); Elseprintf"no\n"); } return 0;}
HDU1269 (graph indent template problem)