poj 1094 Sorting It All Out (拓扑排序)

Sorting It All Out
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 26088   Accepted: 9035

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.

Source

题意:

    给出n个大写字母一系列的大小关系,问给出第几个时能确定其全部关系(是否确定全部大小),关系有三种:有环矛盾、不确定、确定。

    这题有个小陷阱,就是要每输入一个关系就要topo判断一次,确定后之后的关系不影响,简单处理掉就行。

拓扑排序:

    这题其实不难,全部处理完如果不是确定关系或矛盾关系即是不确定关系。处理时难点好像也没有,就是注意一下in和V的复制。

 

 1 //180K    16MS    C++    1629B    2014-04-11 17:30:45
 2 #include<iostream>
 3 #include<queue>
 4 #include<vector>
 5 using namespace std;
 6 vector<int>V[30],tV[30];
 7 int in[30];
 8 int n,m;
 9 int certain,conflict;
10 void topo(int times)
11 {
12     queue<int>Q;
13     char ans[30];
14     memset(ans,0,sizeof(ans));
15     int sum=0,uncertain=0;
16     int tin[30];
17     for(int i=0;i<n;i++){ //复制 
18         tin[i]=in[i];
19         tV[i].resize(V[i].size());
20         memcpy(&tV[i][0],&V[i][0],V[i].size()*sizeof(int));
21     }
22     for(int i=0;i<n;i++){
23         if(tin[i]==0){
24             Q.push(i);tin[i]--;
25         }
26     }
27     
28     while(!Q.empty()){
29         if(Q.size()>1) uncertain=1;
30         int t=Q.front();
31         Q.pop();
32         ans[sum++]=t+'A';
33         tin[t]--;
34         for(int i=0;i<tV[t].size();i++){
35             if(--tin[tV[t][i]]==0){
36                 Q.push(tV[t][i]);
37             }
38         }
39     }
40     //printf("**%d\n",sum); 
41     if(sum<n){
42         conflict=1;printf("Inconsistency found after %d relations.\n",times);
43     }
44     else if(uncertain) return;
45     else{
46         certain=1;
47         printf("Sorted sequence determined after %d relations: %s.\n",times,ans);
48     }
49 }
50 int main(void)
51 {
52     char s[5];
53     while(scanf("%d%d",&n,&m),n+m)
54     {
55         for(int i=0;i<30;i++){
56             in[i]=0;
57             V[i].clear();
58         }
59         certain=conflict=0;
60         for(int i=0;i<m;i++){
61             scanf("%s",s);
62             V[s[0]-'A'].push_back(s[2]-'A');
63             in[s[2]-'A']++;
64             if(!certain && !conflict) topo(i+1);
65         }
66         if(!certain && !conflict) puts("Sorted sequence cannot be determined.");
67     }
68 }

 

posted @ 2014-04-11 17:43  heaventouch  阅读(248)  评论(0编辑  收藏  举报