hdu 3635(并查集)
Dragon Balls
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5335 Accepted Submission(s): 2009
hundred years later, the number of dragon balls 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.
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)
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.
3 3
T 1 2
T 3 2
Q 2
3 4
T 1 2
Q 1
T 1 3
Q 1
2 3 0
Case 2:
2 2 1
3 3 2
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <map>
using namespace std;
const int N =;
int father[N];
int sum[N]; ///记录每个城市龙珠的个数
int mov[N]; ///记录每个点的移动次数
int _find(int x){
if(x!=father[x]){ ///路径压缩更新
int t = father[x];
father[x] = _find(father[x]);
mov[x]+=mov[t];
}
return father[x];
}
void Union(int a,int b){
int x = _find(a);
int y = _find(b);
if(x!=y){
father[x] = y;
mov[x]++;
sum[y]+=sum[x];
sum[x] = ; ///记得清0
}
}
int main()
{
int tcase;
int t = ;
scanf("%d",&tcase);
while(tcase--){
printf("Case %d:\n",t++);
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
father[i] = i;
sum[i] = ;
mov[i] = ;
}
while(m--){
char s[];
scanf("%s",s);
if(s[]=='T'){
int a,b;
scanf("%d%d",&a,&b);
Union(a,b);
}
else{
int a;
scanf("%d",&a);
int b = _find(a);
printf("%d %d %d\n",b,sum[b],mov[a]);
}
}
}
return ;
}
hdu 3635(并查集)的更多相关文章
- HDU 3635 并查集+路径压缩+记录每个点移动次数
题意: 给定n个点 oper个操作 每个点有1个龙珠 下面2种操作: T u v 把u点所有龙珠搬到v Q u 问u点当前所在城市 u点所在城市有几个龙珠 u点被移动几次 思路: 并查集可以求出 u ...
- hdu 4514 并查集+树形dp
湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...
- HDU 3926 并查集 图同构简单判断 STL
给出两个图,问你是不是同构的... 直接通过并查集建图,暴力用SET判断下子节点个数就行了. /** @Date : 2017-09-22 16:13:42 * @FileName: HDU 3926 ...
- HDU 4496 并查集 逆向思维
给你n个点m条边,保证已经是个连通图,问每次按顺序去掉给定的一条边,当前的连通块数量. 与其正过来思考当前这边会不会是桥,不如倒过来在n个点即n个连通块下建图,检查其连通性,就能知道个数了 /** @ ...
- HDU 1232 并查集/dfs
原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...
- HDU 2860 并查集
http://acm.hdu.edu.cn/showproblem.php?pid=2860 n个旅,k个兵,m条指令 AP 让战斗力为x的加入y旅 MG x旅y旅合并为x旅 GT 报告x旅的战斗力 ...
- hdu 1198 (并查集 or dfs) Farm Irrigation
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1198 有题目图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇 ...
- hdu 1598 (并查集加贪心) 速度与激情
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1598 一道带有贪心思想的并查集 所以说像二分,贪心这类基础的要掌握的很扎实才行. 用结构体数组储存公 ...
- hdu 4496(并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4496. 思路:简单并查集应用,从后往前算就可以了. #include<iostream> ...
随机推荐
- Hbase的安装与部署(集群版)
HBase 部署与使用 部署 Zookeeper 正常部署 $ ~/modules/zookeeper-3.4.5/bin/zkServer.sh start 首先保证 Zookeeper 集群的正常 ...
- AD RMS总结
AD RMS 认识篇 AD RMS(Active Directory Right Mangement Servic)活动目录权限服务. 首先我通过了解AD RMS的用途去深入学习AD RMS.在过去用 ...
- CodeIgniter学习笔记四:CI中的URL相关函数,路由,伪静态,去掉index.php
一.URL相关函数 1.加载url模块 加载url有两种方式: a.自动加载:在 application/config/autoload.php 中开启 $autoload['helper'] = a ...
- webstrom11 vue插件配置
直接上图 1. 安装vue插件 2.添加模板 3.指定模板类型 最新的是插件 是 vue.js 创建完 Vue File 文件后 需要在 下面这里关联一下
- PyCharm 解决有些库(函数)没有代码提示
问题描述: 如图,当输入 im. 没有智能提示第三库相应的函数或其他提示. 解决方案: python是动态强类型语言,IDE无法判断Image.open("panda.png")的 ...
- Class对象和反射
1.class对象在java中一切都是对象,从某种意义上,java中的对象可以分为:实例对象和Class对象.实例对象通过new关键得到,那么Class对象呢?Class对象无法通过new关键字获取, ...
- 先立一个书单【flag】,敦促自己温故知新
书单来源david mimno副教授给ML新生的建议博文,外加一部分搜罗的书籍 学习方式:以书籍查看,习题为辅,代码为最终实现方式,分主题进行今年的学习笔记,立此旗为证. 线代 --> 概率统计 ...
- Linux下samba编译与安装(Ubuntu和嵌入式linux)
Ubuntu[i386-linux下安装过程] 1.安装samba $ sudo apt-get install samba $ sudo apt-get install smbfs (可选) $ s ...
- linux备忘录-基本命令
基本命令 将命令分类为获取信息类,文件管理类,目录管理类,文本处理类,系统类,工具类. 获取信息类 uname # 输出所有信息 # 一行输出,空格分割 uname -a # 输出内核名称 uname ...
- TOJ 3046: 招商银行网络系统
3046: 招商银行网络系统 Time Limit(Common/Java):1000MS/3000MS Memory Limit:65536KByteTotal Submit: 12 ...