PKU 1094 Sorting It All Out(拓扑排序)
题目大意:就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列。
是典型的拓扑排序,但输出格式上确有三种形式:
1.该字母序列有序,并依次输出;
2.判断该序列是否唯一;
3.该序列字母次序之间是否有矛盾,即是否有环存在;
而这三种形式的判断是有顺序的:先判断(3)是否有环,再判断是否有序(1),最后才能判断是否能得出结果(2)。注意:对于(2)必须遍历完整个图,而(1)和(3)一旦得出结果,对后面的输入就不用做处理了。
#include<cstdio>
#include<cstring>
char alp[];
int edge[][],in[];
int TopoSort(int n)
{
int t=,tmp[],flag=;
for(int i=;i<=n;i++)
tmp[i]=in[i];
for(int i=;i<=n;i++){
int in0=,loc;//查找入度为零的顶点个数
for(int j=;j<=n;j++){
if(tmp[j]==){
in0++;
loc=j;
}
}
if(in0==) return ;//有环,那么后面的就不用再判断了
if(in0>) flag=-;//不确定,但不能直接return -1;因为后面的字母中还可能形成环而return 0;
alp[t++]='A'+loc-;//入度为零的字母入队
tmp[loc]=-;
for(int k=;k<=n;k++)
if(edge[loc][k]==)
tmp[k]--;
}
return flag;
}
//flag=1:有序 flag=-1:不确定
int main()
{
int m,n,x,y;
char str[];
while(scanf("%d%d",&n,&m),n|m){
memset(edge,,sizeof(edge));
memset(in,,sizeof(in));
bool sign=;
for(int i=;i<=m;i++){
scanf("%s",str);
if(sign) continue; //一旦得出结果,对后续的输入不做处理,但是不能break,因为还得继续输入
x=str[]-'A'+;
y=str[]-'A'+;
edge[x][y]=;
in[y]++;
int flag=TopoSort(n);
if(flag==){//有环
printf("Inconsistency found after %d relations.\n",i);
sign=;
}
if(flag==){//有序
printf("Sorted sequence determined after %d relations: ",i);
for(int j=;j<n;j++)
printf("%c",alp[j]);
printf(".\n");
sign=;
}//当sign=1时表明已得出结果
}
if(!sign) //不确定
printf("Sorted sequence cannot be determined.\n");
}
return ;
}
PKU 1094 Sorting It All Out(拓扑排序)的更多相关文章
- ACM: poj 1094 Sorting It All Out - 拓扑排序
poj 1094 Sorting It All Out Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld & ...
- poj 1094 Sorting It All Out (拓扑排序)
http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- [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 (拓扑排序) - from lanshui_Yang
Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...
- poj 1094 Sorting It All Out_拓扑排序
题意:是否唯一确定顺序,根据情况输出 #include <iostream> #include<cstdio> #include<cstring> #include ...
- POJ 1094 Sorting It All Out 拓扑排序 难度:0
http://poj.org/problem?id=1094 #include <cstdio> #include <cstring> #include <vector& ...
- POJ 1094:Sorting It All Out拓扑排序之我在这里挖了一个大大的坑
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 29984 Accepted: 10 ...
- POJ1094 Sorting It All Out —— 拓扑排序
题目链接:http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Tot ...
- nyoj349 poj1094 Sorting It All Out(拓扑排序)
nyoj349 http://acm.nyist.net/JudgeOnline/problem.php?pid=349poj1094 http://poj.org/problem?id=10 ...
随机推荐
- push an existing repository from the command line
git remote add origin https://github.com/gaoconggit/LandMVC.git git push -u origin master
- <LeetCode OJ> 217./219. Contains Duplicate (I / II)
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- char[]与TCHAR[]互相转换引发的一个问题!
软件的一个驱动由于开发的年代比较久一些,使用的是非Unicode编码,而当前新的软件使用的是Unicode编码,于是将非Unicode驱动用于Unicode软件上时,就出现了问题! 问题就出现在非 ...
- C++之运算符重载
C++ Code 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 ...
- AWS系列-EC2默认限制说明
Amazon EC2 提供您可以使用的不同资源,例如实例和卷. 在您创建 AWS 账户时,AWS 会针对每个区域中的这些资源设置限制.此页面列出您在 亚太区域 (东京) 中的 EC2 服务限制. 1. ...
- Android 数据存储(XML解析)
在androd手机中处理xml数据时很常见的事情,通常在不同平台传输数据的时候,我们就可能使用xml,xml是与平台无关的特性,被广泛运用于数据通信中,那么在android中如何解析xml文件数据 ...
- std__vector介绍
vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单地说,vec ...
- python基础之3
1,列表可以嵌套任何东西.包括字典,列表等 字典是无序的健值型,不需要下标,也可以嵌套列表和字典 2,集合:对列表进行差异化处理后形成集合,特点:去重和无序.主要作用: (1)去重;(2) 关系测试, ...
- struts2中s:iterator 标签的使用详解 及 OGNL用法
简单的demo: s:iterator 标签有3个属性:value:被迭代的集合id :指定集合里面的元素的idstatus 迭代元素的索引 1:jsp页面定义元素写法 数组或list <s ...
- 学习ASP.NET MVC3(6)----- Filte
前言 在开发大项目的时候总会有相关的AOP面向切面编程的组件,而MVC(特指:Asp.Net MVC,以下皆同)项目中不想让MVC开发人员去关心和写类似身份验证,日志,异常,行为截取等这部分重复的代码 ...