Poj 1904 King's Quest strong connected components

Source: Internet
Author: User

Topic Links:

http://poj.org/problem?id=1904

Test instructions

There are n princes and N princesses, the Prince can only marry his favorite princess (a prince may have a number of favorite princess), has now given a perfect match, ask each Prince can take which princess, and guarantee to take a princess, the overall still exist perfect match.

Exercises

1. Build the map:

If Prince U is right for Princess V, then one side u->v. In the perfect match given by the example, if Prince U marries Princess V, even a side v->u.

2, to find strong connected components:

If the prince and his beloved princess belong to the same strong connecting component, then the Prince can marry the princess.

1#include <iostream>2#include <algorithm>3#include <cstdio>4#include <stack>5#include <vector>6#include <cstring>7 using namespacestd;8 9 Const intmaxn=4444;Ten  One intN; A  - structedge{ -     intV,ne; theEdge (intVintne): V (v), NE (NE) {} - Edge () {} -}egs[201010+MAXN]; -  + intHead[maxn],tot; -  + voidAddedge (intUintv) { Aegs[tot]=Edge (V,head[u]); athead[u]=tot++; - } -  - intpre[maxn],lowlink[maxn],sccno[maxn],dfs_clock,scc_cnt; -stack<int>S; - //read in write optimization in intScan () { -     intret=0, flag=0;Charch; to     if((Ch=getchar ()) = ='-') flag=1; +     Else if(ch>='0'&&ch<='9') ret=ch-'0'; -      while((Ch=getchar ()) >='0'&&ch<='9') ret=ret*Ten+ch-'0'; the     returnflag?-Ret:ret; * } $ Panax Notoginseng void  out(intx) { -     if(x>9) out(x/Ten); thePutchar (%Ten+'0'); + } A //Strong Connectivity the voidDfsintu) { +pre[u]=lowlink[u]=++Dfs_clock; - s.push (u); $      for(inti=head[u];i!=-1; i=egs[i].ne) { $edge& e=Egs[i]; -         intv=e.v; -         if(!Pre[v]) { the Dfs (v); -lowlink[u]=min (lowlink[u],lowlink[v]);Wuyi}Else if(!Sccno[v]) { thelowlink[u]=min (lowlink[u],pre[v]); -         } Wu     }  -     if(lowlink[u]==Pre[u]) { Aboutscc_cnt++; $          for(;;) { -             intx=S.top (); S.pop (); -sccno[x]=scc_cnt; -             if(X==u) Break;  A         } +     } the } -  $ voidFIND_SCC (intN) { theDfs_clock=scc_cnt=0; thememset (Sccno,0,sizeof(SCCNO)); thememset (PRE,0,sizeof(pre)); the      for(intI=0; i<n;i++){ -         if(!Pre[i]) DFS (i); in     } the } the //Building Map About voidbuild () { the     intcnt,v; the      for(intI=0; i<n;i++){ theCnt=Scan (); +          while(cnt--){ -V=scan (); v--;  theAddedge (i,v+N);Bayi         } the     } the      for(intI=0; i<n;i++){ -V=scan (); v--; -Addedge (v+n,i); the     } the } the  the voidinit () { -memset (head,-1,sizeof(head)); thetot=0; the } the 94 intans[201010],t; the  the intMain () { the      while(SCANF ("%d", &n) = =1&&N) {98 init (); About build (); -FIND_SCC (2*N);101          for(intI=0; i<n;i++){102t=0;103              for(intj=head[i];j!=-1; j=egs[j].ne) {                104edge& e=Egs[j]; the                 intv=e.v;106                 if(Sccno[i]==sccno[v]) ans[t++]=v;107             }108Sort (ans,ans+t);109              out(t); the              for(intI=0; i<t;i++){111Putchar (' '); the                  out(ans[i]+1-N);113             } thePutchar ('\ n'); the         } the     }117     return 0;118}

Poj 1904 King's Quest strong connected components

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.