码迷,mamicode.com
首页 > 其他好文 > 详细

[HDOJ2818]Building Block(带权并查集,路径压缩)

时间:2016-05-30 10:05:39      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

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

题意:有N个块,每次有两个操作:

M x y表示把x所在的那一堆全部移到y所在的那一堆的下方。

C x 询问在x下方有多少个方块。

用并查集,在路径压缩的时候后序更新当前块下有多少个其他块,注意这里“当前块下”其实和并查集是相反的,也就是父亲节点在儿子下面。

需要额外维护一个量:某一堆的块的总数,这个在unite操作的时候,如果不是同一个根,直接将一部分加到另一部分上就可以。

  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 lrt rt << 1
 54 #define rrt rt << 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 const int maxn = 30030;
 71 int pre[maxn];
 72 int under[maxn];
 73 int num[maxn];
 74 int p;
 75 
 76 int find(int x) {
 77     if(x == pre[x]) RT x;
 78     int px = pre[x];
 79     pre[x] = find(pre[x]);
 80     under[x] += under[px];
 81     RT pre[x];
 82 }
 83 
 84 void unite(int x, int y) {
 85     int fx = find(x);
 86     int fy = find(y);
 87     if(fx != fy) {
 88         under[fx] = num[fy];
 89         num[fy] += num[fx];
 90         pre[fx] = fy;
 91     }
 92 }
 93 
 94 int main() {
 95     // FRead();
 96     char cmd[5];
 97     int u, v;
 98     while(~Rint(p)) {
 99         Cls(under);
100         Rep(i, maxn) {
101             pre[i] = i;
102             num[i] = 1;
103         }
104         W(p) {
105             Rs(cmd);
106             if(cmd[0] == M) {
107                 Rint(u); Rint(v);
108                 unite(u, v);
109             }
110             if(cmd[0] == C) {
111                 Rint(u); find(u);
112                 printf("%d\n", under[u]);
113             }
114         }
115     }
116     RT 0;
117 }

 

[HDOJ2818]Building Block(带权并查集,路径压缩)

标签:

原文地址:http://www.cnblogs.com/vincentX/p/5541309.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!