题目链接:点击查看

题目大意:给出N个点以及M个比较关系,问在第几个数字可以确定出唯一的序列,或者判断出矛盾的序列,或者最后也无法确定出一个唯一的序列

题目分析:关于这个题目可以直接分类讨论,可以直接用拓扑排序判环,判断是否有唯一确定的顺序,这里我用floyd写了一下,可以直接传递闭包,然后根据题目的条件来判断是否有唯一答案,在确定了唯一答案之后就可以跑一边拓扑序输出答案了

有个小坑需要注意一下,每次拓扑序或者传递闭包判断的过程中,如果判断出肯定无序,不要着急返回,因为矛盾的优先级要高于无序,无序的结果要等全部答案都判断完毕后再返回

代码:

拓扑排序:

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<sstream>
#include<cmath>
using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=50;bool maze[N][N];int num[N];int ans[N];//存结果 int n,t,m;int toposort()
{int cnt=0; int flag=1;int temp[N];for(int i=1;i<=n;i++)//备份入度 {temp[i]=num[i];}for(int i=1;i<=n;i++)//n个数 {int m=0;//存入度为零的个数int pos; //记录位置 for(int j=1;j<=n;j++) //遍历所有点 {if(temp[j]==0){pos=j;m++;}}if(m==0)//有环return 0;else if(m>1)//无序,但必须继续遍历,查找是否有环存在 flag=-1;ans[cnt++]=pos;temp[pos]=-1;for(int j=1;j<=n;j++)//将入度为0的点移除,并将所有与此点相连的点入度减一{if(maze[pos][j])temp[j]--;} }return flag;
}int main()
{
//  freopen("input.txt","r",stdin);while(scanf("%d%d",&n,&m)!=EOF&&n+m){char s[5];bool ok=false;memset(maze,false,sizeof(maze));memset(num,0,sizeof(num));for(int i=1;i<=m;i++){scanf("%s",s);if(ok)continue;int u=s[0]-'A'+1;int v=s[2]-'A'+1;maze[u][v]=true;num[v]++;int flag=toposort();if(flag==1){ok=true;printf("Sorted sequence determined after %d relations: ",i);for(int i=0;i<n;i++)putchar(ans[i]+'A'-1);putchar('.');cout<<endl;}else if(flag==0){ok=true;printf("Inconsistency found after %d relations.\n",i);}}if(!ok)printf("Sorted sequence cannot be determined.\n");}return 0;
}

floyd传递闭包+拓扑排序:

