Poj/OpenJudge 1094 Sorting It All Out
1.链接地址:
http://poj.org/problem?id=1094
http://bailian.openjudge.cn/practice/1094
2.题目:
Sorting It All Out
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25547 Accepted: 8861 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 0Sample Output
Sorted sequence determined after 4 relations: ABCD. Inconsistency found after 2 relations. Sorted sequence cannot be determined.Source
East Central North America 2001
3.思路:
4.代码:
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <cstdlib> 5 6 using namespace std; 7 8 int n,m; 9 int map[30][30]; 10 int reg[100]; 11 int in[30],out[30]; 12 char ans[30]; 13 int stack[30]; 14 void fun() 15 { 16 int i,j; 17 for(i='A',j=1;i<='Z';i++,j++)reg[i]=j; 18 } 19 void toposort(char *ans) 20 { 21 int i,top=0,u,s=0; 22 for(i=1;i<=n;i++) 23 if(in[i]==0)stack[top++]=i; 24 while(top!=0) 25 { 26 u=stack[--top]; 27 ans[s++]=u+64; 28 for(i=1;i<=n;i++) 29 { 30 if(map[u][i]) 31 { 32 in[i]--; 33 if(!in[i])stack[top++]=i; 34 } 35 } 36 } 37 ans[s]=0; 38 } 39 int main() 40 { 41 int i,j,x,y,k,flag1,flag2,flag; 42 fun(); 43 char ch[5]; 44 while(1) 45 { 46 flag1=flag2=0; 47 memset(map,0,sizeof(map)); 48 scanf("%d%d",&n,&m); 49 if(n==0&&m==0)break; 50 for(i=1;i<=m;i++) 51 { 52 flag=1; 53 scanf("%s",ch); 54 x=reg[ch[0]]; 55 y=reg[ch[2]]; 56 map[x][y]=1; 57 if(x==y)flag1=i; 58 memset(in,0,sizeof(in)); 59 memset(out,0,sizeof(out)); 60 if(!flag1&&!flag2) 61 for(j=1;j<=n;j++) 62 for(k=1;k<=n;k++) 63 { 64 if(j!=x&&k!=y)map[j][k]=map[j][k]||(map[j][x]&&map[y][k]); 65 if(j==x&&k!=y)map[j][k]=map[j][k]||map[y][k]; 66 if(j!=x&&k==y)map[j][k]=map[j][k]||map[j][x]; 67 if(map[j][k]) 68 { 69 out[j]++; 70 in[k]++; 71 } 72 } 73 j=1; 74 if(!flag1) 75 for(j=1;j<=n;j++) 76 { 77 if(map[j][j])flag1=i; 78 if(in[j]+out[j]!=n-1)flag=0; 79 } 80 if(flag&&!flag2&&j>n){flag2=i;toposort(ans);} 81 } 82 if(flag2) 83 { 84 printf("Sorted sequence determined after %d relations: %s.\n",flag2,ans); 85 continue; 86 } 87 if(flag1) 88 { 89 printf("Inconsistency found after %d relations.\n",flag1); 90 continue; 91 } 92 printf("Sorted sequence cannot be determined.\n"); 93 } 94 return 0; 95 }
转载于:https://www.cnblogs.com/mobileliker/p/3548236.html
Poj/OpenJudge 1094 Sorting It All Out相关推荐
- POJ 1094 Sorting It All Out (拓扑排序)
题意:给你一些大写字母间的偏序关系,然后让你判断能否唯一确定它们之间的关系,或者所给关系是矛盾的,或者到最后也不能确定它们之间的关系. 由DAG图节点的偏序关系确定节点的排序可以由拓扑排序求出.而 ...
- POJ - 1094 Sorting It All Out(拓扑排序)
https://vjudge.net/problem/POJ-1094 题意 对于N个大写字母,给定它们的一些关系,要求判断出经过多少个关系之后可以确定它们的排序或者排序存在冲突,或者所有的偏序关系用 ...
- POJ - 1094 Sorting It All Out(拓扑排序+floyd传递闭包)
题目链接:点击查看 题目大意:给出N个点以及M个比较关系,问在第几个数字可以确定出唯一的序列,或者判断出矛盾的序列,或者最后也无法确定出一个唯一的序列 题目分析:关于这个题目可以直接分类讨论,可以直接 ...
- poj 1094 Sorting It All Out(拓扑排序)
2018-3-25 拓扑排序的题目,需要注意的是,这里是边输入边判断的,之前有一组数据一直不知道为什么不过: A>F B>D C>E F>D D>E E>F 其实当 ...
- [POJ 3270]Cow Sorting
Description Farmer John's N (1 ≤ N ≤ 10,000) cows are lined up to be milked in the evening. Each cow ...
- POJ 1007 DNA Sorting
按照字符串的逆序排序. /*Accepted 100K 16MS C++ 863B 2012-08-03 08:30:48*/ #include<stdio.h> #include< ...
- 【POJ - 1486】Sorting Slides(思维建图,二分图求必须边,关建边,图论)
题干: Professor Clumsey is going to give an important talk this afternoon. Unfortunately, he is not a ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自--http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- 一系列图论问题[转]
=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...
- 图论练习题(存起来练)
=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Man ...
最新文章
- 3D相机D2C对齐的方法
- 编程中python怎么读-编程语言如何在Python中读写文件
- es6的Proxy(代理)
- VTK:vtkBooleanOperationPolyDataFilter用法实战
- Windows下安装部署DBeaver连接clickhouse
- linux查看进程和线程的命令
- 萌宝出街,熊孩子逆袭小小“时髦精”
- Slog55_lua面向对象之lua类
- JS只能输入数字,数字和字母等的正则表达式
- 小议:怎样解决创建Web Application失败问题?
- 组策略不让你登陆你怎么办
- Android开发学习笔记---搭建Android开发环境
- 认知时代,IBM主机与LinuxONE交付更大业务价值
- 读“产品经理那些事儿”有感
- 30.yii2 --- 全文检索简介
- MIPI D-PHY IP 使用说明(二)
- 缠论找日线找第二类买点买入程序
- 用户画像第四章(企业级360°用户画像_标签开发_挖掘标签_ 客户价值模型-RFM)
- ACM/ICPC 2018亚洲区预选赛北京赛站网络赛-B:Tomb Raider(二进制枚举)
- 上海庆科EMW3162 WiFi模块 串口透传