Dragon Balls

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

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.

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
 
 
这个题大意是开始的时候一个城市一个龙珠,如果输入T A B就是把A球所在的城市中的球全部转移到B球所在的城市
输入Q A就是要输出A球所在的城市序号,A球所在城市的球的个数,A球移动的次数
 
 
 
 
 #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(并查集)的更多相关文章

  1. hdu 3635 Dragon Balls(并查集应用)

    Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, ...

  2. [HDOJ3635]Dragon Balls(并查集,路径压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3635 题意:有n个龙珠,n个城市.初始状态第i个龙珠在第i个城市里.接下来有两个操作: T A B:把 ...

  3. hdoj 3635 Dragon Balls【并查集求节点转移次数+节点数+某点根节点】

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

  4. HDU 3635:Dragon Balls(并查集)

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  5. hdu 3635 Dragon Balls(并查集)

    题意: N个城市,每个城市有一个龙珠. 两个操作: 1.T A B:A城市的所有龙珠转移到B城市. 2.Q A:输出第A颗龙珠所在的城市,这个城市里所有的龙珠个数,第A颗龙珠总共到目前为止被转移了多少 ...

  6. 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 ...

  7. *HDU3635 并查集

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

  8. hdoj--3635--Dragon Balls(并查集记录深度)

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  9. Dragon Balls[HDU3635]

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...

随机推荐

  1. c# 配置连接 mysql

    今天复习了下c#连接mysql  记录下来方便自己也方便别人! 使用vs2010连接mysql 数据库, 1.装连接驱动,使用Connector/Net 连接驱动!下载地址:http://dev.my ...

  2. EcStore中的App是什么东西?

    Ecstore中的App不是手机上的App,它是一个Web应用包,集合了Web应用的后端PHP程序.Mysql数据表定义,以及前端HTML+CSS+JS展现,通常是实现某个业务功能,如购物车.促销.支 ...

  3. Python学习笔记四,dict和set

    dict是字典dictionary的缩写,他存放的是键值对key/value,用花括号表示,格式为d={'micheal':99,'jack':88} 当我们访问的时候直接print(d['miche ...

  4. awk之基本信息

    awk 利用RS来分割文本,分割后形成一条一条的record awk 利用FS来分割record,分割后形成一段一段的field field由一串一串的字符串构成 默认的RS是换行符 默认的FS是空格 ...

  5. web.py安装

    web.py 是python的一个web插件,用于发布web服务安装下载web.py https://github.com/webpy/webpy 安装 解压下载的rar 打开控制台并定位到此文件夹输 ...

  6. Yogurt factory(POJ 2393 贪心 or DP)

    Yogurt factory Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8205   Accepted: 4197 De ...

  7. 使用NPOI将数据库里信息导出Excel表格并提示用户下载

    使用NPOI进行导出Excel表格大家基本都会,我在网上却很少找到导出Excel表格并提示下载的 简单的代码如下 //mvc项目可以传多个id以逗号相隔的字符串 public ActionResult ...

  8. SQL Server 索引的图形界面操作 <第十二篇>

    一.索引的图形界面操作 SQL Server非常强大的就是图形界面操作.关于索引方面也一样那么强大,很多操作比如说重建索引啊,查看各种统计信息啊,都能够通过图形界面快速查看和操作,下面来看看SQL S ...

  9. iframe父子页面互调方法和属性

    1.iframe子页面调用 父页面js函数 子页面调用父页面函数只需要写上window.praent就可以了.比如调用a()函数,就写成: window.parent.a(); 子页面取父页面中的标签 ...

  10. poj 2115 C Looooops(推公式+扩展欧几里得模板)

    Description A Compiler Mystery: We are given a C-language style for loop of type for (variable = A; ...