[POJ](1094)Sorting It All Out ---拓扑排序(图)
来源:互联网 发布:淘宝交易指数 构成 编辑:程序博客网 时间:2024/06/09 16:50
Sorting It All Out
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 35706 Accepted: 12565
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.
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 6A<BA<CB<CC<DB<DA<B3 2A<BB<A26 1A<Z0 0
Sample Output
Sorted sequence determined after 4 relations: ABCD.Inconsistency found after 2 relations.Sorted sequence cannot be determined.
Source
East Central North America 2001
解题新知:(*@ο@*) 哇~这道题看了好长时间,理解题意是关键,还要注意题意情况的优先级关系。/(ㄒoㄒ)/~~不说了,还是看前辈们的思路和做法吧。自己也是稍微理解,稍微改了改前辈的代码。
参考:http://www.cnblogs.com/pushing-my-way/archive/2012/08/23/2652033.html
AC代码:
#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<queue>using namespace std;int mmap[30][30];int indegree[30];//int copyindegree[30];int llist[30];int n,m;int topoSort(){ queue<int>q; int copyindegree[30]; for(int i=0;i<n;i++) { copyindegree[i]=indegree[i]; if(!indegree[i]) q.push(i); } int flag=0; int num=0; int k=-1; while(!q.empty()) { if(q.size()>1) flag=1; k=q.front(); q.pop(); llist[num++]=k; for(int i=0;i<n;i++) { if(mmap[k][i]==1) { copyindegree[i]--; if(copyindegree[i]==0) q.push(i); } } } if(num!=n) return 1; else if(flag) return 2; return 0;}int main(){ while(cin>>n>>m && n && m) { int determined=0;//唯一升序序列确定 int inconsistency=0;//矛盾 char s[5]; int u,v; int res; memset(mmap,0,sizeof(mmap)); memset(indegree,0,sizeof(indegree)); memset(llist,0,sizeof(llist)); for(int i=1;i<=m;i++) { cin>>s; u=s[0]-'A'; v=s[2]-'A'; if(!determined && !inconsistency) { if(mmap[v][u]==1) { inconsistency=1; printf("Inconsistency found after %d relations.\n",i); continue;//??? } if(mmap[u][v]==0) { mmap[u][v]=1; indegree[v]++; } res=topoSort(); if(res==0) { printf("Sorted sequence determined after %d relations: ",i); for(int j=0;j<n;j++) printf("%c",llist[j]+'A'); printf(".\n"); determined=1; } else if(res==1) { printf("Inconsistency found after %d relations.\n",i); inconsistency=1; } } } if(!determined && !inconsistency) { printf("Sorted sequence cannot be determined.\n"); } } return 0;}
阅读全文
0 0
- [POJ](1094)Sorting It All Out ---拓扑排序(图)
- POJ 1094Sorting It All Out(拓扑排序)
- [ACM] POJ 1094 Sorting It All Out (拓扑排序)
- poj 1094 Sorting It All Out (拓扑排序)
- POJ 1094-Sorting It All Out(拓扑排序)
- POJ 题目1094Sorting It All Out(拓扑排序)
- poj 1094 Sorting It All Out(拓扑排序)
- POJ 1094 Sorting It All Out(拓扑排序)
- POJ-1094---Sorting It All Out(拓扑排序)
- POJ 1094 Sorting It All Out (拓扑排序)
- POJ 1094 -- Sorting It All Out (拓扑排序)
- POJ-1094-Sorting It All Out(拓扑排序)
- POJ 1094 Sorting It All Out(拓扑排序)
- POJ 1094 Sorting It All Out(拓扑排序)
- POJ-1094 Sorting It All Out (拓扑排序)
- POJ 1094 Sorting It All Out (拓扑排序)
- poj 1094 Sorting It All Out (拓扑排序)@
- poj 1094 Sorting It All Out(拓扑排序)
- HDU 6127 Hard challenge 计算几何 极角排序
- .NET MVC Scripts.Render 上下文不存在问题解决方法
- Qt:基于widget方式的窗口阴影边框的实现
- Compare Version Numbers问题及解法
- leetcode 213. House Robber II
- [POJ](1094)Sorting It All Out ---拓扑排序(图)
- 笔记——TensorFlow and deep learning, without a PhD
- 设计模式学习:策略模式(Strategy Pattern)
- 20170816任务
- java内存区域-虚拟机栈
- CentoOS常用操作命令
- MySQL中的表中增加删除字段
- Coursera机器学习 Week3 笔记
- react-native-scrollable-tab-view的版本坑