hdu 3635 Dragon Balls (带权并查集)

时间:2021-12-23 16:27:07

Dragon Balls

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

Problem Description
Five hundred years later, the number of dragon balls will increase unexpectedly, so it's too difficult for Monkey King(WuKong) to gather all of the dragon balls together. 
hdu  3635 Dragon Balls (带权并查集)
His country has N cities and there are exactly N dragon balls in the world. At first, for the ith dragon ball, the sacred dragon will puts it in the ith city. Through long years, some cities' dragon ball(s) would be transported to other cities. To save physical strength WuKong plans to take Flying Nimbus Cloud, a magical flying cloud to gather dragon balls. 
Every time WuKong will collect the information of one dragon ball, he will ask you the information of that ball. You must tell him which city the ball is located and how many dragon balls are there in that city, you also need to tell him how many times the ball has been transported so far.
 
Input
The first line of the input is a single positive integer T(0 < T <= 100). 
For each case, the first line contains two integers: N and Q (2 < N <= 10000 , 2 < Q <= 10000).
Each of the following Q lines contains either a fact or a question as the follow format:
  T A B : All the dragon balls which are in the same city with A have been transported to the city the Bth ball in. You can assume that the two cities are different.
  Q A : WuKong want to know X (the id of the city Ath ball is in), Y (the count of balls in Xth city) and Z (the tranporting times of the Ath ball). (1 <= A, B <= N)
 
Output
For each test case, output the test case number formated as sample output. Then for each query, output a line with three integers X Y Z saparated by a blank space.
 
Sample Input
2
3 3
T 1 2
T 3 2
Q 2
3 4
T 1 2
Q 1
T 1 3
Q 1
 
Sample Output
Case 1:
2 3 0
Case 2:
2 2 1
3 3 2
 
Author
possessor WC
 
Source
 

题意:

每一个城市都有一颗龙珠,但是随着时间的推移,龙珠会被移到其他的城市,悟空想去收集这些龙珠,但是他需要你告知他,他要找的那颗龙珠的所在的城市,以及这个城市所拥有的龙珠数量,还有这颗龙珠迁移过多少次。

代码:

 #define LOCAL
#include<cstring>
#include<cstdio>
#define maxn 10005
/*
(1) 第i求所在的城市x city[];
(2) x城市所拥有的球数量 ball[];
(3) i球被转移的次数 cnt[];
*/
int ball[maxn];
int city[maxn];
int cnt[maxn];
int n,q;
void init(){
for(int i= ; i<=n ;i++ ){
city[i]=i;
ball[i]=;
cnt[i]=; //转移次数
}
} //搜索该龙珠所在城市的位置
int fin(int x)
{
//int tem;
if(x==city[x])
return city[x];
int tem=city[x];
city[x]=fin(city[x]);
cnt[x]+=cnt[tem];
return city[x];
} void Union(int x,int y)
{
/*
将 x城市的所有龙珠转移到y城市中
*/
x=fin(x);
y=fin(y);
if(x!=y){
city[x]=city[y];
ball[y]+=ball[x];
ball[x]=; //球全部移动到y城市中
cnt[x]=; //第一次移动
}
} int main()
{ int t,a,b,tt=;
char str[];
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&q);
init();
printf("Case %d:\n",++tt);
while(q--){
scanf("%s",str);
if(str[]=='T'){
scanf("%d%d",&a,&b);
Union(a,b);
}
else
{
scanf("%d",&a);
b=fin(a);
printf("%d %d %d\n",b,ball[b],cnt[a]);
}
}
}
return ;
}

