[HDOJ1811]Rank of Tetris(并查集拓扑排序)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[HDOJ1811]Rank of Tetris(并查集拓扑排序)相关的知识,希望对你有一定的参考价值。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1811

求一堆数据的拓扑序。

处理:x>y就是x到y一条边,x<y就是y到x一条边。关键问题是处理x=y的情况。

假如x=y,就有问题了。假如不处理的话,可能会被当成少处理一个点而使结果编程UNCERTAIN。所以我们考虑用并查集来解决这个问题。

选谁当祖先?题中又给了一个其他的量叫做RP值,这个RP值的规律是序号越大RP值越大。这样我们可以在合并的时候,尽可能地将RP值大的数当成本集合的祖先。

  1 /*
  2 ━━━━━┒ギリギリ♂ eye!
  3 ┓┏┓┏┓┃キリキリ♂ mind!
  4 ┛┗┛┗┛┃\○/
  5 ┓┏┓┏┓┃ /
  6 ┛┗┛┗┛┃ノ)
  7 ┓┏┓┏┓┃
  8 ┛┗┛┗┛┃
  9 ┓┏┓┏┓┃
 10 ┛┗┛┗┛┃
 11 ┓┏┓┏┓┃
 12 ┛┗┛┗┛┃
 13 ┓┏┓┏┓┃
 14 ┃┃┃┃┃┃
 15 ┻┻┻┻┻┻
 16 */
 17 #include <algorithm>
 18 #include <iostream>
 19 #include <iomanip>
 20 #include <cstring>
 21 #include <climits>
 22 #include <complex>
 23 #include <fstream>
 24 #include <cassert>
 25 #include <cstdio>
 26 #include <bitset>
 27 #include <vector>
 28 #include <deque>
 29 #include <queue>
 30 #include <stack>
 31 #include <ctime>
 32 #include <set>
 33 #include <map>
 34 #include <cmath>
 35 using namespace std;
 36 #define fr first
 37 #define sc second
 38 #define cl clear
 39 #define BUG puts("here!!!")
 40 #define W(a) while(a--)
 41 #define pb(a) push_back(a)
 42 #define Rint(a) scanf("%d", &a)
 43 #define Rll(a) scanf("%lld", &a)
 44 #define Rs(a) scanf("%s", a)
 45 #define Cin(a) cin >> a
 46 #define FRead() freopen("in", "r", stdin)
 47 #define FWrite() freopen("out", "w", stdout)
 48 #define Rep(i, len) for(int i = 0; i < (len); i++)
 49 #define For(i, a, len) for(int i = (a); i < (len); i++)
 50 #define Cls(a) memset((a), 0, sizeof(a))
 51 #define Clr(a, x) memset((a), (x), sizeof(a))
 52 #define Full(a) memset((a), 0x7f7f, sizeof(a))
 53 #define lp p << 1
 54 #define rp p << 1 | 1
 55 #define pi 3.14159265359
 56 #define RT return
 57 #define lowbit(x) x & (-x)
 58 #define onenum(x) __builtin_popcount(x)
 59 typedef long long LL;
 60 typedef long double LD;
 61 typedef unsigned long long ULL;
 62 typedef pair<int, int> pii;
 63 typedef pair<string, int> psi;
 64 typedef map<string, int> msi;
 65 typedef vector<int> vi;
 66 typedef vector<LL> vl;
 67 typedef vector<vl> vvl;
 68 typedef vector<bool> vb;
 69 
 70 typedef struct Edge {
 71     int u, v, next;
 72     Edge() {}
 73     Edge(int uu, int vv) : u(uu), v(vv) { next = -1; }
 74 }Edge;
 75 const int maxn = 10010;
 76 const int maxm = 20020;
 77 int n, m;
 78 Edge edge[maxm];
 79 int head[maxn];
 80 int hcnt;
 81 int pre[maxn];
 82 int in[maxn];
 83 int num, q[maxn], front, tail;
 84 int uu[maxn], vv[maxn];
 85 char cc[maxn][5];
 86 
 87 void adde(int u, int v) {
 88     edge[hcnt] = Edge(u, v);
 89     edge[hcnt].next = head[u];
 90     head[u] = hcnt++;
 91     in[v]++;
 92 }
 93 
 94 int find(int x) {
 95     RT x == pre[x] ? x : pre[x] = find(pre[x]);
 96 }
 97 
 98 int unite(int x, int y) {
 99     int fx = find(x);
100     int fy = find(y);
101     if(fx == fy) RT 0;
102     if(x > y) pre[fy] = fx;
103     else pre[fx] = fy;
104     return 1;
105 
106 }
107 
108 int topo() {
109     front = tail = 0;
110     Rep(i, n) {
111         if(in[i] == 0 && i == find(i)) {
112             q[tail++] = i;
113         }
114     }
115     int ret = 0;
116     while(front < tail) {
117         if(tail - front > 1) ret = 1;
118         int u = q[front++];
119         --num;
120         for(int i = head[u]; ~i; i=edge[i].next) {
121             int v = edge[i].v;
122             if(--in[v] == 0) {
123                 q[tail++] = v;
124             }
125         }
126     }
127     if(num > 0) printf("CONFLICT\n");
128     else if(ret) printf("UNCERTAIN\n");
129     else printf("OK\n");
130 }
131 
132 int main() {
133     // FRead();
134     int u, v;
135     while(~Rint(n) && ~Rint(m)) {
136         Cls(in); Clr(head, -1); hcnt = 0;
137         num = n;
138         Rep(i, n+5) pre[i] = i;
139         Rep(i, m) {
140             Rint(uu[i]); Rs(cc[i]); Rint(vv[i]);
141             if(cc[i][0] == =) {
142                 if(unite(uu[i], vv[i])) num--;
143             }
144         }
145         Rep(i, m) {
146             if(cc[i][0] != =) {
147                 u = find(uu[i]); v = find(vv[i]);
148                 if(cc[i][0] == >) adde(u, v);
149                 if(cc[i][0] == <) adde(v, u);
150             }
151         }
152         topo();
153     }
154     RT 0;
155 }

 

以上是关于[HDOJ1811]Rank of Tetris(并查集拓扑排序)的主要内容,如果未能解决你的问题,请参考以下文章

Hdu 1811 Rank of Tetris

HDU 1811 Rank of Tetris ??????????????? + ????????????

HDU 1811:Rank of Tetris(并查集+拓扑排序)

HDU 1811 Rank of Tetris(拓扑排序+并查集)

hdu 1811 Rank of Tetris 并查集+拓扑排序

HDU 1811:Rank of Tetris(并查集+拓扑排序)