poj1094 Sorting It All Out【floyd】【傳遞閉包】【拓撲序】

Sorting It All Out
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions:39731   Accepted: 13975

Descriptionnode

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.

Inputios

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.

Outputc++

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 Outputless

Sorted sequence determined after 4 relations: ABCD.
Inconsistency found after 2 relations.
Sorted sequence cannot be determined.

Sourceide

 

題意:this

給定關於n個字母的對應優先級關係,問是否能夠根據優先級對他們進行排序。而且要求輸出是在第幾組能夠得出結果。url

會有優先級不一致和沒法排序的狀況。spa

思路:.net

感受這題數據很迷啊,m的範圍都不給我都很差估計複雜度。

以及,要注意輸出時候的句號。特別是能夠sort的時候。

是一道傳遞閉包的問題,用鄰接矩陣建圖,若是$A<B$,則表示$A$到$B$有一條有向邊,$g['A']['B'] = 1$

每次添加一個關係,使用一次floyd計算可否得出結果,或者是否出現不一致。

輸出排序結果的時候,其實只須要比較每一個點的入度,按照入度從大到小排序就好了。

由於最後的矩陣是一個傳遞閉包,最小的那個字母確定小於其餘全部字母,也就是說他這一行會有$n-1$個$1$

 1 #include<iostream>
 2 //#include<bits/stdc++.h>
 3 #include<cstdio>
 4 #include<cmath>
 5 #include<cstdlib>
 6 #include<cstring>
 7 #include<algorithm>
 8 #include<queue>
 9 #include<vector>
 10 #include<set>
 11 #include<climits>
 12 using namespace std;  13 typedef long long LL;  14 #define N 100010
 15 #define pi 3.1415926535
 16 #define inf 0x3f3f3f3f
 17 
 18 int n, m;  19 int g[30][30], tmpg[30][30];  20 
 21 int floyd()  22 {  23     for(int i = 0; i < n; i++){  24         for(int j = 0; j < n; j++){  25             tmpg[i][j] = g[i][j];  26  }  27  }  28     for(int k = 0; k < n; k++){  29         for(int i = 0; i < n; i++){  30             for(int j = 0; j < n; j++){  31                 tmpg[i][j] |= tmpg[i][k] & tmpg[k][j];  32                 if(tmpg[i][j] == tmpg[j][i] && tmpg[i][j] == 1 && i != j){  33                     return -1;  34  }  35  }  36  }  37  }  38     for(int i = 0; i < n; i++){  39         for(int j = 0; j < n; j++){  40             if(tmpg[i][j] == tmpg[j][i] && tmpg[i][j] == 0 && i != j){  41                 return 0;  42  }  43  }  44  }  45     return 1;  46 }  47 
 48 struct node{  49     int deg;  50     char ch;  51 };  52 bool cmp(node a, node b)  53 {  54     return a.deg > b.deg;  55 }  56 
 57 void print()  58 {  59     //int deg[30];  60     //memset(deg, 0, sizeof(deg));
 61     node character[30];  62     for(int i = 0; i < n; i++){  63         character[i].deg = 0;  64         character[i].ch = 'A' + i;  65  }  66     for(int i = 0; i < n; i++){  67         for(int j = 0; j < n; j++){  68             if(tmpg[i][j]){  69                 character[i].deg++;  70  }  71  }  72  }  73     sort(character, character + n, cmp);  74     for(int i = 0; i < n; i++){  75         printf("%c", character[i].ch);  76  }  77     printf(".\n");  78 
 79     /*queue<int>que;  80  for(int i = 0; i < n; i++){  81  printf("%d\n", deg[i]);  82  if(!deg[i]){  83  que.push(i);  84  break;  85  }  86  }  87  for(int i = 0; i < n; i++){  88  int x = que.front();que.pop();  89  printf("%c", x + 'A');  90  for(int k = 0; k < n; k++){  91  if(tmpg[k][x])deg[k]--;  92  if(!deg[k])que.push(k);  93  }  94  }*/
 95 }  96 
 97 int main()  98 {  99     while(scanf("%d%d", &n, &m) != EOF && n || m){ 100         memset(g, 0, sizeof(g)); 101         /*for(int i = 0; i < n; i++){ 102  g[i][i] = 1; 103  }*/
104 
105         int i; 106         bool dont = false; 107         for(i = 1; i <= m; i++){ 108             char a, b; 109  getchar(); 110             scanf("%c<%c", &a, &b); 111             g[a - 'A'][b - 'A'] = 1; 112             if(!dont){ 113                  int flag = floyd(); 114                 if(flag == -1){ 115                     printf("Inconsistency found after %d relations.\n", i); 116                     dont = true; 117  } 118                 else if(flag == 1){ 119                     printf("Sorted sequence determined after %d relations: ", i); 120  print(); 121                     dont = true; 122  } 123  } 124  } 125         if(i > m && !dont){ 126             printf("Sorted sequence cannot be determined.\n"); 127  } 128  } 129     return 0; 130 }
相關文章
相關標籤/搜索