拓扑序列变形 之 poj 1094 Sorting It All Out


/*
拓扑序列变形 之 poj 1094 Sorting It All Out
 
变形:
	在每消去唯一一个入度为0的点后,只剩下唯一一个入度为0的点。
	这样获得的n个点才是排序好的。
*/
  1 #include <iostream>
  2 #include <cstdlib>
  3 #include <cstdio>
  4 #include <cstddef>
  5 #include <iterator>
  6 #include <algorithm>
  7 #include <string>
  8 #include <locale>
  9 #include <cmath>
 10 #include <vector>
 11 #include <cstring>
 12 #include <map>
 13 #include <utility>
 14 #include <queue>
 15 #include <stack>
 16 #include <set>
 17 #include <functional>
 18 using namespace std;
 19 typedef pair<int, int> PII; 
 20 typedef long long int64;
 21 const int INF = 0x3f3f3f3f;
 22 const int modPrime = 3046721;
 23 const double eps = 1e-9;
 24 const int MaxN = 30;
 25 const int MaxM = 10010;
 26 
 27 int n, m;
 28 bool G[MaxN][MaxN];
 29 int ndCnt[MaxN];
 30 char chList[MaxN];
 31 
 32 enum emFlag
 33 {
 34     fgSUCCESS, fgFAIL, fgWAIT
 35 };
 36 
 37 
 38 emFlag Solve()
 39 {
 40     stack<int> stk;
 41 
 42     int ndCntTmp[MaxN];
 43     for (int i = 0; i < n; ++i)
 44     {
 45         ndCntTmp[i] = ndCnt[i];
 46         if (0 == ndCnt[i])
 47         {
 48             stk.push(i);
 49         }
 50     }
 51 
 52     bool sign = true;
 53     int chLen = 0;
 54 
 55     while (!stk.empty())
 56     {
 57         // 下面的if语句,就是需要注意的地方
 58         if (stk.size() > 1)
 59         {
 60             sign = false;
 61         }
 62         int nd = stk.top();
 63         stk.pop();
 64         chList[chLen++] = static_cast<char> ('A' + nd);
 65         for (int i = 0; i < n; ++i)
 66         {
 67             if (G[nd][i])
 68             {
 69                 --ndCntTmp[i];
 70                 if (0 == ndCntTmp[i])
 71                 {
 72                     stk.push(i);
 73                 }
 74             }
 75         }
 76     }
 77 
 78     if (n == chLen)
 79     {
 80         if (sign)
 81         {
 82             return fgSUCCESS;
 83         }
 84         else
 85         {
 86             return fgWAIT;
 87         }
 88     }
 89     else
 90     {
 91         return fgFAIL;
 92     }
 93 }
 94 
 95 void ini()
 96 {
 97     for (int i = 0; i < n; ++i)
 98     {
 99         ndCnt[i] = 0;
100         for (int j = 0; j < n; ++j)
101         {
102             G[i][j] = false;
103         }
104     }
105 }
106 
107 int main()
108 {
109 #ifdef HOME
110     freopen("in", "r", stdin);
111     //freopen("out", "w", stdout);
112 #endif
113     string str;
114     while ((cin >> n >> m) && (n || m))
115     {
116         ini();
117 
118         bool sign = false;
119         int pos;
120         emFlag fg = fgWAIT;
121         for (int i = 1; i <= m; ++i)
122         {
123             cin >> str;
124             if (!sign)
125             {
126                 int a = str[0] - 'A', b = str[2] - 'A';
127                 if (!G[a][b])
128                 {
129                     G[a][b] = true;
130                     ++ndCnt[b];
131                     fg = Solve();
132                     if (fg != fgWAIT)
133                     {
134                         pos = i;
135                         sign = true;
136                     }
137                 }
138             }
139         }
140         if (sign)
141         {
142             if (fgSUCCESS == fg)
143             {
144                 cout << "Sorted sequence determined after " << pos << " relations: ";
145                 for (int j = 0; j < n; ++j)
146                 {
147                     cout << chList[j];
148                 }
149                 cout << "." << endl;
150             }
151             else
152             {
153                 cout << "Inconsistency found after " << pos << " relations." << endl;
154             }
155         }
156         else
157         {
158             cout << "Sorted sequence cannot be determined." << endl;
159         }
160     }
161 
162 #ifdef HOME
163     cerr << "Time elapsed: " << clock() / CLOCKS_PER_SEC << " ms" << endl;
164     _CrtDumpMemoryLeaks();
165 #endif
166     return 0;
167 }

 

 
 
posted @ 2015-12-18 03:38  JmingS  阅读(195)  评论(0编辑  收藏  举报