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. 使用自定义线程池优化EchoServer

    在上一篇文章中http://www.cnblogs.com/gosaint/p/8494423.html 我自定义了线程池ThreadPool.现在在我的EchoServer中使用自定义线程池去负责和 ...

  2. UITextField的文本框部分文本以*的方式来显示

    #import "AppDelegate.h" @interface AppDelegate ()<UITextFieldDelegate>// 添加代理协议 @end ...

  3. linux中的管道命令

    很有用的一个命令,用法如下: A | B 是把A命令的输出作为B命令的输入. 比如我想查看一下我在终端输入过的命令,可以这样: history | less

  4. 算法Sedgewick第四版-第1章基础-003一封装日期

    1. package ADT; import algorithms.util.StdOut; /**************************************************** ...

  5. R: 绘图 barplot

    问题:barplot 18.5.16 怎么绘制 barplot,用两种方式:基础绘图 & ggplot2解决方案: 基础绘图 barplot(height, width = 1, space ...

  6. WOJ 18 动态无向图

    一开始我是不会写的,后来点开了题解: 无话可说……那就写吧……然而第一发跑成暴力分,后来加了一个优化:就是在询问里面提到过的边都不用再加了. 然后……然后就过了呀…… 其实还有面向数据的编程的骚操作… ...

  7. 红帽企业版RHEL7.1在研域工控板上,开机没有登陆窗口 -- 编写xorg.conf 简单三行解决Ubuntu分辩率不可调的问题

    红帽企业版RHEL7.1在研域工控板上,开机没有登陆窗口 没有登陆窗口 的原因分析: 没有登陆窗口的原因是因为有多个屏幕在工作,其中一个就是build-in 屏幕(内置的虚拟屏幕)和外接的显示器,并且 ...

  8. Entity Framework Tutorial Basics(14):Choose development approach

    Choose development approach with Entity Framework: We have seen Code-first, Model-first and Database ...

  9. URLTester2.3.2

    文件: URLTester2.3.2.zip 大小: 1170KB 下载: 下载 URLTester是一个URL测试工具,最主要的一个特色是:当一个域名对应多个IP地址时,不用修改hosts文件,即可 ...

  10. win10开机时不显示锁屏壁纸

    win10开机壁纸存放在此目录下: C:\Users\%username%\AppData\Local\Packages\Microsoft.Windows.ContentDeliveryManage ...