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 ...
随机推荐
- windows内存映射文件
http://shushanyegui.duapp.com/?p=731 在描述内存映射文件之前 我们先来写一个系统通过I/O来读写磁盘文件的小程序 #include "stdafx.h&q ...
- 一维的Haar小波变换
小波变换的基本思想是用一组小波函数或者基函数表示一个函数或者信号,例如图像信号.为了理解什么是小波变换,下面用一个具体的例子来说明小波变换的过程. 1. 求有限信号的均值和差值 [例] 假设有一幅分辨 ...
- Codeforces Round #342 (Div. 2) C
C. K-special Tables time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- POJ3690:Constellations(二维哈希)
Constellations Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 6822 Accepted: 1382 题目 ...
- Java中的字符串常量池?
参考:http://droidyue.com/blog/2014/12/21/string-literal-pool-in-java/index.html
- Android 命名规范 (转)
刚接触android的时候,命名都是按照拼音来,所以有的时候想看懂命名的那个控件什么是什么用的,就要读一遍甚至好几遍才知道,这样的话,在代码的审查和修改过程中就会浪费不少不必要的时间.如果就是我一个人 ...
- [Luogu 1168] 中位数
中位数可以转化为区间第k大问题,当然是选择Treap实现名次树了啊.(笑) 功能十分简单的Treap即能满足需求--只需要插入与查找第大的功能. 插入第i个数时,如果i是奇数,随即询问当前排名第(i+ ...
- html 制作静态页面新知识
1.在区块线边框添加一条水平线 例如:<div style:"height :300px;width:800px;border-bottom: solid 1px orange ;& ...
- 【BZOJ2287】消失之物 [分治][DP]
消失之物 Time Limit: 10 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description ftiasch 有 N 个物品, ...
- vs调试 配置IISExpress允许局域网内部访问
内网可访问后,本机不能使用localhost 1.找到IISExpress的配置文件,位于 <文档>/IISExpress/config文件夹下,打开applicationhost.c ...