Sorting It All Out
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 37764   Accepted: 13327

Description

An ascending sorted sequence of distinct values is one in which some form of a less-than operator is used to order the elements from smallest to largest. For example, the sorted sequence A, B, C, D implies that A < B, B < C and C < D. in this problem, we will give you a set of relations of the form A < B and ask you to determine whether a sorted order has been specified or not.

Input

Input consists of multiple problem instances. Each instance starts with a line containing two positive integers n and m. the first value indicated the number of objects to sort, where 2 <= n <= 26. The objects to be sorted will be the first n characters of the uppercase alphabet. The second value m indicates the number of relations of the form A < B which will be given in this problem instance. Next will be m lines, each containing one such relation consisting of three characters: an uppercase letter, the character "<" and a second uppercase letter. No letter will be outside the range of the first n letters of the alphabet. Values of n = m = 0 indicate end of input.

Output

For each problem instance, output consists of one line. This line should be one of the following three: 

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.

题目链接:点击打开链接

题目大意:给你一堆A<B这样的顺序,进行拓扑,判断到第几次满足条件了就输出,不需要管后面的东西。要么顺序确定,要么顺序冲突,要么结尾了还不确定。

题目思路:首先理解两个概念,入度和出度,入度就是插入这个点的边的数量(指向这个点),出度就是这个点出去的边的数量。对于一个确定了顺序的序列,每一个节点的出度入度之和肯定为n-1,(自己想一下),那这道题就每次输入边的时候对出度入度进行操作就可以了,具体看代码,比较详细。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<math.h>
#include<cmath>
#include<time.h>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<algorithm>
#include<numeric>
using namespace std;
typedef long long ll;
const int maxn=30;
int g[maxn][maxn];
char s[10];
struct peo{
int in,ou;// 存放每一个节点的出度和入度
char id;
}aa[40];
bool cmp(peo a,peo b){
return a.ou>b.ou;
}
int main(){
int n,m;
while(scanf("%d%d",&n,&m),n&&m){
memset(g,0,sizeof(g));
for(int i=1;i<=n;i++){
aa[i].in=0;
aa[i].ou=0;
aa[i].id='A'+i-1;
}
int to,flag=1;//flag为1 表示还没有输出过
for(to=1;to<=m;to++){
scanf("%s",s);
if(!flag)continue;
int a=s[0]-'A'+1,b=s[2]-'A'+1;//将字符转化为数字 更简单
if(g[b][a]||a==b){//如果存在环 或者输入的是A<A
printf("Inconsistency found after %d relations.\n",to);
flag=0;
}
if(g[a][b])continue;//如果关系已经存在 就不用再操作
g[a][b]=1;//建立关系
aa[a].ou++;//节点出度入度操作
aa[b].in++;
for(int i=1;i<=n;i++){
if(g[i][a]){// 存在i-a 肯定存在 a-b i出度++ b入度++
if(g[i][b])continue;//如果已经存在关系 说明之前操作过了 continue
g[i][b]=1;
aa[i].ou++;
aa[b].in++;
}
if(g[b][i]){// 与上同理
if(g[a][i])continue;
g[a][i]=1;
aa[i].in++;
aa[a].ou++;
}
}
int f=1;
for(int i=1;i<=n;i++){
if(aa[i].in+aa[i].ou!=n-1){//每一个节点 出度入度之和为n-1 即整个序列关系确定
f=0;
break;
}
}
if(f){
flag=0;
sort(aa+1,aa+1+n,cmp);//按照出度进行排序
printf("Sorted sequence determined after %d relations: ",to);
for(int i=1;i<=n;i++){
printf("%c",aa[i].id);
}
printf(".\n");
}
}
if(flag)printf("Sorted sequence cannot be determined.\n");//如果flag还是1 说明之前没有输出 顺序不确定
}
}

