拓扑排序(栈)——POJ 1094
2014-08-06 13:01
309 查看
对应POJ题目:点击打开链接
Sorting It All Out
Crawling in process...Crawling failedTime
Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u
SubmitStatus
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
Sample Output
另附拓扑排序DFS版
Sorting It All Out
Crawling in process...Crawling failedTime
Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u
SubmitStatus
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.
#include <cstdio> #include <cstring> #include <queue> #include <iostream> using namespace std; const int INF=1<<30; const int MAXN=7500+10; int n,m; int G[30][30]; int in[30]; int out[30]; int in1[30]; int topo[30]; int countt; int toposort() { memset(topo,-1,sizeof(topo)); memset(in1,0,sizeof(in1)); memcpy(in1,in,sizeof(in)); queue<int>q; int t=0,qin=0; for(int u=0; u<n; u++){ if(!in[u] && out[u]) q.push(u); if(in[u] || out[u]) t++; } countt=0; while(!q.empty()) { if(q.size()>1) qin=1; int u=q.front(); q.pop(); topo[countt++]=u; for(int v=0; v<n; v++){ if(G[u][v]) in1[v]--; if(G[u][v] && !in1[v]) q.push(v); } } if(countt!=t) return 0;//有环 if(t!=n) return 1;//边没有连完 if(t==n && qin) return 2;//大小不确定 if(t==n && !qin) return 3;//大小确定 } int main() { //freopen("in.txt","r",stdin); while(cin>>n>>m, n+m) { memset(G,0,sizeof(G)); memset(in,0,sizeof(in)); int i,j,ok=-1; int num1,num2; for(i=0; i<m; i++){ char ch1,ch2,ch3; cin>>ch1>>ch2>>ch3; //cout<<ch1<<" "<<ch3<<endl; int a=ch1-'A'; int b=ch3-'A'; G[a][b]=1; in[b]++; out[a]++; if(ok==0 || ok==3) continue; ok=toposort(); if(ok==0) num1=i+1; if(ok==3) num2=i+1; } if(!ok) cout<<"Inconsistency found after "<<num1<<" relations."<<endl; else if(ok==3) { cout<<"Sorted sequence determined after "<<num2<<" relations: "; for(i=0; i<countt; i++) cout<<char(topo[i]+'A'); cout<<"."<<endl; } else cout<<"Sorted sequence cannot be determined."<<endl; } return 0; }
另附拓扑排序DFS版
bool dfs(int u) { c[c]=-1; for(int v=0; v<n; v++){ if(G[u][v]){ if(c[v]<0) return false;//存在有向环,退出 else if(!c[v] && !dfs(v)) return false;//后面的节点存在有向环,退出; } } c[u]=1; topo[--t]=u; return true; } bool toposort() { t=n; memset(c,0,sizeof(c)); for(int u=0; u<n; u++) { if(!c[u]){ if(!dfs(u)) return false; } } return true; }
相关文章推荐
- 拓扑排序,poj 1094
- POJ 1094: Sorting It All Out( 拓扑排序 )
- POJ 1094-Sorting It All Out(拓扑排序)
- Poj 1094-Sorting It All Out [拓扑排序]
- POJ 1094(拓扑排序)
- poj 1094 Sorting It All Out(java + 拓扑排序)
- POJ 1094(拓扑排序)
- POJ1094 Sorting It All Out 拓扑排序(深搜)
- POJ 1094 Sorting It All Out(拓扑排序)
- POJ 1094 拓扑排序
- poj 1094 Sorting It All Out 拓扑排序
- poj 1094 Sorting It All Out (拓扑排序)
- POJ - 1094(拓扑排序)
- POJ 1094 拓扑排序
- poj_1094 Sorting It All Out(拓扑排序 + floyd)
- POJ 1094 Sorting It All Out(拓扑排序)
- POJ-1094 Sorting It All Out -----拓扑排序判断状态
- poj1094----拓扑排序
- POJ1094(拓扑排序)
- poj 1094 Sorting It All Out (拓扑排序)