Nowcoder 106 C.Professional Manager(统计并查集的个数)
题意:
给出四种操作:
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(统计并查集的个数)的更多相关文章
- 牛客Professional Manager(并查集)
t’s universally acknowledged that there’re innumerable trees in the campus of HUST. Thus a professi ...
- 第十四届华中科技大学程序设计竞赛 C Professional Manager【并查集删除/虚点】
题目描述 It's universally acknowledged that there're innumerable trees in the campus of HUST. Thus a pro ...
- HDU1865--More is better(统计并查集的秩(元素个数))
More is better Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Others) ...
- 并查集 Union-Find
并查集能做什么? 1.连接两个对象; 2.查询两个对象是否在一个集合中,或者说两个对象是否是连接在一起的. 并查集有什么应用? 1. Percolation问题. 2. 无向图连通子图个数 3. 最近 ...
- Luogu P3295 [SCOI2016]萌萌哒(并查集+倍增)
P3295 [SCOI2016]萌萌哒 题面 题目描述 一个长度为 \(n\) 的大数,用 \(S_1S_2S_3 \cdots S_n\) 表示,其中 \(S_i\) 表示数的第 \(i\) 位, ...
- hdu1213 并查集
题意:有 n 个朋友,他们可能相互认识,A 认识 B,B 认识 C,则 ABC 相互认识,现在给出他们的认识情况,相互认识的人坐一桌,否则需要分开坐,问至少需要多少桌. 其实就是问并查集的个数,在初始 ...
- 并查集(Union Find)的基本实现
概念 并查集是一种树形的数据结构,用来处理一些不交集的合并及查询问题.主要有两个操作: find:确定元素属于哪一个子集. union:将两个子集合并成同一个集合. 所以并查集能够解决网络中节点的连通 ...
- 树上统计treecnt(dsu on tree 并查集 正难则反)
题目链接 dalao们怎么都写的线段树合并啊.. dsu跑的好慢. \(Description\) 给定一棵\(n(n\leq 10^5)\)个点的树. 定义\(Tree[L,R]\)表示为了使得\( ...
- HDU 5441 Travel(并查集+统计节点个数)
http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给出一个图,每条边有一个距离,现在有多个询问,每个询问有一个距离值d,对于每一个询问,计算出有多少点 ...
随机推荐
- iOS bounds vs frame
斯坦福iOS开发课程的白胡子大叔的PPT解释得淋漓尽致!
- 跟我一起玩Win32开发(15):ListView控件
这个控件其实不用阿拉来介绍,因为它太常见了,就好像我们一出门就会看到妹子一样常见.当然也可以说,它是对ListBox的扩充. 在使用该控件之前,我先介绍VS的一个相当好玩的功能. 在代码文件的#inc ...
- Mirror Number SPOJ - MYQ10
Mirror Number SPOJ - MYQ10 题意:http://blog.csdn.net/hcbbt/article/details/38349367 稍微改一下http://www.cn ...
- Codeforces Round #321 (Div. 2)
水 A - Kefa and First Steps /************************************************ * Author :Running_Time ...
- spring mvc支持跨域请求
@WebFilter(urlPatterns = "/*", filterName = "corsFilter") public class CorsFilte ...
- python2和python3的区别(转)
基本语法差异 核心类差异 Python3对Unicode字符的原生支持 Python2中使用 ASCII 码作为默认编码方式导致string有两种类型str和unicode,Python3只支持uni ...
- void运算符
void是一元运算符,它出现在操作数之前,操作数可以是任意类型,操作数会照常计算,但忽略计算结果并返回undefined.由于void会忽略操作数的值,因此在操作数具有副作用的时候使用void来让程序 ...
- ag-grid-vue的 行默认选中
that.$nextTick(() => { that.gridListOptions.api.onGroupExpandedOrCollapsed(); that.$nextTick(() = ...
- Kali部署openvas初探与实践
openvas安装 1.我用的清华大学的源,所以我把/etc/apt/source.list中下入如下源地址 #清华大学deb http://mirrors.tuna.tsinghua.edu.cn/ ...
- NSString 与NSMutableString的区别
NSString 与NSMutableString的区别 Suppose You have a code like this NSString *s = [[NSString alloc] ...