题意:

给出四种操作:

1. 合并u,v两棵树

2. 从u所在的集合中删除u

3. 询问u所在集合有多少颗树

4. 询问 u,v是否在同一个集合

分析:

对于删除操作, 只要新开一个点代替原来的点即可。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5e4;
int f[maxn], cnt[maxn], id[maxn];
//id是负责记录这个点是原来的点还是删除操作后新开的点
void init(){
for(int i = ; i < maxn; i++){
f[i] = i;
cnt[i] = ;
id[i] = i;
}
}
int find(int x){
if(x == f[x]) return x;
else {
return f[x] = find(f[x]);
}
}
void merge(int u, int v){
int a = find(u);
int b = find(v);
if(a != b){
f[b] = a;
cnt[a] += cnt[b];
cnt[b] = ;
}
}
int n, q;
int main(){
// freopen("1.txt","r", stdin);
ios::sync_with_stdio(false);
int T;
cin >> T;
for(int kase = ; kase <= T; kase++){
cout << "Case #" << kase << ":\n";
init();
cin >> n >> q;
int tot = n;
for(int i = ; i < q; i++){
int op, u, v;
cin >> op;
switch(op){
case :{
cin >> u >> v;
merge(id[u],id[v]);
break;
}
case :{//删除操作,另开一个点代替原来的点
cin >> u;
int p = find(id[u]);
cnt[p]--;
id[u] = ++tot;
// f[id[u]] = id[u];
break;
}
case :{
cin >> u;
cout << cnt[find(id[u])] << "\n";
break;
}
case :{
cin >> u >> v;
if(find(id[u]) == find(id[v])){
cout << "YES\n";
}else cout << "NO\n";
}
}
}
}
return ;
}

Nowcoder 106 C.Professional Manager(统计并查集的个数)的更多相关文章

  1. 牛客Professional Manager(并查集)

    t’s universally acknowledged that there’re innumerable trees in the campus of HUST.  Thus a professi ...

  2. 第十四届华中科技大学程序设计竞赛 C Professional Manager【并查集删除/虚点】

    题目描述 It's universally acknowledged that there're innumerable trees in the campus of HUST. Thus a pro ...

  3. HDU1865--More is better(统计并查集的秩(元素个数))

    More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others) ...

  4. 并查集 Union-Find

    并查集能做什么? 1.连接两个对象; 2.查询两个对象是否在一个集合中,或者说两个对象是否是连接在一起的. 并查集有什么应用? 1. Percolation问题. 2. 无向图连通子图个数 3. 最近 ...

  5. Luogu P3295 [SCOI2016]萌萌哒(并查集+倍增)

    P3295 [SCOI2016]萌萌哒 题面 题目描述 一个长度为 \(n\) 的大数,用 \(S_1S_2S_3 \cdots S_n\) 表示,其中 \(S_i\) 表示数的第 \(i\) 位, ...

  6. hdu1213 并查集

    题意:有 n 个朋友,他们可能相互认识,A 认识 B,B 认识 C,则 ABC 相互认识,现在给出他们的认识情况,相互认识的人坐一桌,否则需要分开坐,问至少需要多少桌. 其实就是问并查集的个数,在初始 ...

  7. 并查集(Union Find)的基本实现

    概念 并查集是一种树形的数据结构,用来处理一些不交集的合并及查询问题.主要有两个操作: find:确定元素属于哪一个子集. union:将两个子集合并成同一个集合. 所以并查集能够解决网络中节点的连通 ...

  8. 树上统计treecnt(dsu on tree 并查集 正难则反)

    题目链接 dalao们怎么都写的线段树合并啊.. dsu跑的好慢. \(Description\) 给定一棵\(n(n\leq 10^5)\)个点的树. 定义\(Tree[L,R]\)表示为了使得\( ...

  9. HDU 5441 Travel(并查集+统计节点个数)

    http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给出一个图,每条边有一个距离,现在有多个询问,每个询问有一个距离值d,对于每一个询问,计算出有多少点 ...

随机推荐

  1. mysql用户和授权

    CREATE USER 'monitor'@'10.224.32.%' IDENTIFIED BY '123@abAB'; mysql> GRANT select,insert,update O ...

  2. docker监控之cadvisor

    docker run -d \ --volume=/:/rootfs:ro \ --volume=/var/run:/var/run:rw \ --volume=/sys:/sys:ro \ --vo ...

  3. linux查找命令(find)

    linux查找命令(find) 命令格式: find [目录] [选项] [选项的条件] 选项: -name:文件名称查找 -size:文件的大小来查找 -perm:文件的权限来查找 ①根据文件的名称 ...

  4. Excel 宏练习

    任务描述: 利用 Excel 绘制函数图像 f(x)=x^2/3+0.9*(3.3-x^2)^1/2*sin(a*x),并通过按钮事件来刷新图像. 问题分析: 可以参考类似 Matlab 绘图的方式, ...

  5. 当document.write 遇到外联script

    先来看个例子: <!DOCTYPE html> <html> <head> <title>测试 document.write</title> ...

  6. 生产环境中配置的samba

    实验需求: 由于实验室纳新一帮新孩子,搭建samba主要是临时共享一些学习资源的,基本上大家用的都是windows.而且这个服务可以让他们在校园的里的个个角落都可以访问到,所以给挂了学校的公网,不过我 ...

  7. python_函数嵌套(4)

    第1章 名称空间 1.1 定义 1.2 变量运行流程 1.3 临时名称空间 1.4 python三种名称空间 第2章 作用域 2.1 作用域分类 2.2 加载顺序 2.3 取值顺序 函数嵌套 2.4 ...

  8. ios微信浏览器click事件不起作用的解决方法

    $(document).on( "click", ".weui_cell", function (event) {alert(); }); JS代码是这样的,h ...

  9. 【HEVC帧间预测论文】P1.9 Coding Tree Depth Estimation for Complexity Reduction of HEVC

    Coding Tree Depth Estimation for Complexity Reduction of HEVC <HEVC标准介绍.HEVC帧间预测论文笔记>系列博客,目录见: ...

  10. Linux环境下手动配置sbt

    一.下载sbt安装包 从sbt官网下载地址:http://www.scala-sbt.org/download.html下载安装包,以sbt-0.13.13.tgz为例. 二.安装 1.将下载的二进制 ...