hdoj 3635 Dragon Balls【并查集求节点转移次数+节点数+某点根节点】
Dragon Balls
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4384 Accepted Submission(s):
1673
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.
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<stdio.h>
#include<string.h>
#define MAX 20000
int set[MAX];
int path[MAX];
int time[MAX];
int find(int fa)
{
int t;
if(fa==set[fa])
return fa;
t=set[fa];
set[fa]=find(set[fa]);
time[fa]+=time[t];
return set[fa];
}
void mix(int x,int y)
{
int fx;
int fy;
fx=find(x);
fy=find(y);
if(fx!=fy)
{
set[fx]=fy;
path[fy]+=path[fx];
time[fx]++;
}
}
int main()
{
int t,n,m,x,y,b,i;
char a;
scanf("%d",&t);
int k=0;
while(t--)
{
scanf("%d%d",&n,&m);
printf("Case %d:\n",++k);
for(i=1;i<=n;i++)
{
set[i]=i;
path[i]=1;
time[i]=0;
}
while(m--)
{
getchar();
scanf("%c %d",&a,&x);
if(a=='T')
{
scanf("%d",&y);
mix(x,y);
}
else
{
y=find(x);//此处必须用一个变量值来表示find(x)
printf("%d %d %d\n",y,path[y],time[x]);
}
}
}
return 0;
}
hdoj 3635 Dragon Balls【并查集求节点转移次数+节点数+某点根节点】的更多相关文章
- hdu 3635 Dragon Balls(并查集)
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- hdu 3635 Dragon Balls(加权并查集)2010 ACM-ICPC Multi-University Training Contest(19)
这道题说,在很久很久以前,有一个故事.故事的名字叫龙珠.后来,龙珠不知道出了什么问题,从7个变成了n个. 在悟空所在的国家里有n个城市,每个城市有1个龙珠,第i个城市有第i个龙珠. 然后,每经过一段时 ...
- Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 并查集求奇偶元环
D. Dividing Kingdom II Long time ago, there was a great kingdom and it was being ruled by The Grea ...
- C. Edgy Trees Codeforces Round #548 (Div. 2) 并查集求连通块
C. Edgy Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- HDU 3018 Ant Trip (并查集求连通块数+欧拉回路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 题目大意:有n个点,m条边,人们希望走完所有的路,且每条道路只能走一遍.至少要将人们分成几组. ...
- hdu 3635 Dragon Balls(并查集应用)
Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, ...
- hdu 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)Tota ...
- hdu 3635 Dragon Balls(并查集)
题意: N个城市,每个城市有一个龙珠. 两个操作: 1.T A B:A城市的所有龙珠转移到B城市. 2.Q A:输出第A颗龙珠所在的城市,这个城市里所有的龙珠个数,第A颗龙珠总共到目前为止被转移了多少 ...
随机推荐
- #define和#undefine的用法
#undef将保持已定义状态且在 作用域内,直到程序结束或者使用#undef 指令取消定义. 预处理器 在此程序中,我们将取消在先前程序中对预处理器的定义. 1 2 3 4 5 6 7 8 9 10 ...
- Python:模块引用
#!/usr/bin/python3 #Filename function.py #导入模块 import sys #导入function.py#function.py 文件import functi ...
- 生成dll文件的示例
看了好多网上写的关于dll文件生成和实用的资料发现多尔不全,都是抄来抄去,有的干脆就是搬用msdn上的原文,实在没有创意和可看的东西.于是本着学和实用的目的自己实践的东西分享给大家. 大前提:使用VS ...
- 中文翻译:pjsip教程(三)之ICE stream transport的使用
1:pjsip教程(一)之PJNATH简介 2:pjsip教程(二)之ICE穿越打洞:Interactive Connectivity Establishment简介 3:pjsip教程(三)之ICE ...
- 读书笔记之 - javascript 设计模式 - 命令模式
本章研究的是一种封装方法调用的方式.命令模式与普通函数有所不同.它可以用来对方法调用进行参数化处理和传送,经过这样处理过的方法调用可以在任何需要的时候执行. 它也可以用来消除调用操作的对象和实现操作的 ...
- Windows phone 之手势识别(Drag)
首先,在XAML页面中,添加如下代码: 前提,别忘了添加 1.引入dll (silverlight for wndows phone toolkit) 2.引入命名空间 xmlns:toolkit=& ...
- SQL70001: This statement is not recognized in this context.
关于错误: SQL70001: This statement is not recognized in this context. 的产生原因以及解决办法. 在SQL Server Databas ...
- 那些年被我坑过的Python——邂逅与初识(第一章)
第一问:为什么学习Python? 虚妖说:为了还债,还技术债,很早接触编程,却一直徘徊,也码了很多代码,却从未真真学会编程! 第二问:什么是Python 是一种以简洁.优雅著称的解释型.动态.强类型的 ...
- namenode启动参数
namenode启动参数:-Xmx153600m -Xms153600m -Xmn4096m -verbose:gc -Xloggc:$LOG_DIR/namenode.gc.log -XX:Erro ...
- python中跟字符串相关的一些操作
公司让用python自动生成代码,以前没看过python.所以匆匆的看了两天python就连猜带蒙就上马开干了..因此好多操作可能看的时候看懂了,用的时候知道有这么个东西,具体用法就忘记了..用到了就 ...