【改了一天的拓扑排序】POJ 1094——Sorting It All Out
来源:点击打开链接
不知道怎么回事,wa了整整一天。。在绝望的时候AC了。
重点是分步处理和三种情况的判断。
1、判断是否成环,成环了直接输出错误信息。
2、然后一条边一条边的加入,进行拓扑排序,如果出度为0的点多于两个,继续判断之,如果到所有点都加入了但仍然没有判断出来,输出第三种情况。
3、以上两种情况都不存在,输出拓扑排序的路径信息。
- #include <iostream>
- #include <cstring>
- #include <string>
- using namespace std;
- int mat[105][105];
- int ans[105];
- int indegree[105];
- int length,rela,tflag,loopflag;
- int TopoLogic()
- {
- int loopcount=0;//0入度数统计
- int entrypoint=0;//第几个检测到0入度,加入队列
- int t=0;
- int flag=1;
- memset(indegree,0,sizeof(indegree));
- for (int a=0;a<length;a++) //求目前图的入度
- {
- for (int b=0;b<length;b++)
- {
- if (mat[a][b])
- indegree[b]++;
- }
- }
- for (int i=0;i<length;i++)
- {
- loopcount=0;
- for (int j=0;j<length;j++)
- {
- if (indegree[j]==0)
- {
- loopcount++;
- entrypoint=j;
- }
- }
- if (loopcount>1)
- flag=-1; //出现多种情况,不好说
- if (loopcount==0 && loopcount!=length)//第二个条件很容易忘
- return 0;
- ans[t++]=entrypoint;
- indegree[entrypoint]=-1;//去点
- for (int p=0;p<length;p++)
- {
- if (mat[entrypoint][p]==1)
- indegree[p]--;
- }
- }
- return flag;
- }
- int main()
- {
- while (cin>>length>>rela)
- {
- if (length==0 && rela==0)
- break;
- memset(mat,0,sizeof(mat));
- memset(indegree,0,sizeof(indegree));
- tflag=0;//队列标志
- string tar;
- for (int t=1;t<=rela;t++)
- {
- int resulter;
- cin>>tar;
- mat[tar[0]-'A'][tar[2]-'A']=1;
- memset(ans,0,sizeof(ans));
- if (tflag==0)
- {
- resulter=TopoLogic();
- //cout<<resulter<<endl;
- }
- if (resulter==1 && tflag==0)
- {
- cout<<"Sorted sequence determined after "<<t<<" relations: ";
- for (int a=0;a<length;a++)
- cout<<(char)(ans[a]+'A');
- cout<<"."<<endl;
- tflag=1;
- }
- if(resulter==0 && tflag==0)
- {
- cout<<"Inconsistency found after "<<t<<" relations."<<endl;
- tflag=1;
- }
- }
- if(tflag==0)
- cout<<"Sorted sequence cannot be determined."<<endl; //全部判断完再输出矛盾!!
- }
- return 0;
- }
【改了一天的拓扑排序】POJ 1094——Sorting It All Out的更多相关文章
- 拓扑排序 POJ 1094 Sorting It All Out
题意:给定N个字和M行他们之间的关系,要求输出他们的拓扑排序.此题采用边输入边检测的方式,如果发现环,就结束并输出当前行号:如果读取到当前行时,可以确定拓扑序列就输出,不管后面的输入(可能包含环路): ...
- 拓扑排序 POJ 1049 Sorting It All Out
题目传送门 /* 拓扑排序裸题:有三种情况: 1. 输入时发现与之前的矛盾,Inconsistency 2. 拓扑排序后,没有n个点(先判断cnt,即使一些点没有边连通,也应该是n,此时错误是有环): ...
- ACM: poj 1094 Sorting It All Out - 拓扑排序
poj 1094 Sorting It All Out Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld & ...
- 拓扑排序(Topological Sorting)
一.什么是拓扑排序 在图论中,拓扑排序(Topological Sorting)是一个有向无环图(DAG, Directed Acyclic Graph)的所有顶点的线性序列.且该序列必须满足下面两个 ...
- 拓扑排序 POJ 2367
今天网易的笔试,妹的,算法题没能A掉,虽然按照思路写了出来,但是尼玛好歹给个测试用例的格式呀,吐槽一下网易的笔试出的太烂了. 就一道算法题,比较石子重量,个人以为解法应该是拓扑排序. 就去POJ找了道 ...
- [ACM_模拟] POJ 1094 Sorting It All Out (拓扑排序+Floyd算法 判断关系是否矛盾或统一)
Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...
- poj 1094 Sorting It All Out (拓扑排序)
http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- 图论之拓扑排序 poj 2367 Genealogical tree
题目链接 http://poj.org/problem?id=2367 题意就是给定一系列关系,按这些关系拓扑排序. #include<cstdio> #include<cstrin ...
- POJ 1094 Sorting It All Out (拓扑排序,判断序列是否唯一,图是否有环)
题意:给出n个字符,m对关系,让你输出三种情况: 1.若到第k行时,能判断出唯一的拓扑序列,则输出: Sorted sequence determined after k re ...
随机推荐
- POJ 2485:Highways(最小生成树&&prim)
Highways Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21628 Accepted: 9970 Descrip ...
- android的animator
3.0 以前,android支持两种动画模式,tween animation,frame animation,在android3.0中又引入了一个新的动画系统:property animation,这 ...
- 演练2-2:Guestbook示例应用程序
为使Guestbook应用程序有用,我们需要为用户提供一些提交数据的方式,将这些数据存储起来,以便稍后进行查看.为了达到这一目标,我们打算对应用程序添加一个数据库,将其作为该留言簿的后台存储库. 1. ...
- ESRI Shapefiles (SHP)
ESRI Shapefiles (SHP) Also known as ESRI ArcView Shapefiles or ESRI Shapefiles. ESRI is the company ...
- mysql备份数据库几种方法
方法一 cmd 到mysql bin文件夹下用 例如以下命令 mysqldump --opt -h192.168.0.156 -uusername -ppassword --skip-lock-tab ...
- Bernstein polynomials
Bernstein多项式能够用来一致逼近闭区间上的连续函数. 对于[0,1]上的连续函数f(x),定义Bernstein多项式 B_n(f,x) = sum{k=0..n} f(k/n)C(k,n)t ...
- springmvc + jquery easyui实现分页显示
如有不明确的地方,戏迎增加QQ群交流:66728073 推荐一本Java学习的书:深入理解Java7 一,下载并导入jquery easyui的导 <link rel="st ...
- C语言,C++,static
术语static有着不寻常的历史.起初,在C中引入关键字static是为了表示退出一个块后仍然存在的局部变量.随后,static在C中有了第二种含义:用来表示不能被其它文件访问的全局变量和函数.为了避 ...
- maven项目部署到Repository(Nexus)
目录[-] (一)下载并安装Nexus (二)配置Nexus Repository 说明: (三)在项目中配置Nexus Repository的信息 (四)发布到Nexus Repository 本文 ...
- json 模块
JSON: JSON-JSON (JavaScript 对象标记) 编码/解码 简介: use JSON; # imports encode_json, decode_json, to_json an ...