Building Block

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4420    Accepted Submission(s): 1372

Problem Description
John
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.

 
Input
The first line contains integer P. Then P lines follow, each of which contain an operation describe above.
 
Output
Output the count for each C operations in one line.
 
Sample Input
6
M 1 6
C 1
M 2 4
M 2 6
C 3
C 4
 
Sample Output
1
0
2
 
越来越觉得并查集牛了。。
 
题目大意:有N个piles(按序编号),每次有两种操作:
M x y 把包含x的一堆放到包含y的一堆上,如果xy同在一堆上,不做处理
C x 询问在x之下有多少个方块
 
题解:在以前的father和count(friend)之上再添加一个新的数组 under 表示当前结点的下面的pile的个数(我的理解是每个点带有一个权值为under[pos])
under数组的更新:第一种是针对每次将x堆放在y堆上面时,under[x]= under[x]+count[y]
第二种是求 a的根节点x与a之间的pile的个数,这里要利用find函数来进行递归,under[deep] = under[deep]+under[deep+1]
最后回溯到a点时就可以将under[a]求出来了
 
代码:
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(并查集,带权更新)的更多相关文章

  1. poj 1962(并查集+带权更新)

    Corporative Network Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 3664   Accepted: 13 ...

  2. 浅谈并查集&种类并查集&带权并查集

    并查集&种类并查集&带权并查集 前言: 因为是学习记录,所以知识讲解+例题推荐+练习题解都是放在一起的qvq 目录 并查集基础知识 并查集基础题目 种类并查集知识 种类并查集题目 并查 ...

  3. HDU 3038 How Many Answers Are Wrong 并查集带权路径压缩

    思路跟 LA 6187 完全一样. 我是乍一看没反应过来这是个并查集,知道之后就好做了. d[i]代表节点 i 到根节点的距离,即每次的sum. #include <cstdio> #in ...

  4. 种类并查集——带权并查集——POJ1182;HDU3038

    POJ1182 HDU3038 这两个题比较像(一类题目),属于带权(种类)并查集 poj1182描绘得三种动物种类的关系,按照他一开始给你的关系,优化你的种类关系网络,最后看看再优化的过程中有几处矛 ...

  5. Poj1182 食物链(并查集/带权并查集)

    题面 Poj 题解 这里采用并查集的补集. \(x\)表示同类集合,\(x+n\)表示敌人集合,\(x+n\times2\)表示敌人的敌人集合. 如果当前给出的是一对同类关系,就判断\(x\)是否吃\ ...

  6. POJ 1182 食物链 [并查集 带权并查集 开拓思路]

    传送门 P - 食物链 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit  ...

  7. 【并查集&&带权并查集】BZOJ3296&&POJ1182

    bzoj1529[POI2005]ska Piggy banks [题目大意] n头奶牛m种语言,每种奶牛分别掌握一些语言.问至少再让奶牛多学多少种语言,才能使得它们能够直接或间接交流? [思路] ( ...

  8. hdu 4514 并查集+树形dp

    湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  9. HDU 3926 并查集 图同构简单判断 STL

    给出两个图,问你是不是同构的... 直接通过并查集建图,暴力用SET判断下子节点个数就行了. /** @Date : 2017-09-22 16:13:42 * @FileName: HDU 3926 ...

随机推荐

  1. codeforces的dp专题

    1.(467C)http://codeforces.com/problemset/problem/467/C 题意:有一个长为n的序列,选取k个长度为m的子序列(子序列中不能有位置重复),求所取的k个 ...

  2. Vue.js中的常用的指令缩写

    Vue.js为两个最为常用的指令提供了特别的缩写: v-bind缩写 <!--完整语法--> <a v-bind:href="url">测试</a&g ...

  3. Android数据库资料

    一.联系人和通话记录: 数据库文件/data/data/com.android.providers.contacts/databases/contacts2.db  通话记录的数据存在calls表中; ...

  4. PowerDesigner16 状态图

    状态图(Statechart Diagram)主要用于描述一个对象在其生存期间的动态行为,表现为一个对象所经历的状态序列,引起状态转移的事件(Event),以及因状态转移而伴随的动作(Action). ...

  5. modelsim10 SE 仿真lattice Xp2工程

    1.首先要建立Lattice XP2库 在modelsim10 SE启动后.首先指定Lattice Diamond 1.4 给定的仿真器库源代码编译目录: C:\lscc\diamond\1.4\ca ...

  6. python学习笔记(九)之字符串

    定义字符串 >>> mystring = 'Hello Python' >>> name = str('Mountain') >>> mystri ...

  7. HDU 1180 诡异的楼梯 (广搜)

    题目链接 Problem Description Hogwarts正式开学以后,Harry发现在Hogwarts里,某些楼梯并不是静止不动的,相反,他们每隔一分钟就变动一次方向. 比如下面的例子里,一 ...

  8. 在前端发起ajax遇到问题

    1.请注意设置datatype的类型. 如下图:

  9. CSS浮动为什么不会遮盖同级元素

    1.问题描述 在W3CSchool学习web前端时,看完CSS定位-浮动这一节后,感觉没有什么问题.但是在CSS高级-分类这一节的中进行实践时,遇到了如下问题.测试地址:浮动的简单应用. 完整的htm ...

  10. windows下常用快捷键(转)

    原文转自 https://blog.csdn.net/LJFPHP/article/details/78818696 win+E                 打开文件管器 win+D        ...