hdu 3635 Dragon Balls (带权并查集)的更多相关文章

  1. hdu 3635 Dragon Balls(加权并查集)2010 ACM-ICPC Multi-University Training Contest(19)

    这道题说,在很久很久以前,有一个故事.故事的名字叫龙珠.后来,龙珠不知道出了什么问题,从7个变成了n个. 在悟空所在的国家里有n个城市,每个城市有1个龙珠,第i个城市有第i个龙珠. 然后,每经过一段时 ...

  2. hdu3635 Dragon Balls&lpar;带权并查集&rpar;

    /* 题意:有N个城市, 每一个城市都有一个龙珠(编号与城市的编号相同),有两个操作 T A ,B 将标号为A龙珠所在城市的所有的龙珠移动到B龙珠所在城市中! 思路:并查集 (压缩路径的时候将龙珠移动 ...

  3. hdu 5441 Travel 离线带权并查集

    Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...

  4. Hdu 2047 Zjnu Stadium&lpar;带权并查集&rpar;

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

  5. How Many Answers Are Wrong (HDU - 3038)(带权并查集)

    题目链接 并查集是用来对集合合并查询的一种数据结构,或者判断是不是一个集合,本题是给你一系列区间和,判断给出的区间中有几个是不合法的. 思考: 1.如何建立区间之间的联系 2.如何发现悖论 首先是如何 ...

  6. hdu 5441 travel 离线&plus;带权并查集

    Time Limit: 1500/1000 MS (Java/Others)  Memory Limit: 131072/131072 K (Java/Others) Problem Descript ...

  7. hdu 2818 Building Block &lpar;带权并查集,很优美的题目&rpar;

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

  8. How Many Answers Are Wrong HDU - 3038 (经典带权并查集)

    题目大意:有一个区间,长度为n,然后跟着m个子区间,每个字区间的格式为x,y,z表示[x,y]的和为z.如果当前区间和与前面的区间和发生冲突,当前区间和会被判错,问:有多少个区间和会被判错. 题解:x ...

  9. hdu 3047&ndash&semi;Zjnu Stadium&lpar;带权并查集&rpar;

    题目大意: 有n个人坐在zjnu体育馆里面,然后给出m个他们之间的距离, A B X, 代表B的座位比A多X. 然后求出这m个关系之间有多少个错误,所谓错误就是当前这个关系与之前的有冲突. 分析: 首 ...

随机推荐

  1. ci连贯操作的limit两个参数和sql是相反的

    ci连贯操作的limit两个参数和sql是相反的 db->where("name =",'mary')->ge() name后面要有个空格否则报错当为一个字段 -&gt ...

  2. Redis存储Session

    net Core 使用Redis存储Session   前言 Asp.net Core 改变了之前的封闭,现在开源且开放,下面我们来用Redis存储Session来做一个简单的测试,或者叫做中间件(m ...

  3. window&period;location&period;href跳转至空白页

    现象:window.location.href = "XXX"调到了空白页,但是将XXX在窗口地址栏输入就会可以访问到. 原因:就是XXX前缀没有加上"http://&q ...

  4. JAVA 处理 Spring data mongodb 时区问题

    Spring data mongodb 查询出结果的时候会自动 + 8小时,所以我们看起来结果是对的 但是我们查询的时候,并不会自动 + 8小时,需要自己处理 解决方法 1   @JsonFormat ...

  5. 14&period;UA池和代理池

    今日概要 scrapy下载中间件 UA池 代理池 今日详情 一.下载中间件 先祭出框架图: 下载中间件(Downloader Middlewares) 位于scrapy引擎和下载器之间的一层组件. - ...

  6. html5播放器制作小结

    链接:http://snowinmay.net/6rooms/html/music.php 9月份前的版本: 播放,暂停,点赞,播放状态显示. 9.2版本: 下载歌曲,静音,时间倒计时(点击暂停时倒计 ...

  7. Unity3d 镜面折射 vertex and frag Shader源代码

    Unity3d 镜面折射  网上能找到的基本上是固定管道或表面渲染的shader. 特此翻译为顶点.片段渲染的Shader, 本源代码仅仅涉及shader与cs部分, 请自行下载NGUI  unity ...

  8. Linux基础命令---fsck

    fsck 检查或者修复指定的文件系统,可以是设备名.挂载点,还可以是一个ext2的label,或者是一个UUID.此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.ope ...

  9. 在ANTMINER&lpar;阉割版BeagleBone Black&rpar;运行Debain

    开门见山,直入主题 咸鱼入手3块阉割ARM板,经过快递近6天运输到手,不过价格便宜 东西下面这样的(借了咸鱼的图): 发现这块板是阉割版的国外beagleboard.org型号为BeagleBone ...

  10. 【BZOJ】1068&colon; &lbrack;SCOI2007&rsqb;压缩(dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1068 发现如果只设一维的话无法转移 那么我们开第二维,发现对于前i个来说,如果确定了M在哪里,第i个 ...