http://poj.org/problem?id=1094

题意:该题题意明确,就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列。是典型的拓扑排序,但输出格式上确有三种形式:

1.该字母序列有序,并依次输出;

2.该序列不能判断是否有序

3.该序列字母次序之间有矛盾,即有环存在。

但是注意顺序,必须要先判断环,其次是无序,最后才是有序。

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std; #define Max 30 int gragh[Max][Max];
int ingreed[Max];
char ans[Max];
int n,m; int TopoSort()
{
int temp[Max],pos,flag = ,all = ; for(int i = ;i < n; i++)
temp[i] = ingreed[i]; for(int i = ;i < n; i++){
int num = ;
for(int j = ;j < n; j++){
if(temp[j] == ){
pos = j;
num++;
}
}
if(num == )
return ;
if(num > ) flag = -;
temp[pos] = -;
ans[all++] = pos;
for(int j = ;j < n; j++)
if(gragh[pos][j] == )
temp[j]--;
}
return flag;
} int main()
{
while(cin>>n>>m){
if(n == && m == )
break; memset(gragh,,sizeof(gragh));
memset(ingreed,,sizeof(ingreed)); char str[];
int sign = ;
for(int i = ;i <= m; i++){
cin>>str;
if(sign) continue;
gragh[str[] - 'A'][str[] - 'A'] = ;
ingreed[str[] - 'A']++; int s = TopoSort(); if(s == ){
printf("Inconsistency found after %d relations.\n",i);
sign = ;
} else if(s == ){
printf("Sorted sequence determined after %d relations: ",i);
for(int j = ;j < n; j++)
printf("%c",ans[j] + 'A' );
printf(".\n");
sign = ;
}
} if(!sign)
printf("Sorted sequence cannot be determined.\n");
}
return ;
}

POJ 1094 (TopoSort)的更多相关文章

  1. POJ 1094 (传递闭包 + 拓扑排序)

    题目链接: POJ 1094 题目大意:有 1 ~ N 个大写字母,且从 A 开始依次 N 个.再给你 M 个小于的关系,比如 A < B ,让你判断三种可能: 1.在第 i 个关系罗列之后,是 ...

  2. POJ题目(转)

    http://www.cnblogs.com/kuangbin/archive/2011/07/29/2120667.html 初期:一.基本算法:     (1)枚举. (poj1753,poj29 ...

  3. Repeater POJ - 3768 (分形)

    Repeater POJ - 3768 Harmony is indispensible in our daily life and no one can live without it----may ...

  4. Booksort POJ - 3460 (IDA*)

    Description The Leiden University Library has millions of books. When a student wants to borrow a ce ...

  5. Radar Installation POJ - 1328(贪心)

    Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. ...

  6. Best Cow Fences POJ - 2018 (二分)

    Farmer John's farm consists of a long row of N (1 <= N <= 100,000)fields. Each field contains ...

  7. E - The Balance POJ - 2142 (欧几里德)

    题意:有两种砝码m1, m2和一个物体G,m1的个数x1,  m2的个数为x2, 问令x1+x2最小,并且将天平保持平衡 !输出  x1 和 x2 题解:这是欧几里德拓展的一个应用,欧几里德求不定方程 ...

  8. poj 1094(拓扑排序)

    http://poj.org/problem?id=1094 题意:给你m个字母,有n个判断语句.求在哪个语句就可以判断出这个是不是一个环,或者在哪个语句可以判断出这些字母的排序规则,或者就是不能确定 ...

  9. poj 1220(短除法)

    http://poj.org/problem?id=1220 题意:进制转换,把a进制转换为b进制. 如果数据不大的话,那么这个题还是很简单的,但这个题就是数据范围太大,所以这里可以采用短除法来做. ...

随机推荐

  1. (转) linux之sort用法

    sort命令是帮我们依据不同的数据类型进行排序,其语法及常用参数格式: sort [-bcfMnrtk][源文件][-o 输出文件] 补充说明:sort可针对文本文件的内容,以行为单位来排序. 参 数 ...

  2. thinkphp下载远程图片到本地

    $url="http://www.test.com/test.jpg";//图片远程地址 $local="./Upload/test/test.jpg";//下 ...

  3. 1057 N的阶乘(大数运算)

    题目链接:51nod 1057 N的阶乘 #include<cstdio> using namespace std; typedef long long ll; ; const int m ...

  4. 基于双向链表的增删改查和排序(C++实现)

    双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱.所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点.一般我们都构造双向循环链表 ...

  5. 向下滚动页面加载图片的js

    js代码 scroll.photo.js : window.imgscroll = { options: { target: null, //插入图片的目标位置 img_list: null, //图 ...

  6. Leetcode 1 two sum 难度:0

    https://leetcode.com/problems/two-sum/ class Solution { public: vector<int> twoSum(vector<i ...

  7. node

    docker run -d --name data -v c:/jayruanwork/app:/usr/share/app busybox || windows c:\jayruanwork\app ...

  8. JavaScript 语句

    JavaScript 语句 JavaScript 语句向浏览器发出的命令.语句的作用是告诉浏览器该做什么. JavaScript 语句 JavaScript 语句是发给浏览器的命令. 这些命令的作用是 ...

  9. 【59测试】【树】【dp】

    第一题 A : 这棵树由n个节点以及n - 1条有向边构成,每条边都从父亲节点指向儿子节点,保证除了根节点以外的每个节点都有一个唯一的父亲.树上的节点从1到n标号.该树的一棵子树的定义为某个节点以及从 ...

  10. C# 跨线程操作无效

    提示此错误的原因就是控件由主线程创建,在另一个线程进行操作时就会被阻止,防止数据间随意篡改. 如果一定要跨线程作业,如进度条或状态显示等,基本有三种方法解决: 1.Control.CheckForIl ...