poj2762 strong connectivity + topological sequence

Source: Internet
Author: User

Test instructions: There are n rooms, there is a one-way channel between the different rooms, ask whether any two rooms A, B can be from A to B or from B to a (there is a bar can).

In this case, if some of the points are in the same strong connected component, then these points must be able to reach each other, and if the other points arrive at any point here, they can reach all the points in the strongly connected component, so we need to indent the problem first. After the contraction point in the picture there is no ring, then need all the points have at least one road, so in fact, there is a long chain, and is one-way, if there is a two-way edge will be shrunk, if it is a number of a node of the multiple subtree can not reach each other, so must be a one-way long chain, So as long as the topological sequence to determine the line, you can also for this plot is only a point in the degree of 1, a point out of 1, the remaining point of access is 1 on the line.

1#include <stdio.h>2#include <string.h>3#include <stack>4#include <queue>5 using namespacestd;6 7 Const intmaxn=1005;8 Const intmaxm=6005;9 Ten inthead[2][maxn],point[2][maxm],nxt[2][maxm],size[2]; One intn,t,scccnt; A intID[MAXN],STX[MAXN],LOW[MAXN],SCC[MAXN]; -stack<int>S; -  the voidinit () { -memset (head,-1,sizeof(head)); -size[0]=size[1]=0; -memset (ID,0,sizeof(head)); + } -  + voidAddintAintBintC=0){ Apoint[c][size[c]]=b; atnxt[c][size[c]]=Head[c][a]; -head[c][a]=size[c]++; -     if(c) id[b]++; - } -  - voidDfsints) { instx[s]=low[s]=++T; - S.push (S); to      for(inti=head[0][s];~i;i=nxt[0][i]) { +         intj=point[0][i]; -         if(!Stx[j]) { the Dfs (j); *low[s]=min (low[s],low[j]); $         }Panax Notoginseng         Else if(!Scc[j]) { -low[s]=min (low[s],stx[j]); the         } +     } A     if(low[s]==Stx[s]) { thescccnt++; +          while(1){ -             intu=S.top (); S.pop (); $scc[u]=scccnt; $             if(S==u) Break; -         } -     } the } - Wuyi voidSETSCC () { thememset (STX,0,sizeof(STX)); -memset (SCC,0,sizeof(SCC)); Wut=0; -Scccnt=0; About      for(intI=1; i<=n;++i)if(!Stx[i]) DFS (i); $      for(intI=1; i<=n;++i) { -          for(intj=head[0][i];~j;j=nxt[0][j]) { -             intk=point[0][j]; -             if(scc[i]!=Scc[k]) { AAdd (Scc[i],scc[k],1); +             } the         } -     } $ } the  the BOOLtopo () { thequeue<int>Q; the     intCnt=0; -      for(intI=1; i<=scccnt;++i)if(!Id[i]) Q.push (i); in      while(!Q.empty ()) { the         intu=Q.front (); the Q.pop (); About         if(!q.empty ())return 0; thecnt++; the          for(inti=head[1][u];~i;i=nxt[1][i]) { the             intj=point[1][i]; +id[j]--; -             if(!Id[j]) Q.push (j); the         }Bayi     } the     if(cnt==scccnt)return 1; the     return 0; - } -  the intMain () { the     intT; thescanf"%d",&T); the      while(t--){ -         intm; thescanf"%d%d",&n,&m); the init (); the          while(m--){94             intb; thescanf"%d%d",&a,&b); the Add (A, b); the         }98 SETSCC (); About         if(Topo ()) printf ("yes\n"); -         Elseprintf"no\n");101     }102     return 0;103}
View Code

poj2762 strong connectivity + topological sequence

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.