牛客Professional Manager(并查集)
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;
输入描述:
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
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(并查集)的更多相关文章
- P3043 [USACO12JAN]牛联盟Bovine Alliance(并查集)
P3043 [USACO12JAN]牛联盟Bovine Alliance 题目描述 Bessie and her bovine pals from nearby farms have finally ...
- P3043 [USACO12JAN]牛联盟Bovine Alliance——并查集
题目描述 给出n个点m条边的图,现把点和边分组,每条边只能和相邻两点之一分在一组,点可以单独一组,问分组方案数. (友情提示:每个点只能分到一条边,中文翻译有问题,英文原版有这样一句:The cows ...
- 牛客网-Beauty of Trees 【加权并查集】
锟斤拷锟接o拷https://www.nowcoder.com/acm/contest/119/A锟斤拷源锟斤拷牛锟斤拷锟斤拷 锟斤拷目锟斤拷锟斤拷 It锟斤拷s universally acknow ...
- 【并查集缩点+tarjan无向图求桥】Where are you @牛客练习赛32 D
目录 [并查集缩点+tarjan无向图求桥]Where are you @牛客练习赛32 D PROBLEM SOLUTION CODE [并查集缩点+tarjan无向图求桥]Where are yo ...
- 牛客练习赛16 C 任意点【并查集/DFS/建图模型】
链接:https://www.nowcoder.com/acm/contest/84/C 来源:牛客网 题目描述 平面上有若干个点,从每个点出发,你可以往东南西北任意方向走,直到碰到另一个点,然后才可 ...
- 2019牛客第八场多校 E_Explorer 可撤销并查集(栈)+线段树
目录 题意: 分析: @(2019牛客暑期多校训练营(第八场)E_Explorer) 题意: 链接 题目类似:CF366D,Gym101652T 本题给你\(n(100000)\)个点\(m(1000 ...
- 牛客网多校第4场 J Hash Function 【思维+并查集建边】
题目链接:戳这里 学习博客:戳这里 题意: 有n个空位,给一个数x,如果x%n位数空的,就把x放上去,如果不是空的,就看(x+1)%n是不是空的. 现在给一个已经放过数的状态,求放数字的顺序.(要求字 ...
- 并查集 牛客练习赛41 C抓捕盗窃犯
题目链接 :https://ac.nowcoder.com/acm/contest/373/C 题意,初始每一个城市都有一伙盗贼,没过一个时刻盗贼就会逃窜到另一个城市,你可以在m个城市设置监察站,会逮 ...
- 牛客网 牛客练习赛43 C.Tachibana Kanade Loves Review-最小生成树(并查集+Kruskal)+建虚点+读入挂
链接:https://ac.nowcoder.com/acm/contest/548/C来源:牛客网 Tachibana Kanade Loves Review 时间限制:C/C++ 2秒,其他语言4 ...
随机推荐
- struts2学习笔记(2)action多个方法的动态调用
①在struts.xml中的action添加method <action name="addhelloworld" method="add" class= ...
- 基于aspectj的aop注解操作
- 多线程 wait(),notify()方法,案例总结
废话不多说,案例如下 package com.xujingyang.Exok; /** * 商品类 * @author 徐景洋 */ public class Goods { private Stri ...
- SQL 连贯操作 [2]
1.alias 用于设置数据表别名 $user = M('User'); var_dump($user->alias('anothername')->select()); 这时在SQL中的 ...
- 用bat写的一个小病毒
最近看了一点bat的知识,具体说是看了一个博客:http://blog.csdn.net/qsyzb/article/details/17364581 用了三天才看完=.=,感觉作者整理整理可以把博客 ...
- Python03 字符串类型、强制类型转化、列表、元组、字典、集合
1 字符串类型 在python中字符串类型用str表示,字符串的连接用 + 1.1 创建字符串对象 ·创建一个字符串对象有两种方式,一种方式是直接用字符串进行赋值,另外一种是利用str类实例化对象:具 ...
- Angular07 利用angular打造管理系统页面
1 创建一个新的angular应用 ng new adminSystem 2 利用WebStorm打开adminSystem应用 3 借助AdminLTE这个开源项目来辅助开发 AdminLTE项目: ...
- C#调用C++类库的几种方式
1. 直接调用C++类库中的公共方法 使用DllImport特性对方法进行调用,比如一个C++类库SampleCppWrapper.dll中的公共方法: extern "C" _ ...
- Luogu 2149 [SDOI2009]Elaxia的路线
感觉这题可以模板化. 听说spfa死了,所以要练堆优化dijkstra. 首先对$x_{1},y_{1},x_{2},y_{2}$各跑一遍最短路,然后扫一遍所有边看看是不是同时在两个点的最短路里面,如 ...
- 使用 Bulma
一.起因 最近我在学习 SASS,通过它,可以将 CSS 像编程语言一样书写. 在最近之前,我又学习了 Flex 布局,用起来很方便. 所以,我学习了 Bulma 这个纯 CSS 框架--使用 Fle ...