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. 安装完Ubuntu 14.04后的几件事

    周末折腾一下Ubuntu 14.04,稍作记录: 1. 切换源,我还是钟情163的(ps, 这里如果用的以前13.04时候163的源会有问题,记得配置最新的http://mirrors.163.com ...

  2. sql语句中的问号是干什么的???

    1.实例代码一: String sql = "SELECT userid,name FROM tuser WHERE userid=? AND password=?" ;  pst ...

  3. render tree与css解析

    浏览器在构造DOM树的同时也在构造着另一棵树-Render Tree,与DOM树相对应暂且叫它Render树吧,我们知道DOM树为javascript提供了一些列的访问接口(DOM API),但这棵树 ...

  4. TextView中ellipsize属性

    TextView中可以设置一个ellipsize属性,作用是当文字长度超过textview宽度时的显示方式: 例如,"encyclopedia"显示, 只是举例,以实际显示为准:) ...

  5. day17-jdbc 7.Statement介绍

    SQL语句:DML.DQL.DCL.DDL.DML和DQL是用的最多的.DCL和DDL用的很少. 程序员一般是操作记录,创建一表很少. package cn.itcast.jdbc; import c ...

  6. phper 需要学习js

  7. nginx负载均衡, 配置地址带端口

    nginx.conf  配置如下: upstream wlcf.dev.api { server 127.0.0.1:8833; server 127.0.0.2:8833; } server { l ...

  8. JavaWeb面试题 有用

    ajax的原理简单来说通过XmlHttpRequest对象来向服务器发异步请求,从服务器获取数据,然后用JavaScript来操作DOM从而更新页面的局部显示. Ajax的优点: 1.最大的一点是页面 ...

  9. Luogu 4281 [AHOI2008]紧急集合 / 聚会

    BZOJ 1832 写起来很放松的题. 首先发现三个点在树上一共只有$3$种形态,大概长这样: 这种情况下显然走到三个点的$lca$最优. 这种情况下走到中间那个点最优. 这种情况下走到$2$最优. ...

  10. Luogu 2312 [NOIP2014] 解方程

    感觉好无聊. 秦九昭算法:一般地,一元n次多项式的求值需要经过(n+1)*n/2次乘法和n次加法,而秦九韶算法只需要n次乘法和n次加法.在人工计算时,一次大大简化了运算过程.(百度百科) 具体来说怎么 ...