poj1094
Sorting It All Out
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 32966 | Accepted: 11458 |
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 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.
题目大意:给你n个大写字母,和m组字母相互之间的大小关系,让你判断在哪一次能够将所有字母
排好序,又或者在哪一次给出的关系与前面矛盾,即构成环了,否则就不能判断
思路:利用拓扑排序,如果能够排序,那么入度为0的点只可能有一个,否则就不知道选择哪一个在前
如果所有的点不能全部进行出栈排序,说明一定形成环了
注意:在拓扑排序时一定要注意判断顺序,就因为这个WA了好多次
代码如下:
1 #include <iostream> 2 #include<cstdio> 3 #include<stack> 4 #include<cstring> 5 using namespace std; 6 int n,m; 7 char str[4]; 8 int degree[27],A[27][27];//入度,以及邻接矩阵 9 char letter[27];//存储排序好的字母 10 int topSort() 11 { 12 //如果能够排序,那么肯定入度为0的点只有一个,有多个则不能确定选择哪一个 13 stack<int> st; 14 int temp[27],flag=0; 15 memcpy(temp,degree,sizeof(degree)); 16 for(int i=1;i<=n;i++) 17 if(temp[i]==0) 18 st.push(i);//找到入度为0的点 19 int index=0,cur; 20 while(!st.empty()) 21 { 22 if(st.size()>1)//入度为0点有多个,不能唯一排序 23 flag=1; 24 cur = st.top(); 25 st.pop(); 26 letter[index++]=cur+'A'-1; 27 for(int i=1;i<=n;i++) 28 if(A[cur][i]==1) 29 { 30 temp[i]--; 31 if(temp[i]==0) 32 st.push(i);//找到入度为0的点 33 } 34 } 35 if(index<n) 36 return 0;//有环 37 if(flag) 38 return -1; 39 return 1;//排序成功 40 41 } 42 int main() 43 { 44 freopen("in1.txt","r",stdin); 45 while(scanf("%d%d",&n,&m)!=EOF&&n) 46 { 47 bool sucess=false; 48 int flag=-1,time=0; 49 memset(degree,0,sizeof(degree)); 50 memset(A,0,sizeof(A)); 51 memset(letter,0,sizeof(letter)); 52 if(m==0&&n==1) 53 { 54 printf("Sorted sequence determined after 0 relations: A.\n"); 55 continue; 56 } 57 for(int i=1;i<=m;i++) 58 { 59 scanf("%s",str); 60 if(sucess) 61 continue; 62 int x = str[0]-'A'+1; 63 int y = str[2]-'A'+1; 64 degree[y]++;//入度加1 65 A[x][y]=1;//表示x邻接y 66 int flag1 = topSort(); 67 if(flag1==0) 68 { 69 flag=0;time=i; 70 sucess=true; 71 } 72 if(flag1==1) 73 { 74 flag=1;time=i; 75 sucess=true; 76 } 77 78 } 79 if(flag==-1) 80 printf("Sorted sequence cannot be determined.\n"); 81 else if(flag==0) 82 printf("Inconsistency found after %d relations.\n",time); 83 else 84 { 85 printf("Sorted sequence determined after %d relations: ",time); 86 for(int j=0;j<n;j++) 87 printf("%c",letter[j]); 88 printf(".\n"); 89 } 90 } 91 return 0; 92 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构