codevs1506 Messenger (Kosaraju algorithm)

Source: Internet
Author: User

- -   - -   - -   - -

One () dozen [] watched all night .....

/*finding strong connected component Kosaraju algorithm side-table chart positive and negative structure two graphs run both sides to write down the order of the stack and the specific information of each strong connected component.*/#include<iostream>#include<cstring>#include<cstdio>#include<stack>#defineMAXN 1010#defineMaxx 10010using namespacestd;intn,m,num,head1[maxn],head2[maxn],f[maxn],cnt;intFlag[maxn],ans[maxn][maxn];stack<int>s;structnode{intU,v,per;} E1[maxx],e2[maxx];voidInput ()//The input composition E1 positive E2 as the inverse{ intI,x,y; CIN>>n>>m; for(i=1; i<=m;i++) {cin>>x>>y; Num++; E1[NUM].U=x; E1[NUM].V=y; E1[num].per=Head1[x]; HEAD1[X]=num; E2[NUM].U=y; E2[NUM].V=x; E2[num].per=Head2[y]; Head2[y]=num; }}voidDFS1 (intk) {F[k]=1; for(intP=head1[k];p; p=e1[p].per) { if(!F[E1[P].V]) DFS1 (E1[P].V); } s.push (k);//record entry stack sequence}voidDFS2 (intk) {F[k]=1; ans[cnt][++ans[cnt][0]]=k;//Note each point of the first CNT strong connected component for(intI=head2[k];i;i=e2[i].per)if(f[e2[i].v]==0) DFS2 (E2[I].V); if(ans[cnt][0]>1) flag[k]=1;//if the size of the first CNT strongly connected component is >1 then x belongs to a strongly connected component}voidKosaraju () {memset (f),0,sizeof(f)); for(intI=1; i<=n;i++)//run the first time DFS each point into the stack sequentially { if(f[i]==0) DFS1 (i); } memset (F,0,sizeof(f)); while(!s.empty ())//run the second time. DFS marks whether each point belongs to a strong connected component and calculates the quantity { inttmp=S.top (); S.pop (); if(f[tmp]==0) {CNT++;//count Strong connected componentsDFS2 (TMP); } }}voidPrintf () { for(intI=1; i<=n;i++) if(flag[i]==1) cout<<"T"<<Endl; Elsecout<<"F"<<Endl;}intMain () {Input (); Kosaraju (); Printf (); return 0;}

codevs1506 Messenger (Kosaraju algorithm)

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.