POJ 1094 Sorting It All Out (拓扑排序)
来源:互联网 发布:数据录入员工作描述 编辑:程序博客网 时间:2024/04/29 12:41
题目链接
#include<iostream>#include<cstring>#include<cstdio>using namespace std;struct node{ int in;//入度 bool valid;//是否有效 int n;//以此为开始的边有n个 int s[30];}p[30],q[30];//p是副本int top(int n,char str[]){ int c=0,r=0; for(int j=0;j<n;j++)//总共找n次 { int flg=0,flg2=0,pi; for(int i=0;i<n;i++) { if(p[i].valid) { flg=1; if(p[i].in==0) { flg2++; if(flg2>1)//如果有多个入度为零的点,这种情况即使没有环,也不会有唯一解,但这时不能直接判断它是无法确定,我们要判断一下是否有环 r=2; pi=i;//把入度为零的点保存下来,即使有多个我们也只记录最新的并在下面处理,另外的下次还能找到 } } } if(flg&&!flg2)//如果循环一遍存在有效的点,但所有的点中没有入度为零的,判断为有环 return 1; if(!flg)//如果循环一遍,发现没有未处理的点(也就是有效点),这时判断为无法判断 return 2; str[c++]=pi+'A';//记录拓扑排序,只有返回0时str才是有效的 if(c==n)//完成排序 { str[c]=0;//结束字符串 return r; } for(int k=0;k<p[pi].n;k++)//把所有以它为起点的边的终点的入度减1 p[p[pi].s[k]].in--; p[pi].valid=0;//处理过,设为无效 } return 2;}int main(){ int n,m,i; char a,o,b; while(cin>>n>>m,n||m) { memset(q,0,sizeof(q)); bool con=1; for(i=1;i<=m;i++) { cin>>a>>o>>b; if(con) { q[a-'A'].valid=1; q[b-'A'].valid=1; q[b-'A'].in++; q[a-'A'].s[q[a-'A'].n++]=b-'A'; memcpy(p,q,sizeof(q)); char str[30]; int r=top(n,str); if(r==0) { cout<<"Sorted sequence determined after "<<i<<" relations: "<<str<<"."<<endl; con=0; } else if(r==1) { cout<<"Inconsistency found after "<<i<<" relations."<<endl; con=0; } else if(i==m&&r==2)//最后一遍还是无法确定,可以判断为无法确定 { cout<<"Sorted sequence cannot be determined."<<endl; con=0; } } } } return 0;}
- POJ 1094 Sorting It All Out(拓扑排序)
- Poj 1094-Sorting It All Out [拓扑排序]
- POJ 1094-Sorting It All Out 拓扑排序
- zoj 1060 || poj 1094 Sorting It All Out(拓扑排序)
- POJ 1094 Sorting It All Out 拓扑排序
- POJ 1094 Sorting It All Out 拓扑排序
- POJ 1094 Sorting It All Out 拓扑排序
- poj 1094 Sorting It All Out 拓扑排序
- POJ 1094 Sorting It All Out (拓扑排序)
- POJ 1094 Sorting It All Out 拓扑排序
- poj 1094 Sorting It All Out(拓扑排序)
- poj 1094 Sorting It All Out 拓扑排序
- poj 1094 Sorting It All Out 拓扑排序
- poj 1094 Sorting It All Out (拓扑排序)
- poj 1094 Sorting It All Out (拓扑排序)
- Poj 1094 Sorting It All Out——拓扑排序
- POJ 1094 Sorting It All Out 拓扑排序+Floyd算法
- POJ 1094 Sorting It All Out (拓扑排序) - from lanshui_Yang
- 简单的bochs配置(个人理解)
- 黑马程序员——面向对象学习笔记回忆
- 黑马程序员——多线程学习笔记日志
- 用c语言写Dos下的贪吃蛇(奇迹冬瓜)
- TFS merge简单原理
- POJ 1094 Sorting It All Out (拓扑排序)
- 理解javascript中的MVVM开发模式
- 《软件管理沉思录》第二章读书笔记
- xocde4.3 如何手动安装iPhone simulator 4.3/5.0
- HDU 1587 Flowers
- python分割有plist文件的精灵表
- Ubuntu不能关机解决方案
- C++跨平台开发方法/工具
- 新一代跨平台C++开发工具Code::Blocks简介