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,对于每一个询问,计算出有多少点 ...
随机推荐
- 题解报告:hdu 1392 Surround the Trees(凸包入门)
Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to surround a ...
- php输出中文字符
中文字符不可以使用imagettftext()函数在图片中直接输出,如果要输出中文字符,需要先使用iconv()函数对中文字符进行编码,语法格式如下:string iconv ( string $in ...
- 508 Most Frequent Subtree Sum 出现频率最高的子树和
详见:https://leetcode.com/problems/most-frequent-subtree-sum/description/ C++: /** * Definition for a ...
- Suricata的输出
不多说,直接上干货! 见官网 https://suricata.readthedocs.io/en/latest/output/index.html 总的来说,Suricata采集下来的数据输出分为: ...
- SQL异常为"当IDENTITY_INSERT设置为OFF时" 的解决
误删数据库时,可以利用insert插入删除的数据,但是有时表可能有自增字段如id.这是插入数据如果包含自增字段就会出现错误,提示"IDENTITY_INSERT设置为OFF,插入失败&quo ...
- BeanUtils.copyProperties(productInfo, productInfoVO);
一:spring的工具类方法:BeanUtils.copyProperties(orderMasterDTO, orderMasterDO); 作用:将orderMasterDTO对象中的属性值,赋值 ...
- http://www.360doc.com/content/10/0928/12/11991_57014502.shtml
http://www.360doc.com/content/10/0928/12/11991_57014502.shtml
- 我来解数独(附delphi源码)
前段时间看到“69岁农民3天破解世界最难数独游戏”,然后在看了那个号称世界最难的数独题目之后,就打算抽空编程解决.今晚抽出一个晚上,大约四五个小时的时间,中间还间歇在clash of clans上造兵 ...
- like SQL注入与防止 (bin2hex unhex)
普通的列表模糊查询,可能会被sql注入利用,造成数据泄漏,严重的甚至导致删表删库! 程序中sql语句拼装: $sql = 'student_name like '"%'.$name.'%&q ...
- CortexA7工业级迅为-iMX6UL开发板硬件和资料介绍
商业级核心板 ARM Cortex-A7架构 主频高达528 MHz 核心板512M DDR内存 8G EMMC 存储 运行温度:-20℃ ~ +80℃ CPU集成电源管理 核心板尺寸仅:42mm*3 ...