#include<iostream>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cctype>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<set>
#include<map>
#include<sstream>
using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=30;int n,m;int maze[N][N];//maze[a][b]:a<bint temp[N][N];int du[N];bool vis[N];int cal()
{for(int i=0;i<n;i++)for(int j=0;j<n;j++)temp[i][j]=maze[i][j];for(int k=0;k<n;k++)for(int i=0;i<n;i++)for(int j=0;j<n;j++)temp[i][j]|=temp[i][k]&temp[k][j];bool flag=true;for(int i=0;i<n;i++)for(int j=0;j<n;j++)if(i!=j&&temp[i][j]==temp[j][i]){if(temp[i][j]==1)//矛盾return 2;else//无法确定 flag=false;}return flag;
}void print()
{memset(vis,false,sizeof(vis));queue<int>q;string ans;for(int i=0;i<n;i++)if(!du[i]){q.push(i);vis[i]=true;}while(q.size()){int cur=q.front();q.pop();ans+=char(cur+'A');for(int i=0;i<n;i++){if(vis[i])continue;if(maze[cur][i])du[i]--;if(!du[i]){q.push(i);vis[i]=true;}}}printf("%s",ans.c_str());
}int main()
{
//  freopen("input.txt","r",stdin);
//  ios::sync_with_stdio(false);while(scanf("%d%d",&n,&m)!=EOF&&n+m){memset(maze,0,sizeof(maze));memset(du,0,sizeof(du));bool flag=false;for(int i=1;i<=m;i++){char s[5];scanf("%s",s);if(flag)continue;maze[s[0]-'A'][s[2]-'A']=1;du[s[2]-'A']++;int ans=cal();if(ans==1){printf("Sorted sequence determined after %d relations: ",i);print();printf(".\n");flag=true;}if(ans==2){printf("Inconsistency found after %d relations.\n",i);flag=true;}}if(!flag)printf("Sorted sequence cannot be determined.\n");}return 0;
}

POJ - 1094 Sorting It All Out(拓扑排序+floyd传递闭包)相关推荐

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

    题意:给你一些大写字母间的偏序关系,然后让你判断能否唯一确定它们之间的关系,或者所给关系是矛盾的,或者到最后也不能确定它们之间的关系.   由DAG图节点的偏序关系确定节点的排序可以由拓扑排序求出.而 ...

  2. POJ - 1094 Sorting It All Out(拓扑排序)

    https://vjudge.net/problem/POJ-1094 题意 对于N个大写字母,给定它们的一些关系,要求判断出经过多少个关系之后可以确定它们的排序或者排序存在冲突,或者所有的偏序关系用 ...

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

    2018-3-25 拓扑排序的题目,需要注意的是,这里是边输入边判断的,之前有一组数据一直不知道为什么不过: A>F B>D C>E F>D D>E E>F 其实当 ...

  4. nyoj349 poj1094 Sorting It All Out(拓扑排序)

    nyoj349   http://acm.nyist.net/JudgeOnline/problem.php?pid=349 poj1094   http://poj.org/problem?id=1 ...

  5. Sorting It All Out 拓扑排序+确定点

    这一道题的话  数据有一点问题    ........     例如 不过 还是   能理解一下  试试吧  ......... 3 5 A<B B<C C<A A<C B&l ...

  6. Poj 1094 拓扑排序Kahn

    Poj 1094 拓扑排序Kahn Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4133 ...

  7. POJ 1094 拓扑排序

    题意大坑,建议先看Discuss-- 否则代码写得就像以下的一团糟.... 其实并不难,拓扑排序+乱搞就可以AC. // by SiriusRen #include <cstdio> #i ...

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

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

  9. [POJ] 3687 Labeling Balls(拓扑排序)

    题目地址:http://poj.org/problem?id=3687 反向建边,即重的球指向轻的球,注意重边,然后拓扑排序.从n-->1循环,即每次从入度为0的球里面选编号大的存(包含输入的逻 ...

最新文章

  1. Core Text 学习笔记-基础
  2. 200多个js技巧代码(4)
  3. 《深入理解Java虚拟机》读书笔记七
  4. Flex4之与后台服务器通信方式:URLRequest+URLLoader【JAVA方式】
  5. 422器件与lvds接收器的区别_SPI、I2C、UART三种串行总线的原理、区别
  6. delphi刷新界面所选行丢失问题
  7. Android中AndFix使用
  8. FineReport帆软报表的安装
  9. 课设(房屋出租系统)
  10. Java-String类常用方法汇总
  11. 前端程序员专用的在线工具箱
  12. 微信小程序 —— 自定义picker选择器弹窗内容
  13. 荆棘遍地,鲜花满开(随笔二)
  14. 适合在企业网站展示企业发展历程的时间轴滑动特效源码
  15. linux泰语语言包,linux安装中文语言包(示例代码)
  16. new open SQL ABAP语法错误,逗号和转义符变量 when escaped, all host variables must be escaped using@
  17. 剑指XX游戏(七) - 不拼搏,枉少年
  18. 如何挑选视频会议摄像头?
  19. GO语言-自定义error
  20. 【实战演练】快速获取容器VIPs,解决集群自动化测试难题

热门文章

  1. mybatis反射的核心类
  2. flume案例-flume级联-配置文件编写
  3. Ribbon-1 Ribbon的基本使用
  4. 配置jvm堆最大内存eden区与s0或者s1区域比例
  5. 加密与安全 - Java加密与安全
  6. 天津市职高高一计算机试题及答案,职高(中职)数学(基础模块)上册题库.doc
  7. android datepicker使用方法,android中DatePicker和TimePicker的使用方法详解
  8. mysql触发器新元组_MySQL触发器-条件触发器语法
  9. c#如何使用反射去创建一个委托_【自学C#】|| 笔记 37 创建线程
  10. java 带点的字符串处理,关于android:java中字符串上带点的分割函数