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;
}
相關文章
- Sorting with Twos
- Sorting arrays in NumPy by column
- Homework record-Simple sorting
- Atcoder nomura2020F Sorting GameGAM
- (DP) CF1861D Sorting By Multiplication
- CF1815A Ian and Array Sorting 題解
- 347. Top K Frequent Elements - Bucket Sorting
- poj 2031
- poj 3461
- Sorting 排序演算法: Quick Sort 快速排序排序演算法UI
- CF1982F Sorting Problem Again 題解AI
- [題解]CF1374F Cyclic Shifts Sorting
- PostgreSQL 13支援增量排序(Incremental Sorting)SQL排序REM
- poj 3278 BFS
- POJ 2975 Nim
- poj3417
- POJ 1089 Intervals
- POJ 3414 Pots
- 洛谷P1094 紀念品分組(Java)Java
- 19c ADG報錯Error 1094 attaching to RFS for reconnectError
- Kotlin 1.2.50 is out!Kotlin
- OOM(Out Of Memory)OOM
- docker in allDocker
- PAT 乙級 1094 谷歌的招聘 (20分)---【素數 字串】谷歌字串
- Network(POJ-1144)
- POJ 2553 The Bottom of a Graph
- POJ 1861 Network (Kruskal)
- Apple Catching POJ - 2385APP
- POJ 1442 Black Box
- POJ 2799 IP Networks
- POJ3259-WormholesWorm
- POJ3414-Pots
- 【BFS】poj 3414 Pots
- SQL not exist out joinSQL
- torch--drop out
- (十) 資料庫查詢處理之排序(sorting)資料庫排序
- Road Construction(POJ-3352)Struct
- Redundant Paths(POJ-3177)