poj 1094
来源:互联网 发布:淘宝店铺图片要求 编辑:程序博客网 时间:2024/05/12 15:15
拓扑排序,做的时候wa了n次,后来找来数据,又想了想,发现了自己的错误,就是当拓扑排序的时候出现了不确定的情况,还要在拓扑下去,因为可能还会有矛盾的情况出现,保证没矛盾的情况下才可以说是不确定的情况
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 using namespace std; 6 const int maxn=30; 7 int e[maxn*maxn],head[maxn],next[maxn*maxn],in[maxn],in2[maxn]; 8 int n,m,tot,path[maxn]; 9 int solve()10 {11 int i;12 int top=0;13 int sta[maxn];14 for(i=0;i<n;i++)15 {16 if(!in[i])17 sta[top++]=i;18 }19 int j,v;20 int flag=1;21 int tem;22 for(i=0;i<n;i++)23 {24 if(top==0) return 0;25 if(top>1) flag=-1;26 tem=sta[--top];27 path[i]=tem;28 for(j=head[tem];j!=-1;j=next[j])29 {30 v=e[j];31 in[v]--;32 if(in[v]==0) sta[top++]=v;33 }34 }35 return flag;36 }37 int main()38 {39 // freopen("1.txt","r",stdin);40 // freopen("2.txt","w",stdout);41 while(scanf("%d%d",&n,&m))42 {43 if(!n&&!m) break;44 int i;45 char s[4];46 tot=0;47 memset(head,-1,sizeof(head));48 memset(in2,0,sizeof(in2));49 int ans;50 int j;51 for(i=0;i<m;i++)52 {53 scanf("%s",s);54 s[0]-='A';s[2]-='A';55 in2[s[2]]++;56 for(j=0;j<n;j++) in[j]=in2[j];57 e[tot]=s[2];58 next[tot]=head[s[0]];59 head[s[0]]=tot++;60 ans=solve();61 if(ans==1||ans==0) break;62 }63 if(i<m)64 {65 for(j=i+1;j<m;j++) scanf("%s",s);66 if(ans==1)67 {68 printf("Sorted sequence determined after %d relations: ",i+1);69 for(j=0;j<n;j++) printf("%c",path[j]+'A');70 }71 else printf("Inconsistency found after %d relations",i+1);72 }73 else printf("Sorted sequence cannot be determined");74 printf(".\n");75 }76 return 0;77 }
- POJ 1094
- poj 1094
- poj 1094
- poj 1094
- poj 1094
- poj.1094
- poj.1094
- POJ 1094
- poj 1094
- poj 1094
- poj 1094
- poj 1094
- POJ 1094
- poj 1094
- poj 1094
- poj--1094
- POJ 1094
- POJ 1094
- 仿百度文库、豆丁在线阅读
- poj 1947
- poj 3254
- poj 1322
- poj 3612
- poj 1094
- poj 1087
- poj 1112
- poj 1135
- 图论500题
- hdu 1423 LICS
- hdu 4512
- StringTokenizer类的使用
- tengine后话