F - 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.
InputThe first line of the input is a single positive 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)OutputFor each test case, output the test case number formated as sample output. Then for each query, output a line with three integers X Y Z saparated by a blank space.Sample Input
2
3 3
T 1 2
T 3 2
Q 2
3 4
T 1 2
Q 1
T 1 3
Q 1
Sample Output
Case 1:
2 3 0
Case 2:
2 2 1
3 3 2
并查集带权问题
//这道题感觉有一个坑,,,就是当一个城市的龙珠被挪走,那么这个城市就没用了,不会再有龙珠移动过来了
//因为T A B (A和B都是龙珠),所以根节点的龙珠的下标就对应着他所在的城市,
//所以寻找某一个龙珠所在的城市我们只需要求它的根节点就好了。求一个城市中龙珠的个数,
//无非就是求这个根节点这棵树上有多少个子节点(自己也算)。主要是求龙珠移动的次数。
//我们开一个数组记录,先初始化为0,当连接某两个龙珠时,我们连接的是这两个龙珠的根节点,
//然后我们先让根节点移动,然后让根节点的上一个节点移动,,,以此类推,,。
#include<cstdio>
#include<cstring>
using namespace std;
const int N=1E5+;
int fa[N];//记录父节点
int son[N];//记录树的大小
int ran[N];//记录移动次数
int find(int x){
if(x==fa[x])
return fa[x];
else {
int k=fa[x];
fa[x]=find(fa[x]);
ran[x]+=ran[k];//这里主要是用来传递根节点的移动。
return fa[x];
}
} void join(int x,int y){
int fx=find(x),fy=find(y);
if(fx!=fy){
fa[fx]=fy;
son[fy]+=son[fx];
ran[fx]++;//先让根节点移动
}
}
//初始化
void inint(int x){
memset(ran,,sizeof(ran));
for(int i=;i<=x;i++){
fa[i]=i;
son[i]=;
}
}
int main(){
int t,kk=;
scanf("%d",&t);
while(t--){
kk++;
printf("Case %d:\n",kk);
int n,m;
scanf("%d%d",&n,&m);
inint(n);
for(int i=;i<=m;i++){
char a[];
scanf("%s",a);
if(a[]=='T'){
int x,y;
scanf("%d%d",&x,&y);
join(x,y);
}
else if(a[]=='Q'){
int z;
scanf("%d",&z);
int m=find(z);
printf("%d %d %d\n",m,son[m],ran[z]);
}
}
}
return ;
}
F - Dragon Balls的更多相关文章
- HDU 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 ...
- 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
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- 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, ...
- Dragon Balls
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- HDU 3635 Dragon Balls(超级经典的带权并查集!!!新手入门)
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
随机推荐
- 图-搜索-BFS-DFS-126. 单词接龙 II
2020-03-19 13:10:35 问题描述: 给定两个单词(beginWord 和 endWord)和一个字典 wordList,找出所有从 beginWord 到 endWord 的最短转换序 ...
- JUnit 5基础指南
A Guide to JUnit 5 准备 添加maven依赖: <dependency> <groupId>org.junit.jupiter</groupId> ...
- "xaml+cs"桌面客户端跨平台初体验
"Xaml+C#"桌面客户端跨平台初体验 前言 随着 .Net 5的到来,微软在 .Net 跨平台路上又开始了一个更高的起点.回顾.Net Core近几年的成果,可谓是让.Ne ...
- war 和 war exploded
IDEA 开发项目时,部署 tomcat 的 Deployment 选项出现: war 模式 可以称之为发布模式.先将 WEB 工程打成 war 包,然后再将其上传到服务器进行发布. war expl ...
- iOS 预渲染加速图像显示
使用 UITableView 时,发现滚动时的性能还不错,但来回滚动时,第一次显示的图像不如再次显示的图像流畅,出现前会有稍许的停顿感. 于是猜想显示过的图像肯定是被缓存起来了,查了下文档后发现果然如 ...
- CSS躬行记(2)——伪类和伪元素
一.伪类选择器 伪选择器弥补了常规选择器的不足,能够实现一些特殊情况下的样式,例如在鼠标悬停时或只给字符串中的第一个字符指定样式.与类选择器类似,可以从HTML元素的class属性中查看到,但伪选择器 ...
- C. Yet Another Walking Robot Round #617 (Div. 3)()(map + 前后相同状态的存储)
C. Yet Another Walking Robot time limit per test 1 second memory limit per test 256 megabytes input ...
- 简单记录下RestTemplate 中postForObject调用例子
学无止境! 今天无意中做了下RestTemplate调用demo,简单的尝试了下一个项目调用另一个项目接口示例 在A项目中创建可访问controller 然后在B项目中进行调用 调用成功
- 我去,还在这样读写 excel 这也太低效了吧!
前言 博文地址:https://sourl.cn/SsD3AM 最近读者小 H 给小黑哥发来私信: 小黑哥,最近我在负责公司报表平台开发,需要导出报表到 excel 中.每次使用 POI 开发,都要写 ...
- django中设置定时任务
django中设置定时任务 在django中设置定时任务我们可以借用django-crontab这个第三包来实现 django-crontab只能在linux系统下使用 安装: pip install ...