http://acm.hdu.edu.cn/showproblem.php?pid=2818

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

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
 
Source
 
Recommend
gaojie   |   We have carefully selected several similar problems for you:  2819 2817 2821 2820 2822 
 
多组数据、、貌似因为这个WA好多次了、、
 #include <algorithm>
#include <cstring>
#include <cstdio> using namespace std; const int N();
int fa[N],sum[N],beh[N]; int find(int x)
{
if(fa[x]==x) return x;
int dad=fa[x];
fa[x]=find(fa[x]);
beh[x]+=beh[dad];
return fa[x];
}
void combine(int x,int y)
{
x=find(x),y=find(y);
if(x==y) return ;
beh[x]=sum[y];
sum[y]+=sum[x];
fa[x]=y;
}
void init()
{
for(int i=;i<N;i++) fa[i]=i,sum[i]=,beh[i]=;
} int main()
{
for(int p,u,v;~scanf("%d",&p);)
{
init();
for(char ch[];p--;)
{
scanf("%s%d",ch,&u);
if(ch[]=='M')
{
scanf("%d",&v);
combine(u,v);
}
else find(u),printf("%d\n",beh[u]);
}
}
return ;
}

HDU——T 2818 Building Block的更多相关文章

  1. hdu 2818 Building Block

    Building Block Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  2. hdu 2818 Building Block(并查集,有点点复杂)

    Building Block Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  3. hdu 2818 Building Block (带权并查集,很优美的题目)

    Problem Description John are playing with blocks. There are N blocks ( <= N <= ) numbered ...N ...

  4. hdu 2818 Building Block(加权并查集)2009 Multi-University Training Contest 1

    题意: 一共有30000个箱子,刚开始时都是分开放置的.接下来会有两种操作: 1. M x y,表示把x箱子所在的一摞放到y箱子那一摞上. 2. C y,表示询问y下方有多少个箱子. 输入: 首行输入 ...

  5. hdu 2818 Building Block 种类并查集

    在进行并的时候不能瞎jb并,比如(x, y)就必须把x并给y ,即fa[x] = y #include <iostream> #include <string> #includ ...

  6. Building Block

    Building Block Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...

  7. Building Block[HDU2818]

    Building Block Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...

  8. HDU 5538 House Building(模拟——思维)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5538 Problem Description Have you ever played the vi ...

  9. hdu 5538 House Building(长春现场赛——水题)

    题目链接:acm.hdu.edu.cn/showproblem.php?pid=5538 House Building Time Limit: 2000/1000 MS (Java/Others)   ...

随机推荐

  1. MSSQL读取xml字符串到临时表

    DECLARE @hdoc int DECLARE @doc xml SET @doc ='<CityValueSet> <CityItem> <CityId>20 ...

  2. activity(工作流)初步学习记录

    1.概念 工作流(Workflow),就是“业务过程的部分或整体在计算机应用环境下的自动化”,它主要解决的是“使在多个参与者之间按照某种预定义的规则传递文档.信息或任务的过程自动进行,从而实现某个预期 ...

  3. HDU 5234 Happy birthday【DP】

    题意:给出n*m的格子,每个格子的值为w[i][j],在值不超过k的时候,可以往右或者往下走,问从(1,1)走到(n,m)能够得到的最大的值 类似于背包 d[i][j][k]=maxx(d[i-1][ ...

  4. NetworkX-simple graph

    import networkx as nx import matplotlib.pyplot import scipy.io as sio import numpy as np load_path=' ...

  5. 路飞学城Python-Day17

    [1.编程范式] 1.面向过程编程 2.面向对象编程 [2.面向过程编程] 面向过程:核心就是过程   什么是过程? 过程指的是解决问题的步骤,先做什么,在作什么,面向过程就像是设计一个流水线,是一种 ...

  6. HDU-1052 Tian Ji -- The Horse Racing 贪心 考虑特殊位置(首尾元素)的讨论

    题目链接:https://cn.vjudge.net/problem/HDU-1052 题意 田忌赛马问题扩展版 给n匹马,马的能力可以相同 问得分最大多少 思路 贪心做得还是太少,一开始一点思虑都没 ...

  7. python编写登录与注册

    #编写简单的注册与登陆模块 #使用死循环来检测 while True: #如果条件为真,则一直循环 sum=3 #定义密码输入的次数 username = input("请输入用户名:&qu ...

  8. Docker学习总结(9)——Docker常用命令

    容器生命周期管理 - docker [run|start|stop|restart|kill|rm|pause|unpause] 容器操作运维 - docker [ps|inspect|top|att ...

  9. 最强最全干货分享:Android开发书籍、教程、工具等

    最全干货分享,本文收集整理了Android开发所需的书籍.教程.工具.资讯和周刊各种资源,它们能让你在Android开发之旅的各个阶段都受益. 入门<Learning Android(中文版)& ...

  10. mongodb E11000 duplicate key error collection: index: _id_ dup key

    今天在单测的时候,出现这个问题. 我代码只定义了一个变量 let body = {name: 'wu'} 然后连续2次插入这个body数据 await exam.insertExam(body); a ...