poj1094 拓扑排序(出度入度简单使用)的更多相关文章

  1. POJ1094 拓扑排序

    问题:POJ1094   本题考查拓扑排序算法   拓扑排序:   1)找到入度为0的点,加入已排序列表末尾: 2)删除该点,更新入度数组.   循环1)2)直到 1. 所有点都被删除,则找到一个拓扑 ...

  2. POJ1094——拓扑排序和它的唯一性

    比较模板的topological-sort题,关键在于每个元素都严格存在唯一的大小关系,而一般的拓扑排序只给出一个可能解,这就需要每趟排序的过程中监视它是不是总坚持一条唯一的路径. 算法导论里面的拓扑 ...

  3. poj1094拓扑排序

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29539   Accepted: 10 ...

  4. PAT A1146 Topological Order (25 分)——拓扑排序,入度

    This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...

  5. hdoj1285 拓扑排序

    题目链接 分析: 很明显,一看就是拓扑排序. 看似简单, 暗藏武器啊. 第一次做的时候一边拓扑排序一边标记他们的深度, 例如题中给的例子 {1 2:2 3:4 3 }.1的深度为1. 2.4的深度为2 ...

  6. 使用 C# 代码实现拓扑排序

    0.参考资料 尊重他人的劳动成果,贴上参考的资料地址,本文仅作学习记录之用. https://www.codeproject.com/Articles/869059/Topological-sorti ...

  7. [C#]使用 C# 代码实现拓扑排序 dotNet Core WEB程序使用 Nginx反向代理 C#里面获得应用程序的当前路径 关于Nginx设置端口号,在Asp.net 获取不到的,解决办法 .Net程序员 初学Ubuntu ,配置Nignix 夜深了,写了个JQuery的省市区三级级联效果

    [C#]使用 C# 代码实现拓扑排序   目录 0.参考资料 1.介绍 2.原理 3.实现 4.深度优先搜索实现 回到顶部 0.参考资料 尊重他人的劳动成果,贴上参考的资料地址,本文仅作学习记录之用. ...

  8. 牛客网NOIP赛前集训营-提高组(第六场)-A-最长路[拓扑排序+hash+倍增]

    题意 给定一个 \(n\) 点 \(m\) 边的边权非负的有向图,边有字符,求以每个点为开头的最长路字典序最小的路径 \(hash\) 值. \(n,m\leq 10^6\) 分析 首先建反图拓扑排序 ...

  9. [poj1094]Sorting It All Out_拓扑排序

    Sorting It All Out poj-1094 题目大意:给出一些字符串之间的大小关系,问能否得到一个唯一的字符串序列,满足权值随下标递增. 注释:最多26个字母,均为大写. 想法:显然,很容 ...

随机推荐

  1. EF CODEFIRST WITH ORACLE

    摸索了半天,运行通过了,但是还是有一点坑的,对于初次使用的人来说,可能会遇到几个问题 首先安装两个dll 如果你已经下载好了dll Oracle.ManagedDataAccess.dll Oracl ...

  2. .Net时间运算 - DateTime类,TimeSpan类

    DateTime类是.Net中用于处理时间类型数据的. 一.字段 MaxValue 表示 DateTime 的最大可能值.此字段为只读. MinValue     表示 DateTime 的最小可能值 ...

  3. NLTK词性标注解释

    1.      CC      Coordinating conjunction 连接词2.     CD     Cardinal number  基数词3.     DT     Determin ...

  4. Go语言-变量和常量

    我们在这里需要优先说明的是用于声明变量的关键字var,以及用于声明常量的关键字const.要知道,绝大多数的数据类型的值都可以被赋给一个变量,包括函数.而常量则不同,它只能被赋予基本数据类型的值本身. ...

  5. 3、PACBIO下机数据如何看

    转载:http://www.cnblogs.com/jinhh/p/8328818.html 三代测序的下机数据都有哪些,以及他们具体的格式是怎么样的(以sequel 平台为主). 测序过程 SMRT ...

  6. 异常日志记录 DDLog

    项目中如果想把异常捕获再写入文件,有个十分容易使用的库DDLog. 首先导入库,在git上下载. 一:在项目初始化指定全局LogLeve ,一般在xxxapp.m中 staticconstint dd ...

  7. map集合的应用

    分析以下需求,并用代码实现: (1)利用键盘录入,输入一个字符串 (2)统计该字符串中各个字符的数量例如 用户输入字符串"If~you-want~to~change-your_fate_I_ ...

  8. [学习笔记]scanf弊端以及解决方案

    #include<stdio.h> #include<stdlib.h> #include<unistd.h> int main(void) { ]; //mems ...

  9. [raspberry pi3] hadoop 编译搭建和配置

    Causion: 只有一个raspberry pi3的就随便玩玩吧,瓶颈不在在cpu, 1 G的内存实在是太少了,跑个hadoop就很辛苦了 下面是瞎折腾的过程: oracle的arm jdk的安装过 ...

  10. c#链接postgresql

    1.首先需要添加2个dll文件:Mono.Security.dll; Npgsql.dll. 2.配置文件增加 <appSettings> <!--<add key=" ...