【POJ】1094 Sorting It All Out(トポロジー並べ替え)
4948 ワード
http://poj.org/problem?id=1094
もとは位相の序はこのようにすることができて、もとはsbのは白い本の上で言うdfsを使います...................
トポロジカルシーケンスは、毎回0になる点をスタックに加入し、その後、メンテナンスの進捗度を広げるだけでよい.
私は大きいs bです.この水の問題を朝に変えました.
Description
An ascending sorted sequence of distinct values is one in which some form of a less-than operators is used to order the elemens from slarget.For example、the sorted sequence A、B、C、D implementwe will give you a set of relations of the form AInput
Input consists of multile proble instance.Each instance starts with a line containing two positive integers n and m.the first value indicated the number of object to sort,where 2<=n====26.The oobjecs to be sorted will be the first n characters of the up percase alaboet.The second value m indicates the number of relatititititions of the form AOutput
For each problem instance,output consists of one line.This line shound be one of the follwing three:
Sorted sequence determined after xxx relations:y y...y.
Sorted sequence cannot be determined.
Inconsistency found after xxx relations.
where xxx is the number of relations processed at the time eigher a sorted sequence is determined or an inconsistency is found、whichever cons first、and y y...y the sorted、ascending sequence.
Sample Input
East Central North America 2001
もとは位相の序はこのようにすることができて、もとはsbのは白い本の上で言うdfsを使います...................
トポロジカルシーケンスは、毎回0になる点をスタックに加入し、その後、メンテナンスの進捗度を広げるだけでよい.
私は大きいs bです.この水の問題を朝に変えました.
#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.
", 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 operators is used to order the elemens from slarget.For example、the sorted sequence A、B、C、D implement
Input consists of multile proble instance.Each instance starts with a line containing two positive integers n and m.the first value indicated the number of object to sort,where 2<=n====26.The oobjecs to be sorted will be the first n characters of the up percase alaboet.The second value m indicates the number of relatititititions of the form AOutput
For each problem instance,output consists of one line.This line shound be one of the follwing three:
Sorted sequence determined after xxx relations:y y...y.
Sorted sequence cannot be determined.
Inconsistency found after xxx relations.
where xxx is the number of relations processed at the time eigher a sorted sequence is determined or an inconsistency is found、whichever cons first、and y y...y 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 OutputSorted sequence determined after 4 relations: ABCD.
Inconsistency found after 2 relations.
Sorted sequence cannot be determined.
ソurceEast Central North America 2001