来源:点击打开链接

不知道怎么回事,wa了整整一天。。在绝望的时候AC了。

重点是分步处理和三种情况的判断。

1、判断是否成环,成环了直接输出错误信息。

2、然后一条边一条边的加入,进行拓扑排序,如果出度为0的点多于两个,继续判断之,如果到所有点都加入了但仍然没有判断出来,输出第三种情况。

3、以上两种情况都不存在,输出拓扑排序的路径信息。

  1. #include <iostream>
  2. #include <cstring>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int mat[105][105];
  7. int ans[105];
  8. int indegree[105];
  9. int length,rela,tflag,loopflag;
  10.  
  11. int TopoLogic()
  12. {
  13. int loopcount=0;//0入度数统计
  14. int entrypoint=0;//第几个检测到0入度,加入队列
  15. int t=0;
  16. int flag=1;
  17.  
  18. memset(indegree,0,sizeof(indegree));
  19.  
  20. for (int a=0;a<length;a++) //求目前图的入度
  21. {
  22. for (int b=0;b<length;b++)
  23. {
  24. if (mat[a][b])
  25. indegree[b]++;
  26. }
  27. }
  28. for (int i=0;i<length;i++)
  29. {
  30. loopcount=0;
  31. for (int j=0;j<length;j++)
  32. {
  33. if (indegree[j]==0)
  34. {
  35. loopcount++;
  36. entrypoint=j;
  37. }
  38. }
  39. if (loopcount>1)
  40. flag=-1; //出现多种情况,不好说
  41. if (loopcount==0 && loopcount!=length)//第二个条件很容易忘
  42. return 0;
  43.  
  44. ans[t++]=entrypoint;
  45. indegree[entrypoint]=-1;//去点
  46. for (int p=0;p<length;p++)
  47. {
  48. if (mat[entrypoint][p]==1)
  49. indegree[p]--;
  50. }
  51.  
  52. }
  53. return flag;
  54.  
  55. }
  56.  
  57. int main()
  58. {
  59.  
  60. while (cin>>length>>rela)
  61. {
  62. if (length==0 && rela==0)
  63. break;
  64. memset(mat,0,sizeof(mat));
  65. memset(indegree,0,sizeof(indegree));
  66.  
  67. tflag=0;//队列标志
  68. string tar;
  69.  
  70. for (int t=1;t<=rela;t++)
  71. {
  72. int resulter;
  73. cin>>tar;
  74. mat[tar[0]-'A'][tar[2]-'A']=1;
  75. memset(ans,0,sizeof(ans));
  76. if (tflag==0)
  77. {
  78. resulter=TopoLogic();
  79. //cout<<resulter<<endl;
  80. }
  81.  
  82. if (resulter==1 && tflag==0)
  83. {
  84. cout<<"Sorted sequence determined after "<<t<<" relations: ";
  85. for (int a=0;a<length;a++)
  86. cout<<(char)(ans[a]+'A');
  87. cout<<"."<<endl;
  88. tflag=1;
  89. }
  90.  
  91. if(resulter==0 && tflag==0)
  92. {
  93. cout<<"Inconsistency found after "<<t<<" relations."<<endl;
  94. tflag=1;
  95. }
  96.  
  97. }
  98. if(tflag==0)
  99. cout<<"Sorted sequence cannot be determined."<<endl; //全部判断完再输出矛盾!!
  100.  
  101. }
  102. return 0;
  103. }

【改了一天的拓扑排序】POJ 1094——Sorting It All Out的更多相关文章

  1. 拓扑排序 POJ 1094 Sorting It All Out

    题意:给定N个字和M行他们之间的关系,要求输出他们的拓扑排序.此题采用边输入边检测的方式,如果发现环,就结束并输出当前行号:如果读取到当前行时,可以确定拓扑序列就输出,不管后面的输入(可能包含环路): ...

  2. 拓扑排序 POJ 1049 Sorting It All Out

    题目传送门 /* 拓扑排序裸题:有三种情况: 1. 输入时发现与之前的矛盾,Inconsistency 2. 拓扑排序后,没有n个点(先判断cnt,即使一些点没有边连通,也应该是n,此时错误是有环): ...

  3. ACM: poj 1094 Sorting It All Out - 拓扑排序

    poj 1094 Sorting It All Out Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & ...

  4. 拓扑排序(Topological Sorting)

    一.什么是拓扑排序 在图论中,拓扑排序(Topological Sorting)是一个有向无环图(DAG, Directed Acyclic Graph)的所有顶点的线性序列.且该序列必须满足下面两个 ...

  5. 拓扑排序 POJ 2367

    今天网易的笔试,妹的,算法题没能A掉,虽然按照思路写了出来,但是尼玛好歹给个测试用例的格式呀,吐槽一下网易的笔试出的太烂了. 就一道算法题,比较石子重量,个人以为解法应该是拓扑排序. 就去POJ找了道 ...

  6. [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 ...

  7. poj 1094 Sorting It All Out (拓扑排序)

    http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  8. 图论之拓扑排序 poj 2367 Genealogical tree

    题目链接 http://poj.org/problem?id=2367 题意就是给定一系列关系,按这些关系拓扑排序. #include<cstdio> #include<cstrin ...

  9. POJ 1094 Sorting It All Out (拓扑排序,判断序列是否唯一,图是否有环)

    题意:给出n个字符,m对关系,让你输出三种情况:     1.若到第k行时,能判断出唯一的拓扑序列,则输出:         Sorted sequence determined after k re ...

随机推荐

  1. POJ 2485:Highways(最小生成树&amp;&amp;prim)

    Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21628   Accepted: 9970 Descrip ...

  2. android的animator

    3.0 以前,android支持两种动画模式,tween animation,frame animation,在android3.0中又引入了一个新的动画系统:property animation,这 ...

  3. 演练2-2:Guestbook示例应用程序

    为使Guestbook应用程序有用,我们需要为用户提供一些提交数据的方式,将这些数据存储起来,以便稍后进行查看.为了达到这一目标,我们打算对应用程序添加一个数据库,将其作为该留言簿的后台存储库. 1. ...

  4. ESRI Shapefiles (SHP)

    ESRI Shapefiles (SHP) Also known as ESRI ArcView Shapefiles or ESRI Shapefiles. ESRI is the company ...

  5. mysql备份数据库几种方法

    方法一 cmd 到mysql bin文件夹下用 例如以下命令 mysqldump --opt -h192.168.0.156 -uusername -ppassword --skip-lock-tab ...

  6. Bernstein polynomials

    Bernstein多项式能够用来一致逼近闭区间上的连续函数. 对于[0,1]上的连续函数f(x),定义Bernstein多项式 B_n(f,x) = sum{k=0..n} f(k/n)C(k,n)t ...

  7. springmvc + jquery easyui实现分页显示

    如有不明确的地方,戏迎增加QQ群交流:66728073      推荐一本Java学习的书:深入理解Java7 一,下载并导入jquery easyui的导 <link rel="st ...

  8. C语言,C++,static

    术语static有着不寻常的历史.起初,在C中引入关键字static是为了表示退出一个块后仍然存在的局部变量.随后,static在C中有了第二种含义:用来表示不能被其它文件访问的全局变量和函数.为了避 ...

  9. maven项目部署到Repository(Nexus)

    目录[-] (一)下载并安装Nexus (二)配置Nexus Repository 说明: (三)在项目中配置Nexus Repository的信息 (四)发布到Nexus Repository 本文 ...

  10. json 模块

    JSON: JSON-JSON (JavaScript 对象标记) 编码/解码 简介: use JSON; # imports encode_json, decode_json, to_json an ...