题意:

给出四种操作:

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. 区间dp实战练习

    题解报告:poj 2955 Brackets(括号匹配) Description We give the following inductive definition of a “regular br ...

  2. 转 google 制作 邀请函

    http://blog.csdn.net/adali/article/details/8315619

  3. 144 Binary Tree Preorder Traversal 二叉树的前序遍历

    给定一棵二叉树,返回其节点值的前序遍历.例如:给定二叉树[1,null,2,3],   1    \     2    /   3返回 [1,2,3].注意: 递归方法很简单,你可以使用迭代方法来解决 ...

  4. Jquery show()方法图解

    前两天面试的时候被问到了show()方法,当时回答的实在是太惨烈... 晚上看了一下,最简单的走法是直接移除行内样式的style属性. 如果这步走完了,元素还是隐藏的(display为none),元素 ...

  5. C. Tennis Championship dp递推 || 找规律

    http://codeforces.com/contest/735/problem/C C. Tennis Championship time limit per test 2 seconds mem ...

  6. 关于提示replication性能

    关于GTIDs的二进制日志:gtid_next: 下一个事务的编号,是master传给slave的 如SET @@SESSION.GTID_NEXT= 'c09756b8-a7e7-11e5-9468 ...

  7. eclipse 闪退原因

    电脑关机后 重启发现eclipse竟然打不开了,打开出现闪退,原因是: 1.JDK版本不一致,或者你电脑上安装了多个版本: 2.环境变量:安装了其他软件比如:oracle 它自带有一个较低版本的JDK ...

  8. Suricata的配置

    见官网 https://suricata.readthedocs.io/en/latest/configuration/index.html# Docs » 8. Configuration Edit ...

  9. props.children 和容器类组件

    有一类组件,充当了容器的作用,它定义了一种外层结构形式,然后你可以往里面塞任意的内容.这种结构在实际当中非常常见,例如这种带卡片组件: 组件本身是一个不带任何内容的方形的容器,我可以在用这个组件的时候 ...

  10. P1739 表达式括号匹配

    题目描述 假设一个表达式有英文字母(小写).运算符(+,—,*,/)和左右小(圆)括号构成,以“@”作为表达式的结束符.请编写一个程序检查表达式中的左右圆括号是否匹配,若匹配,则返回“YES”:否则返 ...