UVA11987(带权并查集的删除操作)
2014-10-01 12:45
225 查看
Almost Union-Find
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=57812#problem/CI hope you know the beautiful Union-Find structure. In this problem, you're to implement something similar, but not identical.
The data structure you need to write is also a collection of disjoint sets, supporting 3 operations:
1 p q
Union the sets containing p and q. If p and q are already in the same set, ignore this command.
2 p q
Move p to the set containing q. If p and q are already in the same set, ignore this command
3 p
Return the number of elements and the sum of elements in the set containing p.
Initially, the collection contains n sets: {1}, {2}, {3}, ..., {n}.
Input
There are several test cases. Each test case begins with a line containing two integers n and m (1<=n,m<=100,000), the number of integers, and the number of commands. Each of the next m lines contains a command. For every operation, 1<=p,q<=n. The inputis terminated by end-of-file (EOF). The size of input file does not exceed 5MB.
Output
For each type-3 command, output 2 integers: the number of elements and the sum of elements.Sample Input
5 7 1 1 2 2 3 4 1 3 5 3 4 2 4 1 3 4 3 3
Output for the Sample Input
3 12 3 7 2 8
Explanation
Initially: {1}, {2}, {3}, {4}, {5}Collection after operation 1 1 2: {1,2}, {3}, {4}, {5}
Collection after operation 2 3 4: {1,2}, {3,4}, {5} (we omit the empty set that is produced when taking out 3 from {3})
Collection after operation 1 3 5: {1,2}, {3,4,5}
Collection after operation 2 4 1: {1,2,4}, {3,5}
Rujia Liu's Present 3: A Data Structure Contest Celebrating the 100th Anniversary of Tsinghua University
Special Thanks: Yiming Li
Note: Please make sure to test your program with the gift I/O files before submitting!
解题思路:
一道加权并查集的题,但在这基础上,加入了并查集的删除操作。题意是说n个节点进行m次操作,如果为1操作,
那么把a和b两个操作连接;如果为2操作,那么把a节点全部移到b上,即认b为祖先;如果为3,那么询问a节点所
属的树有多少结点,该树所有结点元素的总和为多少。i 节点的元素初值为 i 。
大体思路很像昨晚做的加权并查集。开sum记录该节点所在集合的元素总值,开cnt记录该节点所在集合有多少元
素。对于本道题涞说,难点是当执行2操作时,涉及到一个a节点连到b节点上的操作,这是要在a原来所属集合中删除a,
并在b集合中新增a,相应的cnt 和sum值都要做修改。
我们额外再开一个id数组,记录当前节点的编号,当执行2 a b , 把a的元素放到b上之前,把a做下处理。有种思
想是直接把a指向b,如果a是当前集合的叶子节点,那么这种思想是成立的,可是如果a是当前集合的非叶子节点,那么
当a指向b后必然会导致a的孩子也指向了b,所以这种思想是有局限性的。
我们可以额外再开一个节点记录a的信息,然后把a当前集合做处理(sum - a , cnt - 1),相当于把该集合的a节点滞
空。然后把新建立的节点连到b上,再对b进行处理(sum + sum[a] , cnt + 1)。
最后在询问时只要找到祖先,然后输出一下就好了。sum开成long long ,因为n给的1e5······微大······
完整代码:
#include <functional> #include <algorithm> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <numeric> #include <cstring> #include <climits> #include <cassert> #include <complex> #include <cstdio> #include <string> #include <vector> #include <bitset> #include <queue> #include <stack> #include <cmath> #include <ctime> #include <list> #include <set> #include <map> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") typedef long long LL; typedef double DB; typedef unsigned uint; typedef unsigned long long uLL; /** Constant List .. **/ //{ const int MOD = int(1e9)+7; const int INF = 0x3f3f3f3f; const LL INFF = 0x3f3f3f3f3f3f3f3fLL; const DB EPS = 1e-9; const DB OO = 1e20; const DB PI = acos(-1.0); //M_PI; char str[3]; const int maxn = 100001; int f[maxn]; long long sum[maxn]; int cnt[maxn]; int id[maxn]; int deep; int n; void init(int n) { for(int i = 0 ; i <= n ; i ++) { f[i] = i; sum[i] = i; id[i] = i; cnt[i] = 1; } deep = n; } int find(int x) { return x == f[x] ? x : (f[x] = find(f[x])); } void change(int x) { int ans = find(id[x]); sum[ans] -= x; cnt[ans] --; id[x] = ++deep; sum[id[x]] = x; cnt[id[x]] = 1; f[id[x]] = id[x]; } void unin(int a , int b) { int x = find(a); int y = find(b); f[x] = y; sum[y] += sum[x]; cnt[y] += cnt[x]; } int main() { #ifdef DoubleQ freopen("in.txt","r",stdin); #endif int m; int a , b; while(~scanf("%d%d",&n,&m)) { init(n); int key; for(int i = 0 ; i < m ; i++) { scanf("%d",&key); if(key == 1) { scanf("%d%d",&a,&b); if(find(id[a]) != find(id[b])) unin(id[a] , id[b]); } else if(key == 2) { scanf("%d%d",&a,&b); if(find(id[a]) != find(id[b])) { change(a); unin(id[a] , id[b]); } } else if(key == 3) { scanf("%d",&a); int t = find(id[a]); printf("%d %lld\n",cnt[t] , sum[t]); } } } }
相关文章推荐
- uva 11987 带删除操作的并查集与并查集的可持久化
- UVA 11987 Almost Union-Find 并查集的删除操作 懒惰标记
- UVA 11987 Almost Union-Find(带有删除操作的并查集)
- UVa-11987 Almost union-find(带删除操作的并查集)
- UVA 11987 Almost Union-Find (带权并查集的操作及并查集的删除操作)
- UVa 11987 Almost Union-Find(支持删除操作的并查集)
- uva 11987 Almost Union-Find(并查集的删除操作)
- UVA 11987 Almost Union-Find(有删除操作的并查集)
- uva 11987 Almost Union-Find(带删除操作的并查集)
- uva 11987 Almost Union-Find(带删除操作的并查集)
- UVA - 11987 Almost Union-Find(带删除的并查集)
- UVA 11987 Almost Union-Find(带删除操作的并查集)
- UVA 11987 Almost Union-Find 并查集节点删除
- UVA - 11987 Almost Union-Find[并查集 删除]
- UVA 11987 Almost Union-Find(并查集的删除)
- 11987 - Almost Union-Find(并查集删除操作)
- UVA 11987 Almost Union-Find(带删除的并查集)
- 并查集(删除) UVA 11987 Almost Union-Find
- UVA - 11987 Almost Union-Find(带删除的并查集)
- UVA11987 - Almost Union-Find (并查集带删除)