Links:
http://acm.hdu.edu.cn/showproblem.php?pid=1811
The topic is Chinese.
Consider putting a person of the same rating in a collection. The person who can think of sorting by number can use and look up the set.
After preprocessing you can consider that there will be no two different rating appearing in the same set, which is contradictory otherwise you can build the edge with the root node of two sets.
After the diagram is built, you can guarantee that the rating of each point are the same. It is only necessary to determine whether there is a ring, topological sequence is unique.
The only way to judge the topological order is to have only one collection per queue, or there can be multiple choices.
#pragma COMMENT (linker, "/stack:10240000,10240000") #include <algorithm> #include <iostream> #include < sstream> #include <cstring> #include <cstdlib> #include <cstdio> #include <vector> #include <cmath> #include <queue> #include <set> #include <map> #define MOD 4294967296#define MAX 0x3f3f3f3f#define Lson o<<1, L, M#define Rson o<<1|1, m+1, R#define SZ (x) ((int) ans.size ()) #define MAKE make_p Air#define infl 0x3f3f3f3f3f3f3f3fll#define Mem (a) memset (a, 0, sizeof (a)) const double PI = ACOs ( -1.0); const double EPS = 1e-9;const int n = 10005;const int M = 20005;typedef Long Long ll;using namespace Std;int N, M, A[n], B[n];char s[n][3];in T he[m], du[n];struct C {int to, NE;} e[n];void Add (int id, int x, int y) {e[id].to = y; E[id].ne = He[x]; HE[X] = ID;} int f[n];int Find (int x) {return x = = F[x]? x:f[x] = Find (F[x]);} int main () {//freopen ("In.txt", "R", stdin); while (CIN >> n >> m) {memset (He,-1, sizeof (he)); Mem (du); int tot = n; for (int i = 1; I <= n; i++) f[i] = i; for (int i = 1; I <= m; i++) {scanf ("%d%s%d", &a[i], s[i], &b[i]); a[i]++, b[i]++; if (s[i][0] = = ' = ') {int fa = Find (A[i]); int fb = Find (B[i]); if (FA! = FB) {F[FA] = FB; tot--; }}} int ok1 = 0, ok2 = 0; for (int i = 1; I <= m; i++) {if (s[i][0] = = ' = ') continue; int fa = Find (A[i]); int fb = Find (B[i]); if (FA = = FB) {Ok1 = 1; Break } if (s[i][0] = = ' > ') {Add (i, FA, FB); du[fb]++; } else {Add (i, FB, FA); du[fa]++; }} if (Ok1) {puts ("CONFLICT"); Continue } queUE <int> Q; for (int i = 1; I <= n; i++) {if (i = = Find (i) && du[i] = = 0) {Q.push (i); }} while (!q.empty ()) {if (Q.size () > 1) ok2 = 1; int u = Q.front (); Q.pop (); tot--; for (int i = he[u]; i =-1; i = e[i].ne) {du[e[i].to]--; if (du[e[i].to] = = 0) q.push (e[i].to); }} if (tot >= 1) {puts ("CONFLICT"); } else if (OK2) {puts ("uncertain"); } else puts ("OK"); }return 0;}
HDU 1811 rank of Tetris (and set preprocessing + topological sequencing)