POJ 1094 Sorting It All Out(拓撲排序+判環+拓撲路徑唯一性確定)
阿新 • • 發佈:2018-10-23
ons esc def input miss with con nts following Sorting It All Out
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.
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.
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 39602 | Accepted: 13944 |
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
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
East Central North America 2001 分析: 有向圖判環:拓撲排序 無向圖判環:並查集 本題需要進行m次的拓撲排序 拓撲排序判環:拓撲之後,如果存在沒有入隊的點,那麽該點一定是環上的點 拓撲路徑的唯一性確定:隊中某一時刻存在兩個元素,則至少有兩條不同的拓撲路徑#include<stdio.h> #include<iostream> #include<math.h> #include<string.h> #include<set> #include<map> #include<list> #include<queue> #include<algorithm> using namespace std; typedef long long LL; int mon1[13]= {0,31,28,31,30,31,30,31,31,30,31,30,31}; int mon2[13]= {0,31,29,31,30,31,30,31,31,30,31,30,31}; int dir[4][2]= {{0,1},{0,-1},{1,0},{-1,0}}; int getval() { int ret(0); char c; while((c=getchar())==‘ ‘||c==‘\n‘||c==‘\r‘); ret=c-‘0‘; while((c=getchar())!=‘ ‘&&c!=‘\n‘&&c!=‘\r‘) ret=ret*10+c-‘0‘; return ret; } #define max_v 55 int indgree[max_v]; int temp[max_v]; int G[max_v][max_v]; int tp[max_v]; int n,m; queue<int> q; int tpsort() { while(!q.empty()) q.pop(); for(int i=1;i<=n;i++) { indgree[i]=temp[i]; if(indgree[i]==0) q.push(i); } int c=0,p; int flag=0; while(!q.empty()) { if(q.size()>1) flag=1; p=q.front(); q.pop(); tp[++c]=p; for(int i=1;i<=n;i++) { if(G[p][i]) { indgree[i]--; if(indgree[i]==0) q.push(i); } } } /* 拓撲完之後,存在沒有入隊的點,那麽該點就一定是環上的 */ if(c!=n)//存在環 return 1; else if(flag)//能拓撲但存在多條路 return 0; return -1;//能拓撲且存在唯一拓撲路徑 } int main() { int x,y; char c1,c2; while(~scanf("%d %d",&n,&m)) { if(n==0&&m==0) break; memset(G,0,sizeof(G)); memset(temp,0,sizeof(temp)); memset(tp,0,sizeof(tp)); int flag1=0,index1=0;//是否有環及環的位置 int flag2=0,index2=0;//能否拓撲和拓撲的位置 for(int i=1;i<=m;i++) { getchar(); scanf("%c<%c",&c1,&c2); x=c1-‘A‘+1; y=c2-‘A‘+1; if(flag1==0&&flag2==0) { if(G[y][x])//環的一種情況 { flag1=1; index1=i; continue; } if(G[x][y]==0)//預防重邊 { G[x][y]=1; temp[y]++; } int k=tpsort(); if(k==1)//存在環 { flag1=1; index1=i; continue; }else if(k==-1)//存在唯一拓撲路徑 { flag2=1; index2=i; } } } if(flag1==0&&flag2==0) { printf("Sorted sequence cannot be determined.\n"); }else if(flag1) { printf("Inconsistency found after %d relations.\n",index1); }else if(flag2) { printf("Sorted sequence determined after %d relations: ",index2); for(int i=1;i<=n;i++) { printf("%c",tp[i]+‘A‘-1); } printf(".\n");//!!!註意還有個點... } } return 0; }
POJ 1094 Sorting It All Out(拓撲排序+判環+拓撲路徑唯一性確定)