Sorting It All Out
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions:39731   Accepted: 13975

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.

Source

题意:

给定关于n个字母的对应优先级关系,问是否可以根据优先级对他们进行排序。并且要求输出是在第几组可以得出结果。

会有优先级不一致和无法排序的情况。

思路:

感觉这题数据很迷啊,m的范围都不给我都不好估计复杂度。

以及,要注意输出时候的句号。特别是可以sort的时候。

是一道传递闭包的问题,用邻接矩阵建图,如果$A<B$,则表示$A$到$B$有一条有向边,$g['A']['B'] = 1$

每次添加一个关系,使用一次floyd计算能否得出结果,或者是否出现不一致。

输出排序结果的时候,其实只需要比较每个点的入度,按照入度从大到小排序就行了。

因为最后的矩阵是一个传递闭包,最小的那个字母肯定小于其他所有字母,也就是说他这一行会有$n-1$个$1$

 #include<iostream>
//#include<bits/stdc++.h>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<climits>
using namespace std;
typedef long long LL;
#define N 100010
#define pi 3.1415926535
#define inf 0x3f3f3f3f int n, m;
int g[][], tmpg[][]; int floyd()
{
for(int i = ; i < n; i++){
for(int j = ; j < n; j++){
tmpg[i][j] = g[i][j];
}
}
for(int k = ; k < n; k++){
for(int i = ; i < n; i++){
for(int j = ; j < n; j++){
tmpg[i][j] |= tmpg[i][k] & tmpg[k][j];
if(tmpg[i][j] == tmpg[j][i] && tmpg[i][j] == && i != j){
return -;
}
}
}
}
for(int i = ; i < n; i++){
for(int j = ; j < n; j++){
if(tmpg[i][j] == tmpg[j][i] && tmpg[i][j] == && i != j){
return ;
}
}
}
return ;
} struct node{
int deg;
char ch;
};
bool cmp(node a, node b)
{
return a.deg > b.deg;
} void print()
{
//int deg[30];
//memset(deg, 0, sizeof(deg));
node character[];
for(int i = ; i < n; i++){
character[i].deg = ;
character[i].ch = 'A' + i;
}
for(int i = ; i < n; i++){
for(int j = ; j < n; j++){
if(tmpg[i][j]){
character[i].deg++;
}
}
}
sort(character, character + n, cmp);
for(int i = ; i < n; i++){
printf("%c", character[i].ch);
}
printf(".\n"); /*queue<int>que;
for(int i = 0; i < n; i++){
printf("%d\n", deg[i]);
if(!deg[i]){
que.push(i);
break;
}
}
for(int i = 0; i < n; i++){
int x = que.front();que.pop();
printf("%c", x + 'A');
for(int k = 0; k < n; k++){
if(tmpg[k][x])deg[k]--;
if(!deg[k])que.push(k);
}
}*/
} int main()
{
while(scanf("%d%d", &n, &m) != EOF && n || m){
memset(g, , sizeof(g));
/*for(int i = 0; i < n; i++){
g[i][i] = 1;
}*/ int i;
bool dont = false;
for(i = ; i <= m; i++){
char a, b;
getchar();
scanf("%c<%c", &a, &b);
g[a - 'A'][b - 'A'] = ;
if(!dont){
int flag = floyd();
if(flag == -){
printf("Inconsistency found after %d relations.\n", i);
dont = true;
}
else if(flag == ){
printf("Sorted sequence determined after %d relations: ", i);
print();
dont = true;
}
}
}
if(i > m && !dont){
printf("Sorted sequence cannot be determined.\n");
}
}
return ;
}

poj1094 Sorting It All Out【floyd】【传递闭包】【拓扑序】的更多相关文章

  1. POJ3660:Cow Contest(Floyd传递闭包)

    Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16941   Accepted: 9447 题目链接 ...

  2. UVA 247 电话圈 (floyd传递闭包 + dfs输出连通分量的点)

    题意:输出所有的环: 思路:数据比较小,用三层循环的floyd传递闭包(即两条路通为1,不通为0,如果在一个环中,环中的所有点能互相连通),输出路径用dfs,递归还没有出现过的点(vis),输出并递归 ...

  3. UVA 753 UNIX 插头(EK网络流+Floyd传递闭包)

    UNIX 插头 紫书P374 [题目链接]UNIX 插头 [题目类型]EK网络流+Floyd传递闭包 &题解: 看了书之后有那么一点懂了,但当看了刘汝佳代码后就完全明白了,感觉他代码写的好牛逼 ...

  4. UVA 247 电话圈(Floyd传递闭包+输出连通分量)

    电话圈 紫书P365 [题目链接]电话圈 [题目类型]Floyd传递闭包+输出连通分量 &题解: 原来floyd还可以这么用,再配合连通分量,简直牛逼. 我发现其实求联通分量也不难,就是for ...

  5. POJ 3660 Cow ContestCow(Floyd传递闭包)题解

    题意:给出m个关系,问你能确定机头牛的排名 思路:要确定排名那必须要把他和其他n-1头牛比过才行,所以Floyd传递闭包,如果赢的+输的有n-1就能确定排名. 代码: #include<cstd ...

  6. nyoj 211——Cow Contest——————【floyd传递闭包】

    Cow Contest 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 N (1 ≤ N ≤ 100) cows, conveniently numbered 1.. ...

  7. POJ 2594 —— Treasure Exploration——————【最小路径覆盖、可重点、floyd传递闭包】

    Treasure Exploration Time Limit:6000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64 ...

  8. POJ 3660—— Cow Contest——————【Floyd传递闭包】

    Cow Contest Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit  ...

  9. BZOJ 1612 [Usaco2008 Jan]Cow Contest奶牛的比赛:floyd传递闭包

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1612 题意: 有n头牛比赛. 告诉你m组(a,b),表示牛a成绩比牛b高. 保证排名没有并 ...

  10. POJ-2594 Treasure Exploration floyd传递闭包+最小路径覆盖,nice!

    Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 8130   Accepted: 3 ...

随机推荐

  1. Spark GraphX实例(2)

    5. 图的转换操作 图的转换操作主要有以下的方法: (1) Graph.mapVertices():对图的顶点进行转换,返回一张新图: (2) Graph.mapEdges():对图的边进行转换,返回 ...

  2. IIS 之 应用程序池

    IIS(Internet Information Services),由于我使用的是Windows10系统,所以本文以其内置 10.0.14393.0 版本说明. 应用程序池 → 右键(待设置应用程序 ...

  3. IDEA使用笔记(六)——设置项目的JDK配置

    1:由于dev分支和master分支的代码差异比较多,所以,就从master上分出一个新的分支dev_,于是我就克隆新的代码,打开对应的项目文件,然后启动试试,发现报出如下的错误,很明显是因为没有制定 ...

  4. SNF快速开发平台MVC-各种级联绑定方式,演示样例程序(包含表单和表格控件)

    做了这么多项目,经常会使用到级联.联动的情况. 如:省.市.县.区.一级分类.二级分类.三级分类.仓库.货位. 方式:有表单需要做级联的,还是表格行上需要做级联操作的. 实现:实现方法也有很多种方式. ...

  5. Ansible 使用普通用户远程执行playbook

    设置ansible使用普通用户jsxge远程连接执行playbook 1. ansible控制端创建普通用户jsxgecd /homeuseradd jsxgechown -R jsxge.wheel ...

  6. [转]关于ios 推送功能的终极解决

    刚刚做了一个使用推送功能的应用 遇到了一些问题整的很郁闷 搞了两天总算是弄明白了 特此分享给大家 本帖 主要是针对产品发布版本的一些问题 综合了网上一些资料根据自己实践写的 不过测试也可以看看 首先要 ...

  7. 译:7.使用Spring MVC服务Web内容

    本指南向您介绍了使用Spring创建“hello world”网站的过程.阅读原文:Serving Web Content with Spring MVC 1. 你将会构建什么? 您将构建一个具有静态 ...

  8. [svc]打通mysql主从同步

    MySQL数据库设置主从同步 主从同步的特点: 逻辑的 异步的 主从同步的作用 1.备份 2.读写分离 主从同步的步骤 准备环境 1, mysql版本一致 5.5以上 2, 主从数据库一致 主库mas ...

  9. 清除win下连接的linux的samba服务缓存 用户名和密码

    1:cmd 2:在停止查看共享的情况下执行:net use * /del 删除所有 或根据列表,一个个删除连接: net use 远程连接名称 /del

  10. Redis 的 5 种数据结构

    1.string 可以是字符串,整数或者浮点数,对整个字符串或者字符串中的一部分执行操作,对整个整数或者浮点执行自增(increment)或者自减(decrement)操作. 字符串命令: ①get: ...