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 ...
随机推荐
- ajax创建对象
<script> function createAjax(){ var request=false; //window对象中有X ...
- Pubwin服务端重装(安装)教程
此博文已移至爬不稳独立博客:www.pubwin2009.net传送门:http://www.pubwin2009.net/index.php/post/6.html 一,卸载原来服务端和数据库. 1 ...
- service2008 word 导入导出 配置问题
除了配置 com组件权限 64位系统还要加 下面的文件 C:\Windows\SysWOW64\config\systemprofile\Desktop C:\Windows\Temp 也要加权限
- 【温故而知新-万花筒】C# 异步编程 逆变 协变 委托 事件 事件参数 迭代 线程、多线程、线程池、后台线程
额基本脱离了2.0 3.5的时代了.在.net 4.0+ 时代.一切都是辣么简单! 参考文档: http://www.cnblogs.com/linzheng/archive/2012/04/11/2 ...
- css案例学习之table tr th td ul li实现日历
效果 代码 <html> <head> <title>Calendar</title> <style> <!-- .month { b ...
- Linux系统编程(20)——信号基本概念
信号及信号来源 信号是在软件层次上对中断机制的一种模拟,在原理上,一个进程收到一个信号与处理器收到一个中断请求可以说是一样的.信号是异步的,一个进程不必通过任何操作来等待信号的到达,事实上,进程也不知 ...
- HTML5学习摘录
设计原理 不是规范里都包含什么,而是规范里为什么会包含它们,以及在设计这个规范的时候,设计者们是怎么看待这些东西的. 发展史:HTML2.0——>HTML3.2——>HTML4.0.1—— ...
- HDU 3634 City Planning (离散化)
City Planning Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- MonoDevelop with Visual Studio to Linux and Mac OSX maintaining a single code base for all platforms.
Home | Screenshots | Download | Contact | FAQ | Documentation | Development | Search MonoDevelop i ...
- 修改vim中的tab为4个空格
记录一下,避免用时还得搜........ 1.临时修改 在vi中,set tabstop=4 或 set ts=4 2.永久修改 vi --version 查看要修改的文件 如果是vim的话,修改~/ ...