POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 39602 | Accepted: 13944 |
Description
Input
Output
Sorted sequence determined after xxx relations: yyy...y.
Sorted sequence cannot be determined.
Inconsistency found after xxx relations.
where xxx is the number of relations processed at the time either a sorted sequence is determined or an inconsistency is found, whichever comes first, and yyy...y is the sorted, ascending sequence.
Sample Input
4 6
A<B
A<C
B<C
C<D
B<D
A<B
3 2
A<B
B<A
26 1
A<Z
0 0
Sample Output
Sorted sequence determined after 4 relations: ABCD.
Inconsistency found after 2 relations.
Sorted sequence cannot be determined.
Source
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<set>
#include<map>
#include<list>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long LL;
int mon1[]= {,,,,,,,,,,,,};
int mon2[]= {,,,,,,,,,,,,};
int dir[][]= {{,},{,-},{,},{-,}}; int getval()
{
int ret();
char c;
while((c=getchar())==' '||c=='\n'||c=='\r');
ret=c-'';
while((c=getchar())!=' '&&c!='\n'&&c!='\r')
ret=ret*+c-'';
return ret;
} #define max_v 55
int indgree[max_v];
int temp[max_v];
int G[max_v][max_v];
int tp[max_v];
int n,m;
queue<int> q;
int tpsort()
{
while(!q.empty())
q.pop();
for(int i=;i<=n;i++)
{
indgree[i]=temp[i];
if(indgree[i]==)
q.push(i);
} int c=,p;
int flag=;
while(!q.empty())
{
if(q.size()>)
flag=;
p=q.front();
q.pop();
tp[++c]=p;
for(int i=;i<=n;i++)
{
if(G[p][i])
{
indgree[i]--;
if(indgree[i]==)
q.push(i);
}
}
}
/*
拓扑完之后,存在没有入队的点,那么该点就一定是环上的
*/
if(c!=n)//存在环
return ;
else if(flag)//能拓扑但存在多条路
return ;
return -;//能拓扑且存在唯一拓扑路径
}
int main()
{
int x,y;
char c1,c2;
while(~scanf("%d %d",&n,&m))
{
if(n==&&m==)
break;
memset(G,,sizeof(G));
memset(temp,,sizeof(temp));
memset(tp,,sizeof(tp));
int flag1=,index1=;//是否有环及环的位置
int flag2=,index2=;//能否拓扑和拓扑的位置
for(int i=;i<=m;i++)
{
getchar();
scanf("%c<%c",&c1,&c2);
x=c1-'A'+;
y=c2-'A'+;
if(flag1==&&flag2==)
{
if(G[y][x])//环的一种情况
{
flag1=;
index1=i;
continue;
}
if(G[x][y]==)//预防重边
{
G[x][y]=;
temp[y]++;
}
int k=tpsort();
if(k==)//存在环
{
flag1=;
index1=i;
continue;
}else if(k==-)//存在唯一拓扑路径
{
flag2=;
index2=i;
}
}
}
if(flag1==&&flag2==)
{
printf("Sorted sequence cannot be determined.\n");
}else if(flag1)
{
printf("Inconsistency found after %d relations.\n",index1);
}else if(flag2)
{
printf("Sorted sequence determined after %d relations: ",index2);
for(int i=;i<=n;i++)
{
printf("%c",tp[i]+'A'-);
}
printf(".\n");//!!!注意还有个点...
}
}
return ;
}
POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)的更多相关文章
- Legal or Not(拓扑排序判环)
http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Time Limit: 2000/1000 MS (Java/Others) ...
- LightOJ1003---Drunk(拓扑排序判环)
One of my friends is always drunk. So, sometimes I get a bit confused whether he is drunk or not. So ...
- HDU1811 拓扑排序判环+并查集
HDU Rank of Tetris 题目:http://acm.hdu.edu.cn/showproblem.php?pid=1811 题意:中文问题就不解释题意了. 这道题其实就是一个拓扑排序判圈 ...
- [bzoj3012][luogu3065][USACO12DEC][第一!First!] (trie+拓扑排序判环)
题目描述 Bessie has been playing with strings again. She found that by changing the order of the alphabe ...
- Almost Acyclic Graph CodeForces - 915D (思维+拓扑排序判环)
Almost Acyclic Graph CodeForces - 915D time limit per test 1 second memory limit per test 256 megaby ...
- ACM: poj 1094 Sorting It All Out - 拓扑排序
poj 1094 Sorting It All Out Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld & ...
- [ACM] POJ 1094 Sorting It All Out (拓扑排序)
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26801 Accepted: 92 ...
- poj 1094 Sorting It All Out 拓补排序
Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...
- HDU 3342 Legal or Not(有向图判环 拓扑排序)
Legal or Not Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
随机推荐
- POJ1222(SummerTrainingDay01-E)
EXTENDED LIGHTS OUT Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11078 Accepted: 7 ...
- Flask中request参数
首先要明确一件事,Request这是个对象,不管使用PHP还是python还是什么java语言,虽然request这个对象可能叫的名字不一样,(在其他语言中可能叫什么HttpRequest),但是原理 ...
- python-中介者模式
源码地址:https://github.com/weilanhanf/PythonDesignPatterns 说明: 面向对象设计中鼓励将行为分不到各个对象中,这种分布可能会导致对象间有许多连接.最 ...
- AJAX通过HTML请求C#一般处理程序
AJAX通过HTML请求C#一般处理程序 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"&g ...
- PDO中的预处理
PDO中的基本的原理和步骤和MySQL中的预处理都是一样的,只不过就是把MySQL中的预处理所有命令行的语法封装成了PDO对象的几个公开的方法而已! 1.发送预处理语句 此时,我们需要调用pdo对象的 ...
- ubuntu下给raspy pi2 编译qt5库
交叉编译时,通常要准备mkspec, 参考: http://wiki.qt.io/Building_Qt_for_Embedded_Linux mkspec包含两个文件: qmake.conf- Th ...
- winform 关于Messagebox自动定时关闭
添加一个类库MessageBoxTimeOut public class MessageBoxTimeOut { private string _caption; public void Show(s ...
- 转:jQuery插件开发全解析
jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery命名空间的函数,另一种是对象级 ...
- 几种模型文件(CDM、LDM、PDM、OOM、BPM)
概念数据模型 (CDM): 帮助你分析信息系统的概念结构,识别主要实体.实体的属性及实体之间的联系.概念数据模型(CDM)比逻辑数据模型 (LDM)和物理数据模型(PDM)抽象.CDM 表现数据库的全 ...
- Hadoop 高可用(HA)的自动容灾配置
参考链接 Hadoop 完全分布式安装 ZooKeeper 集群的安装部署 0. 说明 在 Hadoop 完全分布式安装 & ZooKeeper 集群的安装部署的基础之上进行 Hadoop 高 ...