poj1094 Sorting It All Out【floyd】【传递闭包】【拓扑序】
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions:39731 | Accepted: 13975 |
Description
Input
Output
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】【传递闭包】【拓扑序】的更多相关文章
- POJ3660:Cow Contest(Floyd传递闭包)
Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16941 Accepted: 9447 题目链接 ...
- UVA 247 电话圈 (floyd传递闭包 + dfs输出连通分量的点)
题意:输出所有的环: 思路:数据比较小,用三层循环的floyd传递闭包(即两条路通为1,不通为0,如果在一个环中,环中的所有点能互相连通),输出路径用dfs,递归还没有出现过的点(vis),输出并递归 ...
- UVA 753 UNIX 插头(EK网络流+Floyd传递闭包)
UNIX 插头 紫书P374 [题目链接]UNIX 插头 [题目类型]EK网络流+Floyd传递闭包 &题解: 看了书之后有那么一点懂了,但当看了刘汝佳代码后就完全明白了,感觉他代码写的好牛逼 ...
- UVA 247 电话圈(Floyd传递闭包+输出连通分量)
电话圈 紫书P365 [题目链接]电话圈 [题目类型]Floyd传递闭包+输出连通分量 &题解: 原来floyd还可以这么用,再配合连通分量,简直牛逼. 我发现其实求联通分量也不难,就是for ...
- POJ 3660 Cow ContestCow(Floyd传递闭包)题解
题意:给出m个关系,问你能确定机头牛的排名 思路:要确定排名那必须要把他和其他n-1头牛比过才行,所以Floyd传递闭包,如果赢的+输的有n-1就能确定排名. 代码: #include<cstd ...
- nyoj 211——Cow Contest——————【floyd传递闭包】
Cow Contest 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 N (1 ≤ N ≤ 100) cows, conveniently numbered 1.. ...
- POJ 2594 —— Treasure Exploration——————【最小路径覆盖、可重点、floyd传递闭包】
Treasure Exploration Time Limit:6000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64 ...
- POJ 3660—— Cow Contest——————【Floyd传递闭包】
Cow Contest Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- BZOJ 1612 [Usaco2008 Jan]Cow Contest奶牛的比赛:floyd传递闭包
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1612 题意: 有n头牛比赛. 告诉你m组(a,b),表示牛a成绩比牛b高. 保证排名没有并 ...
- POJ-2594 Treasure Exploration floyd传递闭包+最小路径覆盖,nice!
Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 8130 Accepted: 3 ...
随机推荐
- 基于Centos搭建个人 Leanote 云笔记本
系统要求:CentOS 7.2 64 位操作系统 下载启动 MongoDB Leanote 依赖 MongoDB 作为数据存储,下面开始安装 MongoDB: 下载 MongoDB 进入 /home ...
- 11G新特性 -- archival(long-term)backups
在oracle 10g中,提供了backup ... keep功能来重载配置好的retention策略. 在oracle 11g中,可以重定义backup ... keep命令来创建长期保留的备份,称 ...
- 格雷码(Gray code)仿真
作者:桂. 时间:2018-05-12 16:25:02 链接:http://www.cnblogs.com/xingshansi/p/9029081.html 前言 FIFO中的计数用的是格雷码, ...
- gitlab 502 报错
这里从网上查到文章,我这里看了一下我这里是unicorn的问题 说一下情况:这里我们的一个前端修改了大量的打包,并进行了打包.然后提交merge request 分支到master,结果看到页面50 ...
- c链表之oc AutoReleasePool
直接贴 原文吧: http://blog.sunnyxx.com/2014/10/15/behind-autorelease/
- 第三部分:Android 应用程序接口指南---第二节:UI---第十一章 样式和主题
第11章 样式和主题 style是用于指定View或window的外观和格式的一系列属性的集合.style可以指定高(height).填补(padding).字体颜色.字体大小.背景颜色等等属性.st ...
- CINATRA发布第一个版本
cinatra是什么? cinatra是C++开源社区–purecpp发起的一个开源项目,现在正式发布第一个版本cinatra0.9.0,cinatra是一个现代C++写的web framework, ...
- [转]protoc-gen-lua 编译、安装、使用教程
版权声明:本文转自http://blog.csdn.net/huutu 转载请带上 http://www.liveslives.com/ https://blog.csdn.net/cp7906216 ...
- LeetCode: Valid Parentheses 解题报告
Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', det ...
- 【九天教您南方cass 9.1】 13 等高线法计算土方量
同学们大家好,欢迎收看由老王测量上班记出品的cass9.1视频课程 我是本节课主讲老师九天. 我们讲课的教程附件也是共享的,请注意索取 在测量空间中. [点击索取cass教程]5元立得 (给客服说暗号 ...