第十四届华科校赛 C-Professional Manager(并查集的拆分)

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
 64bit IO Format: %lld


     It’s universally acknowledged that there’re innumerable trees in the campus of HUST.               Thus a professional tree manager is needed. Your task is to write a program to help manage the trees.        Initially, there are n forests and for the i-th forest there is only the i-th tree in it. Given four kinds of operations.      1 u v, merge the forest containing the u-th tree and the forest containing the v-th tree;    2 u, separate the u-th tree from its forest;    3 u, query the size of the forest which contains the u-th tree;    4 u v, query whether the u-th tree and the v-th tree are in the same forest.
输入描述:
The first line contains an integer T, indicating the number of testcases.In each test case:The first line contains two integers N and Q, indicating the number of initial forests and the number of operations.Then Q lines follow, and each line describes an operation.
输出描述:
For each test cases, the first line should be "Case #i:", where i indicate the test case i.For each query 3, print a integer in a single line.For each query 4, print "YES" or "NO" in a single line.

示例1

输入
1
10 8
3 1
4 1 2
1 1 2
3 1
4 1 2
2 1
3 1
4 1 2

输出
Case #1:
1
NO
2
YES
1
NO

题目大意:有n棵树,编号为1~n,接下来又4种操作:1、合并树u和树v为一片森林。2、把树u和原来的森林分开。3、查询树u所在的森林的树的总数。4、查询树u和数v是否在一片森林。

解题思路:从操作来看很容易想到并查集的做法,但是对于操作2,我们似乎感觉到不太好实现,如果不是根节点,我们就直接root[a] = a就可以了,然而根节点本来就是指向它自己,要是根节点也有子节点的性质就好了,所以我们不妨给每个树开始都创建一个根节点,这个虚构的根节点不计入总数即可,这样我们就很轻松的解决了并查集的拆分问题。

AC代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<queue>
#include<map>
using namespace std;

int T, n, q, k;
int root[300010], sum[300010], top;
//有100000次操作,我们再多开100000

void formalset(int n)
{
    for(int i = 1; i <= n; i ++) {
        root[i] = i+n; //创建每棵树的根节点,我们编号为n+1 ~ 2*n
        sum[i] = 0; //这个其实初不初始化也无所谓,反正也用不到
    }
    for(int i = n+1; i <= 2*n; i ++) {
        root[i] = i; //虚构的根节点指向它自己
        sum[i] = 1;
    }
}

int find_root(int x)
{
    if(root[x] != x)
        root[x] = find_root(root[x]);
    return root[x];
}

void merge(int x, int y)
{
    if(find_root(x) == find_root(y))
        return;
    int a = find_root(x);
    int b = find_root(y);
    root[b] = a;
    sum[a] += sum[b];
}

bool issame(int x, int y)
{
    if(find_root(x) == find_root(y))
        return true;
    return false;
}

void del(int x)
{
    if(sum[find_root(x)] > 1) //大于一棵树的森林才减
        sum[find_root(x)] --;
    root[x] = top; //对于删除的树,我们在再虚构一个根节点
    root[top] = top;
    sum[top++] = 1;
}

int main()
{
    int u, v, s;
    int k = 1;
    scanf("%d", &T);
    while(T --) {
        printf("Case #%d:\n", k++);
        memset(sum, 0, sizeof(sum));
        scanf("%d%d", &n, &q);
        formalset(n);
        top = n*2+1;
        while(q --) {
            scanf("%d", &s);
            if(s == 1) {
                scanf("%d%d", &u, &v);
                merge(u, v);
            }
            if(s == 2) {
                scanf("%d", &u);
                del(u);
            }
            if(s == 3) {
                scanf("%d", &u);
                printf("%d\n", sum[find_root(u)]);
            }
            if(s == 4) {
                scanf("%d%d", &u, &v);
                if(issame(u, v))
                printf("YES\n");
                else printf("NO\n");
            }
        }
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值