Dragon Balls--hdu3635(并查集)
Dragon Balls
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4346 Accepted Submission(s):
1658

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.
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)
formated as sample output. Then for each query, output a line with three
integers X Y Z saparated by a blank space.
#include<cstdio>
#include<cstring>
#include<algorithm>
#define max 10002
using namespace std;
int father[max],ran[max],num[max];
int a,b; void init()
{
int i;
for(i=;i<=a;i++)
{
father[i]=i;//记录父节点
ran[i]=;//记录所在城市共有多少龙珠
num[i]=;//记录移动的次数
}
} int find(int x)
{
if(x==father[x]) return x;
int t=father[x];
father[x]=find(father[x]);//压缩路径 ,都指向根节点
num[x]+=num[t];//每个球移动的次数等于本身移动的个数加上父节点移动的次数
return father[x];
} void join(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
{
father[fx]=fy;//移动一个根节点到另一个根节点
ran[fy]+=ran[fx];//fy为根节点的总个数等于两个根节点拥有的个数相加
num[fx]=;//被移动的根节点第一次移动
}
} int main()
{
int i,N,m,n,cot=;
char c;
scanf("%d",&N);
while(N--)
{
scanf("%d%d",&a,&b);
getchar();
init();
printf("Case %d:\n",cot++);
for(i=;i<b;i++)
{
scanf("%c",&c);
if(c=='T')
{
scanf("%d%d",&m,&n);
getchar();
join(m,n);
}
else
{
scanf("%d",&m);
getchar();
int t=find(m);//要输出根节点的所有的个数
printf("%d %d %d\n",t,ran[t],num[m]);
}
}
}
return ;
}
Dragon Balls--hdu3635(并查集)的更多相关文章
- hdu 3635 Dragon Balls(并查集应用)
Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, ...
- [HDOJ3635]Dragon Balls(并查集,路径压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3635 题意:有n个龙珠,n个城市.初始状态第i个龙珠在第i个城市里.接下来有两个操作: T A B:把 ...
- hdoj 3635 Dragon Balls【并查集求节点转移次数+节点数+某点根节点】
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 3635:Dragon Balls(并查集)
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- hdu 3635 Dragon Balls(并查集)
题意: N个城市,每个城市有一个龙珠. 两个操作: 1.T A B:A城市的所有龙珠转移到B城市. 2.Q A:输出第A颗龙珠所在的城市,这个城市里所有的龙珠个数,第A颗龙珠总共到目前为止被转移了多少 ...
- Codeforces Round #245 (Div. 2) B. Balls Game 并查集
B. Balls Game Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/430/problem ...
- *HDU3635 并查集
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- hdoj--3635--Dragon Balls(并查集记录深度)
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- Dragon Balls[HDU3635]
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
随机推荐
- js的function中的指向自身的属性
1,arguments.callee 2,方法的constructor
- 移动端h5页面写法
1. 页面宽度320, 所有元素尺寸设一半 缺点:不自能适应全屏 2.页面宽度640,元素尺寸正常 <meta charset="utf-8" /> <meta ...
- Qt中实现启动画面(延时过程中要加上app.processEvents())
纵所周之,当一个程序的启动比较耗时的时候,为了不让用户枯燥的等待或者是误以为程序运行异常了,所以我们都会在启动比较耗时的程序中加上启动界面 ,例如office软件等等. 在Qt中实现启动界面,主要就是 ...
- json中换行问题
json中不能存在换行,但可以进行替换后给服务器 function(text_info) { text_info=text_info.replace(/\r/gm,"<br\>& ...
- Oleg Sych - » Pros and Cons of T4 in Visual Studio 2008
Oleg Sych - » Pros and Cons of T4 in Visual Studio 2008 Pros and Cons of T4 in Visual Studio 2008 Po ...
- [转]使用Navicat for Oracle工具连接oracle的
使用Navicat for Oracle工具连接oracle的 这是一款oracle的客户端的图形化管理和开发工具,对于许多的数据库都有支持.之前用过 Navicat for sqlserver,感觉 ...
- Java程序员面试题集(51-70)(转)
转:http://blog.csdn.net/jackfrued/article/details/17403101 Java程序员面试题集(51-70) 摘要:这一部分主要讲解了异常.多线程.容器和I ...
- 9.5 在 C# 中使用 F# 库
9.5 在 C# 中使用 F# 库 像 C# 一样,F# 也是一种静态类型的语言,就是说,编译器知道每一个值的类型,以及类方法和属性的签名.对于与 C# 的互操作性来说.这是很重要的,由于,编译器能够 ...
- python3 module中__init__.py的需要注意的地方
网上关于__init__.py的作用的资料到处都是,我在此就不再啰嗦哪些了. 若有需要.请各位看官去搜搜即可. 最近刚开始用Python3 就遇到了这个比较有意思的事情 闲言少叙,下面要介绍的是pyt ...
- 安装mysql时出错。无法初始化。 libstdc++.so.5
./bin/mysqld: error while loading shared libraries: libstdc++.so.5: cannot open shared object file: ...