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 ...
随机推荐
- 二进制补码:Why & How
二进制补码:Why & How 学习计算机原理或者语言的底层操作难免会遇到用二进制补码表示负数的问题.由于一些书本上对于采用补码的原因没有详细解释,很多人会认为这只是一种规定,但实际上采用补码 ...
- [最短路,floyd] Codeforces 1202B You Are Given a Decimal String...
题目:http://codeforces.com/contest/1202/problem/B B. You Are Given a Decimal String... time limit per ...
- 利用 MinIO 轻松搭建静态资源服务
目录 1 引言 2 MinIO 简介 3 MinIO 运行与静态资源使用 3.1 MinIO 获取 3.2 MinIO 启动与运行 3.2.1 前台简单启动 3.2.2 后台指定参数运行 3.2.3 ...
- TCP、UDP服务器模型 在网络程序里面,通常都是一
TCP.UDP服务器模型 在网络程序里面,通常都是一个服务器处理多个客户机,为了出个多个客户机的请求,服务器端的程序有不同的处理方式. 目前最常用的服务器模型: 循环服务器:循环服务器在同一时刻只能响 ...
- python之进程,线程
什么是进程(process)? 程序并不能单独运行,只有将程序装载到内存中,系统为它分配资源才能运行,而这种执行的程序就称之为进程.程序和进程的区别就在于:程序是指令的集合,它是进程运行的静态描述文本 ...
- API开放平台接口设计-------基于OAuth2.0协议方式
1,简介OAuth http://www.ruanyifeng.com/blog/2019/04/oauth_design.html OAuth 是什么? http://www.ruanyifeng. ...
- 【动态规划】最佳加法表达式(百练oj4152)
总时间限制: 1000ms 内存限制: 65536kB 描述 给定n个1到9的数字,要求在数字之间摆放m个加号(加号两边必须有数字),使得所得到的加法表达式的值最小,并输出该值.例如,在1234中摆放 ...
- ArrayList中的Iterator详解
每个实现Iterable接口的类必须提供一个iterator方法,返回一个Iterator对象,ArrayList也不例外 public Iterator<E> iterator() { ...
- IIS 组成
HTTP.sys http.sys 侦听来自网络的 HTTP 请求,将它们传递到 IIS 并返回响应. 它是一种可以从命令行停止和启动的服务. "NET STOP HTT ...
- 多线程之旅(Task 任务)
一.Task(任务)和ThreadPool(线程池)不同 源码 1.线程(Thread)是创建并发工具的底层类,但是在前几篇文章中我们介绍了Thread的特点,和实例.可以很明显发现局限性 ...