t’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
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

-->

示例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 这是一道有点宇宙不同的并查集模板题,与其他不同的是多了个删除操作,那么我们该怎么做呢? 可以想到的是直接把要删除的点的父亲结点改成是自己,但是,如果这个点有子节点怎么办? 很显然,这种方法就无法做了,
我们可以给每个点都给他们一个编号,删除点,就相当于该点的编号改变就可以了,也是相当于构造新点
AC代码
#include<stdio.h>
int n,f[],d[],h[],now;
///f为并查集,d为编号,h为高度
int findd(int x)///查找父节点
{
if(f[x]!=x)
f[x]=findd(f[x]);
return f[x]; }
void init ()
{
for(int k= ;k<=n ;k++)
{
f[k]=k;
d[k]=k;
h[k]=;
}
}
///合并
void HB(int u,int v)
{
u=d[u],v=d[v];
u=findd(u),v=findd(v);
if(u==v)
return ;
f[u]=v;
h[v]+=h[u];
}
///删除
void SC(int u)
{
int v;
v=d[u];
v=findd(v);
h[v]--;
++now;
d[u]=now;
f[now]=now;h[now]=;
} int main()
{
int t,i,q,x,v,u;
scanf("%d",&t);
for(i= ;i<=t; i++)
{
printf("Case #%d:\n",i);
scanf("%d%d",&n,&q);
now=n;
init();
while(q--)
{
scanf("%d",&x);
if(x==)
{
scanf("%d%d",&u,&v);
HB(u,v); }
if(x==)
{
scanf("%d",&u);
SC(u); }
if(x==)
{
scanf("%d",&u);
u=d[u];u=findd(u); printf("%d\n",h[u]); }
if(x==)
{
scanf("%d%d",&u,&v);
u=d[u];v=d[v];
u=findd(u);v=findd(v);
if(u==v)
printf("YES\n");
else
printf("NO\n");
} }
}
return ;
}

牛客Professional Manager(并查集)的更多相关文章

  1. P3043 [USACO12JAN]牛联盟Bovine Alliance(并查集)

    P3043 [USACO12JAN]牛联盟Bovine Alliance 题目描述 Bessie and her bovine pals from nearby farms have finally ...

  2. P3043 [USACO12JAN]牛联盟Bovine Alliance——并查集

    题目描述 给出n个点m条边的图,现把点和边分组,每条边只能和相邻两点之一分在一组,点可以单独一组,问分组方案数. (友情提示:每个点只能分到一条边,中文翻译有问题,英文原版有这样一句:The cows ...

  3. 牛客网-Beauty of Trees 【加权并查集】

    锟斤拷锟接o拷https://www.nowcoder.com/acm/contest/119/A锟斤拷源锟斤拷牛锟斤拷锟斤拷 锟斤拷目锟斤拷锟斤拷 It锟斤拷s universally acknow ...

  4. 【并查集缩点+tarjan无向图求桥】Where are you @牛客练习赛32 D

    目录 [并查集缩点+tarjan无向图求桥]Where are you @牛客练习赛32 D PROBLEM SOLUTION CODE [并查集缩点+tarjan无向图求桥]Where are yo ...

  5. 牛客练习赛16 C 任意点【并查集/DFS/建图模型】

    链接:https://www.nowcoder.com/acm/contest/84/C 来源:牛客网 题目描述 平面上有若干个点,从每个点出发,你可以往东南西北任意方向走,直到碰到另一个点,然后才可 ...

  6. 2019牛客第八场多校 E_Explorer 可撤销并查集(栈)+线段树

    目录 题意: 分析: @(2019牛客暑期多校训练营(第八场)E_Explorer) 题意: 链接 题目类似:CF366D,Gym101652T 本题给你\(n(100000)\)个点\(m(1000 ...

  7. 牛客网多校第4场 J Hash Function 【思维+并查集建边】

    题目链接:戳这里 学习博客:戳这里 题意: 有n个空位,给一个数x,如果x%n位数空的,就把x放上去,如果不是空的,就看(x+1)%n是不是空的. 现在给一个已经放过数的状态,求放数字的顺序.(要求字 ...

  8. 并查集 牛客练习赛41 C抓捕盗窃犯

    题目链接 :https://ac.nowcoder.com/acm/contest/373/C 题意,初始每一个城市都有一伙盗贼,没过一个时刻盗贼就会逃窜到另一个城市,你可以在m个城市设置监察站,会逮 ...

  9. 牛客网 牛客练习赛43 C.Tachibana Kanade Loves Review-最小生成树(并查集+Kruskal)+建虚点+读入挂

    链接:https://ac.nowcoder.com/acm/contest/548/C来源:牛客网 Tachibana Kanade Loves Review 时间限制:C/C++ 2秒,其他语言4 ...

随机推荐

  1. 封装 Toast

    一. ToastView.java 1 import android.content.Context; import android.view.LayoutInflater; import andro ...

  2. 【转】webService概述

    一.序言: 大家或多或少都听过WebService(Web服务),有一段时间很多计算机期刊.书籍和网站都大肆的提及和宣传WebService技术,其中不乏很多吹嘘和做广告的成分.但是不得不承认的是We ...

  3. GitHub Blog创建以及本地管理(转)

    GitHub Blog创建以及本地管理   创建 注册GitHub账户 首页点击新建仓库 New repository repository name必须为 Owner.github.io EX:我的 ...

  4. 202. Happy Number 平方循环数

    [抄题]: Write an algorithm to determine if a number is "happy". A happy number is a number d ...

  5. Amazon S3 云服务

    一.简介 Amazon Simple Storage Service (S3) 是一个公开的服务,Web 应用程序开发人员可以使用它存储数字资产,包括图片.视频.音乐和文档. S3 提供一个 REST ...

  6. Luogu 3302 [SDOI2013]森林

    BZOJ 3123 丑陋的常数,BZOJ 19968ms 卡过. 感觉几个思想都挺经典但是挺套路的. 先考虑不连边的情况怎么做,区间第$k$小,想到主席树,每一个结点维护它到根的前缀和,这样子每一次查 ...

  7. java 中一个char包含几个字节

    背景   char包含几个字节可能记得在上学的时候书上写的是2个字节,一直没有深究,今天我们来探究一下到底一个char多少个字节? Char   char在设计之初的时候被用来存储字符,可是世界上有那 ...

  8. eclipse中jad反编译工具的安装

    我的云盘:工具里面有 Q:为什么有必要在开发环境中配置反编译工具呢? A:  当运行引用了第三方jar包项目时,突然报出了jar包中的某个类的某一行出现异常.我们想看一下这个class文件的代码时,经 ...

  9. Linux下面的IO模型

    1. Linux下的五种I/O模型 阻塞I/O模型: 一直阻塞      应用程序调用一个IO函数,导致应用程序阻塞,等待数据准备好. 如果数据没有准备好,一直等待….数据准备好了,从内核拷贝到用户空 ...

  10. 通过ADB命令行卸载或删除你的Android设备中的应用(转载)

    转自:http://mytiankong.com/?p=11755 如果你对你的Android设备在与命令行的交互间有一定的兴趣,那你可能想学习一些使用ADB卸载设备中已安装应用的技巧.为了使这种方法 ...