poj1094 Sorting It All Out

 一道有点猥琐的拓扑题,之前对于输出样例的数字有点不太理解,后来明白是推到给定的第几组输入可以唯一确定一个拓扑的结果(唯一、没有、还是有回路),那么就需要我们输入一条边处理一条边,每对一次输入拓扑之后要记得把入度加回去

在最后附上测试数据供各位查找错误

#include <iostream>
#include <stack>
#include <vector>
using namespace std;
const int size = 300;

vector <int>mapp[size];
int IN[size];
int counter;
char ans[size];
int id;
bool flag1;
stack <int> ST1;
void init(int n)
{
     for (int i = 0; i < n; i ++){
         mapp[i].clear();   
     }    
}
void toposort(int n, int *IN)
{
     stack <int> ST;
     for (int i = 0; i < n; i ++){
         if (!IN[i]){
            ST.push(i);
            ST1.push(i);
         }  
     }
     while (ST.size()){
           if (ST.size() > 1)flag1 = true;
           int k = ST.top();
           ST.pop();
           ans[id ++] = k+'A';
           counter ++;
           for (int j = 0; j < mapp[k].size(); j ++){
               int kk = mapp[k][j];
               IN[kk] --;
               if (!IN[kk]){ST.push(kk),ST1.push(kk);}
           }     
     }
         
}
int main()
{
    int n, m;
    while (scanf("%d%d", &n, &m) != EOF &&(m|n)){
          char a[5];
          init(n);
          memset(IN, 0, sizeof(IN));
          int check = 0;
          int ans1, ans2;
          for (int i = 0; i < m; i ++){
              scanf("%s", a);
              mapp[a[0]-'A'].push_back(a[2]-'A');
              IN[a[2]-'A'] ++;
              if (!check){
                 flag1 = false;
                 id = counter = 0;   
                 toposort(n, IN);
                 if (counter != n){
                    check = 1;
                    ans1 = i+1;           
                 }
                 else
                     if (i == m-1 && flag1 )check = 2;
                     else if (counter == n && !flag1){ans2 = i+1, check = 3;}
                 while (ST1.size()){
                       int kk = ST1.top();
                       ST1.pop();
                       for (int j = 0; j < mapp[kk].size(); j ++){
                           int s = mapp[kk][j];
                           IN[s] ++;   
                       }    
                 }
              }
          }
          if (check == 1){
             printf("Inconsistency found after %d relations.\n", ans1);
             continue;           
          }
          if (check == 2){
             printf("Sorted sequence cannot be determined.\n");
             continue;           
          }
          if (check == 3){
             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
M<D
H<D
N<T
C<O
I<T
D<R
C<K
H<P
C<I
C<P
N<A
R<B
H<M
R<I
C<Q
I<E
Q<I
Q<D
C<S
D<L
N<B
Q<K
C<G
P<J
C<A
J<T
Q<L
D<P
R<G
S<T
H<K
I<O
D<E
I<B
P<K
K<E
N<G
C<F
M<T
S<B
A<K
S<E
A<F
A<T
P<G
R<E
D<I
F<K
I<S
G<J
H<F
G<S
L<F
P<I
M<L
G<I
N<P
B<T
L<P
J<K
C<H
B<K
A<I
G<E
G<T
N<M
M<B
H<R
D<J
M<E
F<G
Q<M
D<S
H<B
N<J
Q<E
G<O
C<N
H<A
P<T
Q<B
L<S
R<S
N<L
L<G
Q<R
F<O
A<R
L<E
Q<P
E<T
M<R
M<S
L<O
A<L
R<P
H<J
L<K
M<P
A<M
L<T
R<T
L<R
S<J
N<E
F<I
A<D
D<F
Q<O
J<O
O<E
C<L
A<B
N<K
N<F
G<K
D<T
N<I
H<L
M<I
L<J
0 0
输出数据:

Sorted sequence determined after 6 relations: DCAB.
Sorted sequence determined after 29 relations: CFDAEBHGJI.
Sorted sequence cannot be determined.
Inconsistency found after 48 relations.
Sorted sequence determined after 318 relations: GMNVKCHFOBRJDZLPXAYSEIWQTU.
Sorted sequence determined after 25 relations: ABCDEFGHIJKLMNOPQRSTUVWXYZ.
Sorted sequence determined after 7 relations: BADCE.
Sorted sequence determined after 158 relations: HNAMGIPCTBJFRLEODKSQ.
Inconsistency found after 35 relations.
Sorted sequence cannot be determined.
Sorted sequence determined after 7 relations: ABDEC.
Sorted sequence cannot be determined.
Sorted sequence determined after 11 relations: ACDBFE.
Sorted sequence cannot be determined.
Sorted sequence cannot be determined.
Sorted sequence determined after 179 relations: CHQNAMDLRFPGISBJOKET.

 

你可能感兴趣的:(vector,测试,sorting)