Sorting It All Out
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 30110   Accepted: 10411

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. 题意:这题题意好蛋疼啊!!!一个一个给边,一个一个的判断,先判断是否有环。然后再判断是否确定序列。最后前两者都不满足,则此序列不确定。
收获:1.知道只有有环时才k<n。2.入度为0的点不唯一,则此序列不确定。3.入度很重要。
下面是能AC代码,但是 0 0会有问题,不知道为什么?还请各位指教。
#include <cstdio>
#include <iostream>
//#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std; const int INF=0x3f3f3f3f;
const double eps=1e-;
const double PI=acos(-1.0); const int maxn=;
struct Edge
{
int u, v, next;
};
Edge edge[maxn];
int head[maxn];
int num, n,m;
void init_edge()
{
num = ;
memset(head, -, sizeof(head));
}
void addedge(int u, int v)
{
edge[num].u = u;
edge[num].v = v;
edge[num].next = head[u];
head[u] = num++;
}
int ans;
int topo[maxn];
int in[maxn];
int topsort()
{
queue<int> q;
int indeg[];
for(int i = ; i < +n; i++)
{
indeg[i] = in[i];
if(indeg[i] == ) q.push(i);
}
int k = ;
int flag=;
while(!q.empty())
{
if(q.size()>)
flag = ;
int u = q.front();
q.pop();
topo[k++] = u;
for(int i = head[u]; i != -; i = edge[i].next)
{
int v = edge[i].v;
indeg[v]--;
if(indeg[v]==)
q.push(v);
}
}
if(k < n)
return ;
if(flag)
return -;
else
return ;
}
char s[];
int main()
{
while(~scanf("%d%d", &n, &m))
{
if(n== && m==)
break;
init_edge();
memset(in, , sizeof(in));
int flag2 = ;
int flag3 = ;
for(int j = ; j <= m; j++)
{
scanf("%s",s);
if(!flag2&&!flag3)
{
in[s[]]++;
addedge(s[],s[]);
int res = topsort();
if(res == )
{
printf("Inconsistency found after %d relations.\n", j);
flag2 = ;
}
if(res == )
{
printf("Sorted sequence determined after %d relations: ", j);
for(int i=; i < n; i++)
printf("%c", topo[i]);
printf(".\n");
flag3 = ;
}
}
}
if(!flag2&&!flag3)
puts("Sorted sequence cannot be determined."); //puts("QAQ");
}
return ;
}

POJ1094 Sorting It All Out(拓扑排序)的更多相关文章

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

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

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

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

  3. POJ1094 Sorting It All Out —— 拓扑排序

    题目链接:http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Tot ...

  4. ACM: poj 1094 Sorting It All Out - 拓扑排序

    poj 1094 Sorting It All Out Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & ...

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

    http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  6. POJ- 1094 Sorting It All Out---拓扑排序是否唯一的判断

    题目链接: https://vjudge.net/problem/POJ-1094 题目大意: 该题题意明确,就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列.是典型的拓扑排序,但输出格式上 ...

  7. POJ1094 Sorting It All Out LUOGU 排序

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

  8. POJ 1094:Sorting It All Out拓扑排序之我在这里挖了一个大大的坑

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

  9. [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 ...

  10. 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 ...

随机推荐

  1. 可持久化Trie树

    代码 ; struct PerTrie { ][ChSize]; ]; void init() { memset(next[],,])); inf[]=; id=; } int GetId(char ...

  2. jquery 弹出框 showDialog.js具体用法

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABWwAAAImCAIAAABID1T7AAAgAElEQVR4nO3d329c52Hgff1HvPCNLw

  3. WPF ICommand 用法

    基础类,继承与ICommand接口 using System; using System.Collections.Generic; using System.Linq; using System.Te ...

  4. POJ 3114 Countries in War(强连通+最短路)

    POJ 3114 Countries in War 题目链接 题意:给定一个有向图.强连通分支内传送不须要花费,其它有一定花费.每次询问两点的最小花费 思路:强连通缩点后求最短路就可以 代码: #in ...

  5. 传输中文乱码js解决方法

    encodeURI要编码两次 var a="我的"; //编译两次 //window.location.href = "http://127.0.0.1:8080/kab ...

  6. Java内存区域和GC机制篇

    Java内存区域和GC机制一.目录 1.Java垃圾回收概括 2.Java内存区域 3.Java对象的访问方式 4.Java内存访问机制 5.Java GC 机制 6.Java垃圾收集器 二.Java ...

  7. CentOs7下systemd管理知识要点

    centOs7的一个巨大的变动就是用systemd取代了原来的System V init.systemd是一个完整的软件包,安装完成后有很多物理文件组成,大致分布为,配置文件位于/etc/system ...

  8. css-布局1-基本属性

    <!DOCTYPE html>CSS4-布局1-基本属性 属性:displayvisibilityfloatclear HTML元素类型:行内元素,块级元素 块级元素:最大的区别:换行行内 ...

  9. 前端判断用户请求是PC还是移动端

    链接:https://www.zhihu.com/question/20004700/answer/13678113 第一步先在服务器端使用User Agent判断,先匹配出移动设备,这一步可以统计U ...

  10. [Python]打开文件的模式

    Python中以sys.open()方法打开文件 import sys file = open("D:\\file.txt") 其中可在第二个参数的位置指定打开文件的模式 impo ...