Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
第一道强联通,错误bug百出,wa了不下10次,囧,贴代码纪念。#include<iostream> #include<stdio.h> #include<string.h> using namespace std; const int Max1=10002; const int Max2=50002; struct node{ int last; node*next; }*Head[Max1],*fHead[Max1]; int connect[Max1]; int E[Max2][2]; int num[Max1]; bool flag[Max1]; int nn; void add(int a,int b) { node*temp=new node; temp->last=b; temp->next=Head[a]; Head[a]=temp; node*temp1=new node; temp1->last=a; temp1->next=fHead[b]; fHead[b]=temp1; } void dfs1(int i) { node*ptr=Head[i]; flag[i]=1; for(;ptr;ptr=ptr->next) { if(!flag[ptr->last]) dfs1(ptr->last); } num[++nn]=i; } void dfs2(int i,int nn) { node*ptr=fHead[i]; flag[i]=0; for(;ptr;ptr=ptr->next) if(flag[ptr->last]) dfs2(ptr->last,nn); connect[i]=nn; } int main() { int n,m; while(scanf("%d%d",&n,&m)!=EOF) { for(int i=1;i<=n;i++) Head[i]=NULL; for(int i=1;i<=n;i++) fHead[i]=NULL; for(int i=1;i<=m;i++) { scanf("%d%d",&E[i][0],&E[i][1]); add(E[i][0],E[i][1]); } memset(flag,0,sizeof(flag)); nn=0; for(int i=1;i<=n;i++) if(!flag[i]) dfs1(i); nn=0; for(int i=n;i>=1;i--) if(flag[num[i]]) { nn++; dfs2(num[i],nn); } bool flag1[Max1]={false}; for(int i=1;i<=m;i++) if(!flag1[connect[E[i][0]]]&&connect[E[i][0]]!=connect[E[i][1]]) flag1[connect[E[i][0]]]=1; int numm=0; int temp; for(int i=1;i<=n;i++) if(!flag1[connect[i]]) { flag1[connect[i]]=1; numm++; temp=connect[i]; } if(numm!=1) printf("%d\n",0); else { int ans=0; for(int i=1;i<=n;i++) if(connect[i]==temp) ans++; printf("%d\n",ans); } } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator