poj1094 Sorting It All Out【floyd】【传递闭包】【拓扑序】

时间:2022-05-01 06:44:41
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&colon;Cow Contest(Floyd传递闭包)

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

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

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

  3. UVA 753 UNIX 插头&lpar;EK网络流&plus;Floyd传递闭包&rpar;

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

  4. UVA 247 &Tab;电话圈&lpar;Floyd传递闭包&plus;输出连通分量&rpar;

    电话圈 紫书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 &lbrack;Usaco2008 Jan&rsqb;Cow Contest奶牛的比赛:floyd传递闭包

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

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

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

随机推荐

  1. Online Judge(OJ)搭建(第一版)

    搭建 OJ 需要的知识(重要性排序): Java SE(Basic Knowledge, String, FileWriter, JavaCompiler, URLClassLoader, Secur ...

  2. 11-UIView与核心动画对比

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  3. Hadoop上路-04&lowbar;HBase0&period;98&period;0入门

    以下操作在Hadoop分布式集群基础上进行. 一.分布式环境搭建 下载:)验证 3)修改%HBASE%/conf/hbase-env.sh 4)修改$HBASE_HOME/conf/hbase-sit ...

  4. FZU 2225 小茗的魔法阵 扫描线&plus;树状数组

    这个题和一个CF上的找"Z"的题差不多,都是扫描线+树状数组 从右上角的主对角线开始扫描,一直扫到左下角,每次更新,右延伸等于该扫描线的点,注意在其所在的树状数组更新就好了 时间复 ...

  5. 完全背包的变形POJ1252

    话说今天做背包做到有点累了,题目是英文的--而且还很长,我看了好久(弱爆了). 题目大概的意思就是,有六种硬币,之后,求用这六种硬币最小数目支付1到100美分的平均值,以及最小数目中的最大值. 很容易 ...

  6. linux hash&lowbar;map

    在linux下的hash_map hash_map本身以前本身不属于标准库,是后来引入的.有两种可能:一种可能它被放在了stdext名空间里,那么你就要使用using namespace stdext ...

  7. PYCURL ERROR 6 - &OpenCurlyDoubleQuote;Couldn&&num;39&semi;t resolve host &&num;39&semi;mirrorlist&period;centos&period;org&&num;39&semi;”

    在虚拟机上安装的CentOS,估计是网络配置问题,导致yum update和yum install之类的功能的用不了.出现标题上面的错误. ifdown [network_adapter] ifup ...

  8. PDCA循环原理

    1.PDCA循环原理:plan  do  check action 以pdca质量环模型为质量控制和保证的理论依据,对软件质量进行把控. plan计划阶段:项目质量规划 1.分析现状,找出质量问题 2 ...

  9. 浅析文本挖掘(jieba模块的应用)

    一,文本挖掘 1.1,什么是文本挖掘 文本挖掘是指从大量文本数据中抽取事先未知的,可理解的,最终可用的知识的过程,同时运用这些知识更好的组织信息以便将来参考 1.2,文本挖掘基本流程 收集数据 数据集 ...

  10. &lbrack;Swift&rsqb;LeetCode647&period; 回文子串 &vert; Palindromic Substrings

    Given a string, your task is to count how many palindromic substrings in this string. The substrings ...