拓扑序列变形 之 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 }
 
 
时间: 2024-09-29 03:26:52

拓扑序列变形 之 poj 1094 Sorting It All Out的相关文章

poj 1094 Sorting It All Out 解题报告

题目链接:http://poj.org/problem?id=1094 题目意思:给出 n 个待排序的字母 和 m 种关系,问需要读到第 几 行可以确定这些字母的排列顺序或者有矛盾的地方,又或者虽然具体的字母顺序不能确定但至少不矛盾.这些关系均是这样的一种形式: 字母1 < 字母2 这道题目属于图论上的拓扑排序,由于要知道读入第几行可以确定具体的顺序,所以每次读入都需要进行拓扑排序来检验,这时每个点的入度就需要存储起来,所以就有了代码中memcpy 的使用了. 拓扑排序的思路很容易理解,但写起来

POJ 1094 Sorting It All Out(拓扑排序判环)

题目链接:http://poj.org/problem?id=1094 题目: 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

poj 1094 Sorting It All Out[ topo]

传送门:http://poj.org/problem?id=1094 Sorting It All Out 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 sequen

[ACM] POJ 1094 Sorting It All Out (拓扑排序)

Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26801   Accepted: 9248 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 sm

POJ 1094 Sorting It All Out(拓扑排序&#183;判断+实现)

题意  由一些不同元素组成的升序序列是可以用若干个小于号将所有的元素按从小到大的顺序 排列起来的序列.例如,排序后的序列为 A, B, C, D,这意味着 A < B.B < C和C < D.在本题中, 给定一组形如 A < B的关系式,你的任务是判定是否存在一个有序序列. 输出到哪一项可以确定顺序或者在这一项最先出现冲突,若所有的小于关系都处理完了都不能确定顺序也没有出现冲突,就输出不能确定 每来一个小于关系就进行一次拓扑排序  直到出现冲突(也就是出现了环)或者已经能确定顺序

POJ 1094 Sorting It All Out【floyd传递闭包+拓扑排序】

Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 31994 Accepted: 11117 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 small

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

Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 27929   Accepted: 9655 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 sm

POJ 1094 Sorting It All Out 拓扑排序

Sorting It All Out Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & %llu 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

poj 1094 Sorting It All Out 拓补排序

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.