题意: 给出四种操作: 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(…
t’s universally acknowledged that there’re innumerable trees in the campus of HUST.  Thus a professional tree manager is needed. Your task is to write a program to help manage the trees.  Initially, there are n forests and for the i-th forest there i…
题目描述 It's universally acknowledged that there're innumerable trees in the campus of HUST. Thus a professional tree manager is needed. Your task is to write a program to help manage the trees. Initially, there are n forests and for the i-th forest the…
More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others)Total Submission(s): 25168    Accepted Submission(s): 9045 Problem Description Mr Wang wants some boys to help him with a project. Because the project…
并查集能做什么? 1.连接两个对象; 2.查询两个对象是否在一个集合中,或者说两个对象是否是连接在一起的. 并查集有什么应用? 1. Percolation问题. 2. 无向图连通子图个数 3. 最近公共祖先问题 4. Kruskal最小生成树 5. 社交网络 等等 并查集数据结构: 并查集是一种树形的数据结构,处理不相交集合的合并和查询操作. 并查集常用的启发式策略:路径压缩,按秩合并(或按集合元素个数合并) 路径压缩是为了加快查找的效率,让树变得竟可能的平. 按秩合并(或按集合元素个数合并)…
P3295 [SCOI2016]萌萌哒 题面 题目描述 一个长度为 \(n\) 的大数,用 \(S_1S_2S_3 \cdots S_n\) 表示,其中 \(S_i\) 表示数的第 \(i\) 位, \(S_1\) 是数的最高位.告诉你一些限制条件,每个条件表示为四个数, \(l_1,r_1,l_2,r_2\) ,即两个长度相同的区间,表示子串 \(S_{l_1}S_{l_1+1}S_{l_1+2} \cdots S_{r_1}\) 与 \(S_{l_2}S_{l_2+1}S_{l_2+2} \…
题意:有 n 个朋友,他们可能相互认识,A 认识 B,B 认识 C,则 ABC 相互认识,现在给出他们的认识情况,相互认识的人坐一桌,否则需要分开坐,问至少需要多少桌. 其实就是问并查集的个数,在初始情况下一人一个并查集,共 n 个,每次合并操作会减少一个并查集,所以只要每次合并时计数减一下就行,全部合并完之后就可以输出剩余并查集个数了. #include<iostream> #include<cstdio> #include<cstring> using namesp…
概念 并查集是一种树形的数据结构,用来处理一些不交集的合并及查询问题.主要有两个操作: find:确定元素属于哪一个子集. union:将两个子集合并成同一个集合. 所以并查集能够解决网络中节点的连通性问题. 基本实现 package com.yunche.datastructure; /** * @ClassName: UF * @Description: 并查集 * @author: yunche * @date: 2018/12/30 */ public class UF { /** *…
题目链接 dalao们怎么都写的线段树合并啊.. dsu跑的好慢. \(Description\) 给定一棵\(n(n\leq 10^5)\)个点的树. 定义\(Tree[L,R]\)表示为了使得\(L\sim R\)号点两两连通,最少需要选择的边的数量. 求\[\sum_{l=1}^n\sum_{r=l}^nTree[l,r]\] \(Solution\) 枚举每条边,计算它的贡献. 那么我们要判断有多少连续区间的点跨过这条边,并不好算,反过来去求在这条边的两侧分别有多少个连续区间. 那么显然…
http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给出一个图,每条边有一个距离,现在有多个询问,每个询问有一个距离值d,对于每一个询问,计算出有多少点对(x,y)使得在x到y的路径上没有一条边的距离大于d. 思路:只要边距离小于d,那就是可行的,直接加入并查集来维护.并查集需要维护一下树的节点个数. 将边和询问都排序离线处理. #include<iostream> #include<cstdio> #include<cstri…