poj1094
来源:互联网 发布:linux trace 编辑:程序博客网 时间:2024/05/19 16:51
这个拓扑排序有一些特殊,在给出m个信息时,只要前r个信息已经足够判断的时候就要输出判断结果,但输入还要继续,这是Poj的风格了,不过判断时一定要小心优先级,要先判断是否有环,然后是否有唯一序列,最后才是不确定序列,这里我坑了太久,看了别人的提示才知道。拓扑排序做法就是按照找入度为0的点作为起始点进行排序,这里有bfs较为贴切。总归来说还是要细心,理解逻辑的先后顺序。
#include<cstdio>#include<cstdlib>#include<queue>#include<cstring>using namespace std;int n,m,g[30][30],inconsistency,determined,deg[30],in[30],vis[30];queue<int>q1,q2;void toposort(){ bool flag=0; memset(vis,0,sizeof(vis)); while(!q1.empty())q1.pop(); while(!q2.empty())q2.pop(); for(int i=0; i<n; i++){ in[i]=deg[i]; if(in[i]==0)q1.push(i); } while(!q1.empty()){ if(q1.size()>1)flag=1; int p=q1.front(); q2.push(p); q1.pop(); for(int i=0; i<n; i++)if(g[p][i]){ if(--in[i]==0)q1.push(i); } } if(!flag&&q2.size()==n)determined=1; if(q2.size()!=n)inconsistency=1; return ;}int main(){ char a,b,op; while(scanf("%d %d",&n,&m)&&n!=0&&m!=0){ getchar(); inconsistency=0,determined=0; memset(deg,0,sizeof(deg)); memset(g,0,sizeof(g)); for(int r=1; r<=m; r++){ scanf("%c%c%c",&a,&op,&b); getchar(); if(!inconsistency&&!determined){//还没出结果 if(g[b-'A'][a-'A']==1){ printf("Inconsistency found after %d relations.\n",r); inconsistency=1; } else{ deg[b-'A']++; g[a-'A'][b-'A']=1; toposort(); if(inconsistency==1)printf("Inconsistency found after %d relations.\n",r); if(determined==1){ printf("Sorted sequence determined after %d relations: ",r); while(!q2.empty()){ printf("%c",q2.front()+'A'); q2.pop(); } printf(".\n"); } } } } if(!inconsistency&&!determined)printf("Sorted sequence cannot be determined.\n"); } return 0;}
0 0
- POJ1094
- poj1094
- poj1094
- poj1094
- poj1094
- poj1094
- poj1094
- poj1094
- poj1094
- poj1094
- poj1094
- poj1094
- POJ1094
- poj1094注意事项
- POJ1094解题报告
- Poj1094拓扑排序总结
- 拓扑排序 poj1094
- poj1094 拓扑排序
- 使用GDI+ 缩放图片
- poj1321 dfs
- ZOJ-1392
- [回溯]Stamps UVA165
- HBase 异常记录 ERROR: org.apache.hadoop.hbase.MasterNotRunningException: Retried 7 times
- poj1094
- C# 托盘程序设计
- 黑马程序员--交通灯管理系统
- Object 和 String 的equals比较
- Kali Linux渗透测试实战 2.2 操作系统指纹识别
- [转]软件开发基本原则(二)典型错误
- 凌乱的2013咱就这样了,祝福我在2014找回咱的场子
- Linux下Mysql的一些基本操作:
- 死亡