HDU3635 Dragon Balls 解题报告【带权并查集】
来源:互联网 发布:淘宝旗帜 编辑:程序博客网 时间:2024/05/16 08:45
Problem Description
Five 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.
Input
The 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)
Output
For 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
解题报告
题意就是有编号1-n的龙珠,分别放在对应编号的城市里。
支持两种操作,一是把一个城市的龙珠移到另一个城市里去,二一个是输出给定编号的龙珠所在的城市、所在城市的龙珠个数、该龙珠转移到该城的移动次数。
显然这道题需要一个并查集来维护,然而这个并查集还要支持查询次数、龙珠个数,这就意味着我们需要在寻找父节点的过程中统计个数,在合并的过程中更改次数和权值。
代码如下:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N=1e4;int father[2*N+5],w[N+5],cnt[N+5];int T,t,n,m;int find(int a){ if(a==father[a])return a; int temp=father[a]; father[a]=find(father[a]); cnt[a]+=cnt[temp]; return father[a];}void Union(int a,int b){ a=find(a),b=find(b); if(a!=b) { father[a]=b; w[b]+=w[a],w[a]=0; cnt[a]=1; }}int main(){ for(scanf("%d",&T);T;T--) { scanf("%d%d",&n,&m); memset(cnt,0,sizeof(cnt)); for(int i=1;i<=n;i++)father[i]=i,w[i]=1; printf("Case %d:\n",++t); while(m--) { char s[5]; scanf("%s",s); if(s[0]=='T') { int u,v; scanf("%d%d",&u,&v); Union(u,v); } else { int a; scanf("%d",&a); int b=find(a); printf("%d %d %d\n",b,w[b],cnt[a]); } } } return 0;}
- HDU3635 Dragon Balls 解题报告【带权并查集】
- Dragon Balls(hdu3635带权并查集)
- 带权并查集--hdu3635 Dragon Balls
- HDU3635 Dragon Balls 并查集
- HDU3635 Dragon Balls(并查集)
- hdu3635 Dragon Balls--并查集
- HDU3635 Dragon Balls(并查集)
- HDU3635:Dragon Balls(并查集)
- 【hdu3635】Dragon Balls —— 并查集
- HDU 3635 Dragon Balls 带权并查集
- hdu 3635 Dragon Balls (带权并查集)
- hdu 3635 Dragon Balls 带权并查集
- HDU 3635 Dragon Balls(带权并查集)
- HDU 3635 Dragon Balls(带权并查集)
- hdu 3635 Dragon Balls 并查集(带权)
- hdu 3635 Dragon Balls 【基础带权并查集】
- hdu 3635 Dragon Balls(带权并查集)
- HDU 3635 Dragon Balls 带权并查集
- 理查德 · 塞勒获诺贝尔经济学奖!曾出演奥斯卡电影,他让经济学更人性
- 智能生态系统的产业架构与趋势研究
- 博弈论简单题集合 poj1067&poj2234&poj2848&poj2975
- 每日英语阅读(六十八)
- 经典排序算法总结
- HDU3635 Dragon Balls 解题报告【带权并查集】
- Java如何向外提供接口
- android开发之GPS定位详解
- 怎么认证微信服务号-微信公众号使用教程27
- AI大事件 | WaveNet推出了谷歌助手,苹果发布CoreML转换器
- 报名 | 2017英特尔软件开发者大会(成都、西安、广州)
- 还在用ps抠图抠瞎眼?机器学习通用背景去除产品诞生记
- java对外提供服务接口如何写?
- C#获取oracle中某个用户的所有表