hdu 2818(并查集,带权更新)
Building Block
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4420 Accepted Submission(s): 1372
are playing with blocks. There are N blocks (1 <= N <= 30000)
numbered 1...N。Initially, there are N piles, and each pile contains one
block. Then John do some operations P times (1 <= P <= 1000000).
There are two kinds of operation:
M X Y : Put the whole pile
containing block X up to the pile containing Y. If X and Y are in the
same pile, just ignore this command.
C X : Count the number of blocks under block X
You are request to find out the output for each C operation.
M 1 6
C 1
M 2 4
M 2 6
C 3
C 4
0
2
M x y 把包含x的一堆放到包含y的一堆上,如果xy同在一堆上,不做处理
C x 询问在x之下有多少个方块
import java.util.Scanner;
public class Main {
static int[] father;
static int[] count;
static int[] under;// 编号为i的Pile下面有多少个Piles
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
father = new int[30005];
count = new int[30005];
under = new int[30005];
for (int i = 0; i < 30005; i++) {
father[i] = i;
count[i] = 1;
}
int n = sc.nextInt();
while (n-- > 0) {
String str = sc.next();
if (str.equals("M")) {
int a = sc.nextInt();
int b = sc.nextInt();
int x = find(a); // 找到的是a堆最下面的pile x
int y = find(b); // 找到的是b堆最下面的pile y
if (x != y) {
father[x] = y;
under[x] += count[y]; // 根节点下面的pile就是y堆所有的个数
count[y] += count[x]; // 更新当前堆的pile的个数
}
} else {
int a = sc.nextInt();
find(a);
System.out.println(under[a]);
}
}
}
private static int find(int a) {
if (father[a] != a) { //这步非常精彩 从 a点开始向下递归 ,每向下递归一层其上层就是等于 下层的under[dep+1]加上自己的under[dep]
//到最后回溯到a点就把所有的都求出来了
int tmp = find(father[a]);
under[a] += under[father[a]];
father[a] = tmp;
}
return father[a];
}
}
hdu 2818(并查集,带权更新)的更多相关文章
- poj 1962(并查集+带权更新)
Corporative Network Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 3664 Accepted: 13 ...
- 浅谈并查集&种类并查集&带权并查集
并查集&种类并查集&带权并查集 前言: 因为是学习记录,所以知识讲解+例题推荐+练习题解都是放在一起的qvq 目录 并查集基础知识 并查集基础题目 种类并查集知识 种类并查集题目 并查 ...
- HDU 3038 How Many Answers Are Wrong 并查集带权路径压缩
思路跟 LA 6187 完全一样. 我是乍一看没反应过来这是个并查集,知道之后就好做了. d[i]代表节点 i 到根节点的距离,即每次的sum. #include <cstdio> #in ...
- 种类并查集——带权并查集——POJ1182;HDU3038
POJ1182 HDU3038 这两个题比较像(一类题目),属于带权(种类)并查集 poj1182描绘得三种动物种类的关系,按照他一开始给你的关系,优化你的种类关系网络,最后看看再优化的过程中有几处矛 ...
- Poj1182 食物链(并查集/带权并查集)
题面 Poj 题解 这里采用并查集的补集. \(x\)表示同类集合,\(x+n\)表示敌人集合,\(x+n\times2\)表示敌人的敌人集合. 如果当前给出的是一对同类关系,就判断\(x\)是否吃\ ...
- POJ 1182 食物链 [并查集 带权并查集 开拓思路]
传送门 P - 食物链 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit ...
- 【并查集&&带权并查集】BZOJ3296&&POJ1182
bzoj1529[POI2005]ska Piggy banks [题目大意] n头奶牛m种语言,每种奶牛分别掌握一些语言.问至少再让奶牛多学多少种语言,才能使得它们能够直接或间接交流? [思路] ( ...
- hdu 4514 并查集+树形dp
湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...
- HDU 3926 并查集 图同构简单判断 STL
给出两个图,问你是不是同构的... 直接通过并查集建图,暴力用SET判断下子节点个数就行了. /** @Date : 2017-09-22 16:13:42 * @FileName: HDU 3926 ...
随机推荐
- BZOJ1912 APIO2010 洛谷P3629 巡逻
Description: 在一个地区中有 n 个村庄,编号为 1, 2, ..., n.有 n – 1 条道路连接着这些村 庄,每条道路刚好连接两个村庄,从任何一个村庄,都可以通过这些道路到达其 他任 ...
- [sed]记录
sed的括号本身没有特殊意义,如果要保留通配内容,需要转义. echo "1) host" |sed 's/1) ([a-z])/$1) $1/' 有两处有问题. 1. 首先是上 ...
- Kubernetes - Getting Started With Kubeadm
In this scenario you'll learn how to bootstrap a Kubernetes cluster using Kubeadm. Kubeadm solves th ...
- XAMPP 启动mysql报错 InnoDB: Error: could not open single-table tablespace file……
昨天安装了最新版本XAMPP for Windows 1.8.3. 今天早上打开XAMPP双击mysql Start按钮报错,如下(部分截取): 2013-09-17 10:12:02 9012 [E ...
- Linux Shell下执行sqlplus
转载自: http://www.cnblogs.com/include/archive/2011/12/30/2307889.html 以下方法解决了在linux下自动的删除创建用户 sqlplus ...
- Gradle加载本地jar包
有时,我们需要的jar包不一定能在远程仓库中找到,这时我们需要加载本地的jar包. 加载单独的jar包 在项目底下添加libs目录,将jar包仍进libs目录 build.gradle配置如下: de ...
- Plant (矩阵快速幂)
题目链接:http://codeforces.com/problemset/problem/185/A 题目: Dwarfs have planted a very interesting plant ...
- .NET Core Data Access
.NET Core was released a few months ago, and data access libraries for most databases, both relation ...
- [bzoj4569][SCOI2016]萌萌哒-并查集+倍增
Brief Description 一个长度为n的大数,用S1S2S3...Sn表示,其中Si表示数的第i位,S1是数的最高位,告诉你一些限制条件,每个条 件表示为四个数,l1,r1,l2,r2,即两 ...
- NodeJS中Buffer模块详解
一,开篇分析 所谓缓冲区Buffer,就是 "临时存贮区" 的意思,是暂时存放输入输出数据的一段内存. JS语言自身只有字符串数据类型,没有二进制数据类型,因此NodeJS提供了一 ...