HDU - 2818
Building Block
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3563 Accepted Submission(s): 1072
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.
0
2
/***
题意:给出一些数,并给对这些数进行操作;
'M a b'代表把a堆加到b堆上面,
‘C a ' 代表查询当前点a下面有多少个
做法:并查集。
***/
#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
#include<cmath>
using namespace std;
#define maxn 300000 + 3000
int fa[maxn]; ///记录父节点
int sum[maxn]; ///记录当前堆的总和
int under[maxn]; ///带表当前节点下有多少pile
int n;
void Init()
{
for(int i=; i<=n; i++)
{
sum[i] = ;
fa[i] = i;
}
memset(under,,sizeof(under));
}
int Find(int u)
{
int tmp;
if(u != fa[u])
{
tmp = Find(fa[u]);
under[u] += under[fa[u]];
fa[u] = tmp;
}
return fa[u];
}
void Union(int x,int y)
{
int X,Y;
X = Find(x);
Y = Find(y);
if (X!=Y)
{
under[X] = sum[Y]; //X是当前堆(集合)中最底部的,直接更新under[]
sum[Y] += sum[X]; //直接更新Y这堆(集合)的高度(总共多少个Piles)
fa[X] = Y; //合并
}
}
int main()
{
while(~scanf("%d",&n))
{
Init();
char ch[];
int u,v,w;
while(n--)
{
scanf("%s",ch);
if(ch[] == 'M')
{
scanf("%d %d",&u,&v);
Union(u,v);
}
else
{
scanf("%d",&w);
Find(w);
printf("%d\n",under[w]);
}
}
}
return ;
}
HDU - 2818的更多相关文章
- HDU 2818 (矢量并查集)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2818 题目大意:每次指定一块砖头,移动砖头所在堆到另一堆.查询指定砖头下面有几块砖头. 解题思路: ...
- hdu 2818 Building Block
Building Block Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 2818 Building Block(加权并查集)2009 Multi-University Training Contest 1
题意: 一共有30000个箱子,刚开始时都是分开放置的.接下来会有两种操作: 1. M x y,表示把x箱子所在的一摞放到y箱子那一摞上. 2. C y,表示询问y下方有多少个箱子. 输入: 首行输入 ...
- hdu 2818 Building Block (带权并查集,很优美的题目)
Problem Description John are playing with blocks. There are N blocks ( <= N <= ) numbered ...N ...
- hdu 2818 Building Block(并查集,有点点复杂)
Building Block Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 2818(并查集,带权更新)
Building Block Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 2818 Building Block 种类并查集
在进行并的时候不能瞎jb并,比如(x, y)就必须把x并给y ,即fa[x] = y #include <iostream> #include <string> #includ ...
- HDU——T 2818 Building Block
http://acm.hdu.edu.cn/showproblem.php?pid=2818 Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...
- POJ 3100 & ZOJ 2818 & HDU 2740 Root of the Problem(数学)
题目链接: POJ:id=3100" style="font-size:18px">http://poj.org/problem? id=3100 ZOJ:http ...
随机推荐
- Mysql Fabric实现学习笔记
Mysql Fabric用来管理mysql服务,提供扩展性和容易使用的系统,管理mysql分片和高可用部署(当前实现了两个特性:高可用和使用数据分片的横向扩展,能单独使用或结合使用这两个特性.). 架 ...
- SpringBoot-配置文件属性注入-3种方式
配置文件: datasource.username = admin datasource.url = /hello/world 方式一: @Value 前提: <!-- JavaBean处理工具 ...
- spring的RestTemplate使用指南
前言:现在restful接口越来越广泛,而如今很多接口摒弃了传统的配置复杂的webService开发模式,在java领域只需要很简单的springMvc就可以声明为一个控制器,再加上service层, ...
- 阿里云配置redis
一.redis redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sor ...
- selenium - webdriver - 设置元素等待
隐式等待:implicitly_wait(value), value默认是0 from selenium import webdriverfrom selenium.common.exceptions ...
- git merge与git rebase
文章源:https://blog.csdn.net/wh_19910525/article/details/7554489 git merge是用来合并两个分支的. git merge b # 将b分 ...
- [技巧篇]19.InputStream与String,Byte之间互转[转载]
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOExceptio ...
- Java设计模式の装饰者模式
目录 一.问题引入 二.设计原则 三.用装饰者模式解决问题 四.装饰者模式的特点 五.装饰者模式的定义 六.装饰者模式的实现 七.java.io包内的装饰者模式 一.问题引入 咖啡店的类设计: 一个饮 ...
- jquery checkbox选中状态以及实现全选反选
jquery1.6以下版本获取checkbox的选中状态: $('.ck').attr('checked'); $('.ck').attr('checked',true);//全选 $('.ck'). ...
- sublime text 快速编码技巧 GIT图
网上到处都云云sublime有多好.用了一年多的时间,受益匪浅,减少了很多重复性的劳动. 特别是: 1.灵活强大的多行编辑功能: 2.快速查找文件 ctrl + p; 3.正则查找 + 多行编辑; 4 ...