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 ...
随机推荐
- RFC1867 HTTP file upload
RFC1867 HTTP file upload RFC1867 is the standard definition of that "Browse..." button tha ...
- C# 如何实现WinForm程序自重启(重新启动自己)
重启的时间间隔方法 private void Restart() { Thread thtmp = new Thread(new ParameterizedThreadStart(run)); obj ...
- Oracle数据库基础知识总结(一)
数据库名.实例名.数据库域名.全局数据库名.服务名,这是几个令很多初学者容易混淆的概念.相信很多初学者都与我一样被标题上这些个概念搞得一头雾水. 我们现在就来把它们弄个明白. 一.数据库名 什么是数据 ...
- texi格式文件的读取
使用texi2html可以将texi格式的文件转换成html格式的文件. sudo apt-get install texi2html 在对应目录下 texi2html filename.texi 或 ...
- 【sqli-labs】 less43 POST -Error based -String -Stacked with tiwst(POST型基于错误的堆叠变形字符型注入)
和less42一样 login_user=&login_password=1');insert into users(id,username,password) value(15,'root' ...
- 【sqli-labs】 less24 POST- Second Order Injections *Real treat* -Stored Injections (POST型二阶注入 *真的好玩?* 存储注入)
简单登陆浏览一遍后,发现是一个登陆注册修改密码的应用 审查一下代码 登陆页面的username,password使用了转义 注册页面的参数也进行了转义处理 但是在修改password的页面,直接从se ...
- GET 请求控制器 返回绑定后HTML
//$.get("/Home/index/" + $("#S_BookName").val(), function (data) { //MVC控制器返回Vie ...
- Apache_安装
1.下载安装包 网址:https://www.apachelounge.com/download/,下载你需要的安装包 2.解压文件到指定安装目录 3.修改配置文件 使用文本编辑器打开配置文件 con ...
- BZOJ 4259: 残缺的字符串 FFT_多项式
Code: #include<bits/stdc++.h> #define maxn 1200000 using namespace std; void setIO(string s) { ...
- [MySQL优化案例]系列 — 分页优化
通常,我们会采用ORDER BY LIMIT start, offset 的方式来进行分页查询.例如下面这个SQL: SELECT * FROM `t1` WHERE ftype=1 ORDER BY ...