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 |
写得不好 贴个代码#include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; int n,m,nown,flag1,flag2; int mpt[30][30],rul[30],path[30],cal[30]; void search() { int i,total=0; queue<int> q; for(i=1;i<=n;i++) if(!rul[i]) { q.push(i); path[++total]=i;//flag1=1表示不确定。 } if(total>1) flag1=1; while(!q.empty()) { int everytime=0; int p=q.front(); q.pop(); for(i=1;i<=n;i++) { if(p==i||mpt[p][i]==0) continue; mpt[p][i]=0; rul[i]--; if(!rul[i] && cal[i]) { q.push(i); path[++total]=i; everytime++; } } if(everytime>1) flag1=1;//flag1=1表示不确定。 } if(total<nown) flag2=1;//flag2=1表示有回路。 } int main() { int c[1005],r[1005],i,j; while(scanf("%d%d",&n,&m)!=EOF,n,m) { for(i=1;i<=m;i++) { char a,b,ch; cin>>a>>ch>>b; c[i]=a-'A'+1; r[i]=b-'A'+1; } for(i=1;i<=m;i++) { nown=flag1=flag2=0;//初始化,依次代表现有图的节点个数,是否不确定,是否有回路。 memset(mpt,0,sizeof(mpt)); memset(rul,0,sizeof(rul)); memset(cal,0,sizeof(cal)); for(j=1;j<=i;j++)//建图。 { cal[r[j]]=cal[c[j]]=1; mpt[c[j]][r[j]]=1; rul[r[j]]++; } for(j=1;j<=30;j++) if(cal[j]) nown++;//计算现有节点个数。 search(); if(flag1!=1 &&flag2!=1)//如果没有回路,确定。 { cout<<"Sorted sequence determined after "<<i<<" relations: "; for(j=1;j<=n;j++) cout<<char(path[j]+'A'-1); cout<<".\n"; break; } else if(flag2==1)//如果有回路。 { cout<<"Inconsistency found after "<<i<<" relations.\n"; break; } } if(flag2!=1 && flag1==1)//如果不确定。 cout<<"Sorted sequence cannot be determined.\n"; } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator