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颗龙珠总共到目前为止被转移了多少 ...
随机推荐
- C#世界中的委托
委托是C#最重要的特性之一,C#后面的所有特性基本都是建立在委托的基础上的. 1.C#委托是什么? 可以把C#的委托理解为函数的一个包装,它使得C#中的函数可以作为参数来被传递.如果你学过C++,可以 ...
- LINQ 101——约束、投影、排序
什么是LINQ:LINQ 是一组 .NET Framework 扩展模块集合,内含语言集成查询.集合以及转换操作.它使用查询的本机语言语法来扩展 C# 和 Visual Basic,并提供利用这些功能 ...
- DependencyProperty
<Window x:Class="DependencyPropertyDemo.MainWindow" xmlns="http://schemas.microsof ...
- Gherkin学习笔记
前言 由于项目准备使用BDD模式开发,所以最近在学习BDD,同时也记录下自己的学习点滴. 参考原文:https://github.com/cucumber/cucumber/wiki/Gherkin ...
- jQuery动态添加的元素中处理字符串溢出后在指定字符数后添加省略号
"+[jsonData[i].questitle.lenth>40?jsonData[i].questitle.substring(0,40)+"...":json ...
- yii2源码学习笔记(八)
Action是所有控制器的基类,接下来了解一下它的源码.yii2\base\Action.php <?php /** * @link http://www.yiiframework.com/ * ...
- 谈memcache和memcached的区别
用了段时间的memcache和memcached总结下认识,看很多人在用cache的时候,刚刚都没有搞清楚memcache 和 memcached的区别,还有就是使用的时候基本都是 get/set ...
- [Linux]Ubuntu下如何将普通用户提升到root权限
转至:http://jingyan.baidu.com/album/6181c3e0780131152ef153ff.html?picindex=0&qq-pf-to=pcqq.c2c 在u ...
- AngularJS测试框架 karma备忘
AngularJS测试框架karma安装 安装karma $ --save-dev 安装karma组件 $ npm install karma-jasmine karma-chrome-launche ...
- u-boot子目录Makefile分析
一.概述 u-boot的子目录Makefile是整个Makefile体系的重要组成部分,决定了对应子目录的编译过程. 二.分析 以cpu/arm920t/Makefile为例进行说明 (1)首先,调用 ...