UVa 11987 - Almost Union-Find (并查集 需要一点技巧)

Time Limit: 1000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

[]   [Go Back]   [Status]  

Description

Download as PDF

Problem A

Almost Union-Find

I 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 input is 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!

Source

Root :: Prominent Problemsetters ::  Rujia Liu

[]   [Go Back]   [Status]  




题意:

维护一些不相交的集合,支持3总操作

1 p q 合并p q所在集合

2 p q 把元素p移动到q所在集合

3 p 输出p所在集合的元素个数和元素和



在普通并查集基础上,可以给每个元素添加一个附加属性,nowfa,表示该元素在2操作后的父亲(如果没有2操作nowfa[x] = -1)。

find(x)不变,直接按照fa[x]寻找。

新增一个FindRoot(x)操作变:

如果nowfa[x] != -1,说明x经过了2操作,则应该是find(nowfa[x])。否则是find(x)。




#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>

using namespace std;

//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif

#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define P64I1(a) printf(oform1, (a))
#define REP(i, n) for(int (i)=0; (i)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)

const int INF = 0x3f3f3f3f;
const double eps = 1e-9;
const double PI = (4.0*atan(1.0));

const int maxn = 100000 + 20;
int fa[maxn], size[maxn], sum[maxn];
int flag[maxn], nowfa[maxn];

int find(int x) {
    if(x == fa[x]) return x;
    return fa[x] = find(fa[x]);
}

void Union(int a, int b) {
    int pa = find(a);
    int pb = find(b);
    if(pa == pb) return ;
    if(size[pa] > size[pb]) swap(pa, pb);
    size[pb] += size[pa];
    size[pa] = 0;
    sum[pb] += sum[pa];
    sum[pa] = 0;
    fa[pa] = pb;
    return ;
}

int main() {
    int n, m;

    while(scanf("%d%d", &n, &m) != EOF) {
        memset(flag, 0, sizeof(flag));
        for(int i=1; i<=n; i++) fa[i] = i, sum[i] = i, size[i] = 1;
        while(m--) {
            int q;
            int a, b;
            scanf("%d", &q);
            if(q == 1) {
                scanf("%d%d", &a, &b);
                int pa, pb;
                if(flag[a]) pa = find(nowfa[a]);
                else pa = find(a);
                if(flag[b]) pb = find(nowfa[b]);
                else pb = find(b);
                Union(pa, pb);
            } else if(q == 2) {
                scanf("%d%d", &a, &b);
                int pa, pb;
                if(flag[a]) pa = find(nowfa[a]);
                else pa = find(a);
                if(flag[b]) pb = find(nowfa[b]);
                else pb = find(b);
                size[pa]--;
                size[pb]++;
                sum[pa] -= a;
                sum[pb] += a;
                flag[a] = 1;
                nowfa[a] = pb;
            } else {
                scanf("%d", &a);
                int pa;
                if(flag[a]) pa = find(nowfa[a]);
                else pa = find(a);
                printf("%d %d\n", size[pa], sum[pa]);
            }
        }
    }

    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值