POJ 1094 Sorting It All Out Floyd_Washall+Topological_sort
title
Time Limit: 1000MS | Memory Limit: 10000K |
---|---|
Total Submissions: 40141 | Accepted: 14117 |
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
East Central North America 2001
code
#include<algorithm>
#include<bitset>
#include<cctype>
#include<cerrno>
#include<clocale>
#include<cmath>
#include<complex>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<streambuf>
#include<string>
#include<utility>
#include<vector>
#include<cwchar>
#include<cwctype>
using namespace std;
const int maxn=27;
template<typename T>inline void read(T &x)
{
x=0;
T f=1,ch=getchar();
while (!isdigit(ch) && ch^'-') ch=getchar();
if (ch=='-') f=-1, ch=getchar();
while (isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48), ch=getchar();
x*=f;
}
int n,m;
int d[maxn][maxn];
inline int Floyd_Washall(int n)
{
for (int k=1;k<=n;++k)
for (int i=1;i<=n;++i)
for (int j=1;j<=n;++j)
d[i][j]|=d[i][k]&d[k][j];
for (int i=1;i<=n;++i)
if (d[i][i]==1)
return 1;
return 0;
}
int ans[maxn],deg[maxn],vis[maxn];
inline int Topological_sort(int n)
{
int x=0,cnt;
memset(deg,0,sizeof(deg));
memset(ans,0,sizeof(ans));
memset(vis,0,sizeof(vis));
for (int i=1;i<=n;++i)
for (int j=1;j<=n;++j)
if (d[i][j]==1)
++deg[j];
for (int i=1;i<=n;++i)
{
cnt=0;
for (int j=1;j<=n;++j)
if (!deg[j] && !vis[j])
++cnt,x=j;
if (cnt>=2) return 0;
else if (!cnt) return 1;
ans[i]=x;
vis[x]=1;
for (int j=1;j<=n;++j)
--deg[j];
}
return 2;
}
int flag;
char ch[410][5];
int main()
{
while (1)
{
read(n);read(m);
if (!n && !m) break;
memset(d,0,sizeof(d));
flag=0;
for (int i=1;i<=m;++i)
scanf("%s",ch[i]);
int k;
for (k=1;k<=m;++k)
{
int a=ch[k][0]-'A'+1;
int b=ch[k][2]-'A'+1;
d[a][b]=1;
flag=Floyd_Washall(n);
if (flag==1) break;
flag=Topological_sort(n);
if (flag==2) break;
}
if (flag==1)
printf("Inconsistency found after %d relations.\n",k);
else if (!flag)
printf("Sorted sequence cannot be determined.\n");
else
{
printf("Sorted sequence determined after %d relations: ",k);
for (int i=1;i<=n;++i)
printf("%c",ans[i]+'A'-1);
printf(".\n");
}
}
return 0;
}
相關文章
- POJ 1094-Sorting It All Out(元素大小關係-拓撲排序)排序
- poj 1094 拓撲排序排序
- poj1094 拓撲排序排序
- POJ1094[有向環 拓撲排序]排序
- javascript sorting/ v8 sortingJavaScript
- Sorting in JavaScriptJavaScript
- Sorting with Twos
- Homework record-Simple sorting
- LintCode-Topological Sorting
- ZOJ Problem Set - 1094 Matrix Chain MultiplicationAI
- 347. Top K Frequent Elements - Bucket Sorting
- (DP) CF1861D Sorting By Multiplication
- Sorting 排序演算法: Quick Sort 快速排序排序演算法UI
- PostgreSQL 13支援增量排序(Incremental Sorting)SQL排序REM
- HDOJ 1094 A+B for Input-Output Practice (VI)
- docker in allDocker
- OOM(Out Of Memory)OOM
- Out With the Old and in With the New
- java out of memoryJava
- out of springSpring
- CF1815A Ian and Array Sorting 題解
- POJ 3461 kmpKMP
- poj3417
- OCP(11g)-----> oracle First In First Out (FIFO)/Last In First OutOracleAST
- (十) 資料庫查詢處理之排序(sorting)資料庫排序
- [PAT]1028. List Sorting (25)@Java實現Java
- CF1982F Sorting Problem Again 題解AI
- Atcoder nomura2020F Sorting GameGAM
- IPC__ALL
- 洛谷P1094 紀念品分組(Java)Java
- SQL not exist out joinSQL
- OOM--OUT OF MEMORYOOM
- Kotlin 1.2.50 is out!Kotlin
- torch--drop out
- PAT 乙級 1094 谷歌的招聘 (20分)---【素數 字串】谷歌字串
- Flip Game(POJ 1753)GAM
- POJ 2431 Expedition
- POJ3259-WormholesWorm