Dragon Ball

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.





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
 
#include<stdio.h>
#include<string.h>
int pre[1010],num[1010],time[1010],Case=1;
int find(int x)
{
if(x==pre[x])
return x;
int p=pre[x];
time[x]+=time[p];
return pre[x];
}
void join(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
{
pre[fx]=fy;
num[fy]+=num[fx];
time[fx]++;
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int i,u,v,m,n;
char ch;
scanf("%d%d",&m,&n);
for(i=0;i<=m;i++)
{
pre[i]=i;
time[i]=0;num[i]=1;
}
while(n--)
{
getchar();
scanf("%c%d",&ch,&u);
if(ch=='T')
{
scanf("%d",&v);
join(u,v);
}
else
{
v=find(u);
printf("Case %d:\n",Case++);
printf("%d %d %d\n",v,num[v],time[u]);
}
}
}
return 0;
}

Dragon Ball--hdoj的更多相关文章

  1. HDU 4362 Dragon Ball 贪心DP

    Dragon Ball Problem Description   Sean has got a Treasure map which shows when and where the dragon ...

  2. Kattis dragonball1 Dragon Ball I(最短路)

    There is a legendary tale about Dragon Balls on Planet X: if one collects seven Dragon Balls, the Dr ...

  3. 龙珠 超宇宙 [Dragon Ball Xenoverse]

    保持了动画气氛实现的新时代的龙珠视觉 今年迎来了[龙珠]系列的30周年,为了把他的魅力最大限度的发挥出来的本作的概念,用最新的技术作出了[2015年版的崭新的龙珠视觉] 在沿袭了一直以来优秀的动画世界 ...

  4. HDU-3872 Dragon Ball 线段树+DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3872 题意:有n个龙珠按顺序放在一列,每个龙珠有一个type和一个权值,要求你把这n个龙珠分成k个段, ...

  5. HDU 4362 Dragon Ball 线段树

    #include <cstdio> #include <cstring> #include <cmath> #include <queue> #incl ...

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

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

  7. hdu 3635 Dragon Balls(并查集)

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

  8. hdu 3635 Dragon Balls (带权并查集)

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

  9. hdu 3635 Dragon Balls

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

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

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

随机推荐

  1. 五分钟学习React(六):元素(Element)和组件(Component)

    俗话说"万丈高楼平地起",从这一期开始,我们将使用基于Webpack+Babel的React学习React框架中的一些基础概念.在学习React的过程中经常会把Element.Cl ...

  2. 关于改变安卓Button样式,这里有一个好方法。

    首先,在drawable下创建一个新的xml文件(例如我创建的为button.xml).然后在里面输入以下代码. <item> <shape> <gradient and ...

  3. Python语言之常用函数

    1.input(),raw_input() input() = eval( raw_input() ) 其中raw_input()将所有的输入当做字符串处理. eval(str [,globals [ ...

  4. 请不要继续使用VC6.0了!

    很多次和身边的同学交流,帮助同学修改代码,互相分享经验,却发现同学们依然在使用老旧的VC6.0作为编程学习的软件,不由得喊出:“请不要继续使用VC6.0了!”. VC6.0作为当年最好的IDE(集成开 ...

  5. Memcached 之缓存雪崩现象、实际案例和缓存无底洞现象

    一.缓存雪崩现象 由于集群中某个memcached服务器宕机的原因,造成集群中的服务器命中率下降.只能通过访问数据库得到数据,是的数据库的压力倍增,造成数据库服务器崩溃.重启数据库还是会崩溃,但是数据 ...

  6. 用批处理实现垃圾文件清除/自动关机/清除copy病毒

    晚上睡觉之前为了下emule经常使用命令shutdown,最近受一个小程序影响想做个自动关机的批处理文件免的麻烦!网上有高手做了个,不过运行时出 现一个绑定错误,at也不能执行,所以后来自己做了简化版 ...

  7. python tips:最内嵌套作用域规则,闭包与装饰器

    在作用域与名字空间提到,python是静态作用域,变量定义的位置决定了变量作用的范围.变量沿着local,global,builtins的路径搜索,直觉上就是从里到外搜索变量,这称为最内嵌套作用域规则 ...

  8. dmidecode输出详解

    一.先来看几个用dmidecode查看内存信息的例子. 1.查看内存槽数.那个槽位插了内存,大小是多少 [root@jiangyi01.sqa.zmf /home/ahao.mah] #dmideco ...

  9. 一步一步实现基于GPU的pathtracer(二):求交算法

    不管是哪种全局光照算法,最根本的都要落实到光线与物体的求交.主要分为光线与参数曲面和非参数曲面的求交,典型的参数曲面有球.盒.圆柱等基本体及基本体的组合体,以及一些更为复杂的参数曲面.非参数曲面就是所 ...

  10. 构造函数和初始化表、this指针与常函数、析构函数、拷贝构造与拷贝赋值(day05)

    十四 构造函数和初始化表 ... 初始化表 )语法形式 class 类名{ 类名(形参表):成员变量1(初值),...{} }; )必须要使用初始化表的场景 -->如果有类 类型的成员变量,而该 ...