hdoj--3635--Dragon Balls(并查集记录深度)
Dragon Balls
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.
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)
2
3 3
T 1 2
T 3 2
Q 2
3 4
T 1 2
Q 1
T 1 3
Q 1
Case 1:
2 3 0
Case 2:
2 2 1
3 3 2
#include<stdio.h>
#include<string.h>
int pre[20000],num[20000],time[20000];
int find(int x)
{
int p;
if(x==pre[x])
return x; p=pre[x];
pre[x]=find(pre[x]);
time[x]+=time[p]; return pre[x];
}
void mem(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,cot=0;
scanf("%d",&t);
while(t--)
{
int n,m,i;
scanf("%d%d",&n,&m);
printf("Case %d:\n",++cot);
for(i=1;i<=n;i++)
{
pre[i]=i;
num[i]=1;
time[i]=0;
}
while(m--)
{
char op[2];
int u,v;
scanf("%s %d",op,&u);
if(op[0]=='T')
{
scanf("%d",&v);
mem(u,v);
}
else
{
v=find(u);
printf("%d %d %d\n",v,num[v],time[u]);
}
}
}
}
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 ...
- 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(并查集应用)
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(加权并查集)2010 ACM-ICPC Multi-University Training Contest(19)
这道题说,在很久很久以前,有一个故事.故事的名字叫龙珠.后来,龙珠不知道出了什么问题,从7个变成了n个. 在悟空所在的国家里有n个城市,每个城市有1个龙珠,第i个城市有第i个龙珠. 然后,每经过一段时 ...
- 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 (MFSet)
Problem - 3635 切切水题,并查集. 记录当前根树的结点个数,记录每个结点相对根结点的转移次数.1y~ 代码如下: #include <cstdio> #include < ...
- UVA1623-Enter The Dragon(并查集)
Problem UVA1623-Enter The Dragon Accept: 108 Submit: 689Time Limit: 3000 mSec Problem Description T ...
- hdu 3635 Dragon Balls
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
随机推荐
- 基于bootstarp的Dailog
oaoDailog开发帮助 1. 帮助文档关键字 boostrap模态框oaoDailog 2. 使用场景 当网页上点击某个按钮需要给予用户提示确认,用户点击确认按钮才 ...
- dotnetnuk错误提醒机制
DotNetNuke.UI.Skins.Skin.AddModuleMessage(this, "关注保存出错.", DotNetNuke.UI.Skins.Controls.Mo ...
- ES:AI 注释
为AI做注解: AI已经出第三版,大的框架没有改变,DNN也没有引入AI这本书.第四版网络版应流出,不知道最终定版如何! 强化学习的方法有大幅度更新,但从策略系统更新范畴看来,没有什么实质的改变,只是 ...
- asp.net mvc学习入门
MVC是什么? M: Model就是我们获取的网页需要的数据 V: View就是我们的aspx页面,注意这是一个不包含后台代码文件的aspx页面.(其实带有.asp.cs文件也不会有编译错误,但是这样 ...
- react-native 横向滚动的商品展示
在app中会有这种页面 像这样商品是横向的,而且要滚动,思路是利用 ScrollView 横向的滚动 思路: (a): 横向滚动的整体作为一个组件 ShopCenter {/*** 横向滚动 *** ...
- linux 下取进程占用 cpu/内存 最高的前10个进程
linux下获取占用CPU资源最多的10个进程,可以使用如下命令组合: ;|head linux下获取占用内存资源最多的10个进程,可以使用如下命令组合: ;|head 命令组合解析(针对CPU的,M ...
- Windows server 2008R2系统登录密码破解
服务器密码忘记,或者被恶意修改,系统被入侵,都是很让人烦心的事情,我试过很多方法,包括使用PE工具删除C盘Windows\System\config里面的SAM文件,可是过程都相当华美,结果都相当杯具 ...
- eas之控制kdtable滚动条
//滚动条支持三种状态 自动 隐藏 显示 public static final int SCROLL_STATE_AUTO=0://自动根据数据判断是否显示或隐藏 public static fin ...
- [模板]FFT
郝神并没有令我明白这个. 但是巨神的题解太强了. #include <iostream> #include <complex> #include <cmath> # ...
- js实现的时间轴效果
今天整理以前的资料发现以前写的一个时间轴效果,当时也是网上找了很久没有找到,就自己写了一个,现在发出来给有需要的人,代码写的可能有点乱. 效果图: 下面是美工做的设计图的效果(有个美工就是好): 下面 ...