博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 3635 Dragon Balls (并查集)
阅读量:4360 次
发布时间:2019-06-07

本文共 2973 字,大约阅读时间需要 9 分钟。

Dragon Balls

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3245    Accepted Submission(s): 1252

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

 

题意:有n个城市,开始时每个城市只有一个球,编号1到n。两种操作,一种是(T,a,b),把第a个球所在城市的所有球运送到b号球所在的城市,另一种是(Q,a),查询第a个球运送到了第X个城市,第X个城市有Y个球,第a个球被运输了Z次,输出X Y Z。
 
思路:并查集,union过程维护一个球总数sum,以及find过程维护运输次数t
 
#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;const int INF=1<<30;const double eps=1e-6;const int N = 10000;int fa[N],sum[N],t[N];int n,m;int find(int x){ if(fa[x]==x) return x; else { int tmp=find(fa[x]); t[x]+=t[fa[x]]; return fa[x]=tmp; }}void run(){ static int cas = 1; printf("Case %d:\n",cas++); char s[5]; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) fa[i]=i,sum[i]=1,t[i]=0; int u,v; while(m--) { scanf("%s",s); if(s[0]=='T') { scanf("%d%d",&u,&v); u=find(u); v=find(v); if(u!=v) { fa[u]=v; sum[v]+=sum[u]; t[u]++; } } else { scanf("%d",&u); v=find(u); printf("%d %d %d\n",v,sum[v],t[u]); } }}int main(){ freopen("case.txt","r",stdin); int _; scanf("%d",&_); while(_--) run(); return 0;}

 

转载于:https://www.cnblogs.com/someblue/p/3944445.html

你可能感兴趣的文章
【delete】Android自定义控件(四) 自定义ImageView动态设置ImageView的高度
查看>>
HDUOJ------(1230)火星A+B
查看>>
Servlet
查看>>
基于jquery地图特效全国网点查看代码
查看>>
【leetcode】867 - Transpose Matrix
查看>>
selenium动作链
查看>>
敏捷外包工程系列之二:人员结构(敏捷外包工程,敏捷开发,产品负责人,客户价值)...
查看>>
《设计你的人生》的部分经典语录
查看>>
mustache多次渲染和多个赋值
查看>>
《Flutter 实战》开源电子书
查看>>
Python 键盘记录
查看>>
HDU 1381 Crazy Search
查看>>
PLSQL
查看>>
修改计算机名
查看>>
Android-Activity的启动模式
查看>>
禅道项目管理系统整合Selenium IDE的思路
查看>>
Web 前端开发精华文章推荐(HTML5、CSS3、jQuery)【系列二十三】
查看>>
linux-nohup命令
查看>>
[LeetCode OJ] Roman to Integer
查看>>
三次握手和四次挥手
查看>>