【POJ】1094 Sorting It All Out(拓扑排序)
http://poj.org/problem?id=1094
原来拓扑序可以这样做,原来一直sb的用白书上说的dfs。。。。。。。。。。。。
拓扑序只要每次将入度为0的点加入栈,然后每次拓展维护入度即可。。
我是个大sb,这种水题调了一早上。。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | #include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream> #include <algorithm> #include <queue> using namespace std; #define rep(i, n) for(int i=0; i<(n); ++i) #define for1(i,a,n) for(int i=(a);i<=(n);++i) #define for2(i,a,n) for(int i=(a);i<(n);++i) #define for3(i,a,n) for(int i=(a);i>=(n);--i) #define for4(i,a,n) for(int i=(a);i>(n);--i) #define CC(i,a) memset(i,a,sizeof(i)) #define read(a) a=getint() #define print(a) printf("%d", a) #define dbg(x) cout << (#x) << " = " << (x) << endl #define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; } #define printarr1(a, b) for1(_, 1, b) cout << a[_] << '\t'; cout << endl inline const int getint() { int r=0, k=1; char c= getchar (); for (; c< '0' ||c> '9' ; c= getchar ()) if (c== '-' ) k=-1; for (; c>= '0' &&c<= '9' ; c= getchar ()) r=r*10+c- '0' ; return k*r; } inline const int max( const int &a, const int &b) { return a>b?a:b; } inline const int min( const int &a, const int &b) { return a<b?a:b; } const int N=27, oo=~0u>>1; int n, m, e[N][N], in[N], s[N], vis[N], top, tp[N], ans[N], tot; int toposort() { top=tot=0; int num=0, ret=0; for1(i, 1, n) if (vis[i] && in[i]==0) s[++top]=i; for1(i, 1, n) if (vis[i]) ++num; for1(i, 1, n) tp[i]=in[i]; while (top) { if (top>1) ret=-1; int u=s[top--]; ans[tot++]=u; for1(i, 1, n) if (e[u][i] && --tp[i]==0) s[++top]=i; } if (tot!=num) return ret=1; return ret; } int main() { while (~ scanf ( "%d%d" , &n, &m) && !(n==0 && m==0)) { CC(e, 0); CC(in, 0); CC(vis, 0); int u, v; char s[10]; int flag=0; for1(i, 1, m) { scanf ( "%s" , s); if (flag==1) continue ; u=s[0]- 'A' +1; v=s[2]- 'A' +1; e[u][v]=vis[u]=vis[v]=1; ++in[v]; flag=toposort(); if (flag==0) { if (tot!=n) continue ; printf ( "Sorted sequence determined after %d relations: " , i); rep(j, tot) printf ( "%c" , 'A' +ans[j]-1); puts ( "." ); flag=1; } else if (flag==1) printf ( "Inconsistency found after %d relations.\n" , i); } if (flag==-1) puts ( "Sorted sequence cannot be determined." ); } return 0; } |
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.
Source
博客地址:www.cnblogs.com/iwtwiioi 本文为博主原创文章,未经博主允许不得转载。一经发现,必将追究法律责任。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix