Dragon Balls

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5335    Accepted Submission(s): 2009

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个城市,每个城市都有一个龙珠,一次T a b操作表示将a城市里面的龙珠全部转移到b城市,一次Q a操作表示问a龙珠当前在几号城市,这个城市有多少龙珠,以及龙珠
被转移了多少次。
题解:这个题难点在于计算龙珠的转移次数..要在状态压缩里面做。。我怎么没想到 - - 还有就是记录前结点一直往上找的方法。
#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(并查集)的更多相关文章

  1. HDU 3635 并查集+路径压缩+记录每个点移动次数

    题意: 给定n个点 oper个操作 每个点有1个龙珠 下面2种操作: T u v 把u点所有龙珠搬到v Q u  问u点当前所在城市 u点所在城市有几个龙珠 u点被移动几次 思路: 并查集可以求出 u ...

  2. hdu 4514 并查集+树形dp

    湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  3. HDU 3926 并查集 图同构简单判断 STL

    给出两个图,问你是不是同构的... 直接通过并查集建图,暴力用SET判断下子节点个数就行了. /** @Date : 2017-09-22 16:13:42 * @FileName: HDU 3926 ...

  4. HDU 4496 并查集 逆向思维

    给你n个点m条边,保证已经是个连通图,问每次按顺序去掉给定的一条边,当前的连通块数量. 与其正过来思考当前这边会不会是桥,不如倒过来在n个点即n个连通块下建图,检查其连通性,就能知道个数了 /** @ ...

  5. HDU 1232 并查集/dfs

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...

  6. HDU 2860 并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=2860 n个旅,k个兵,m条指令 AP 让战斗力为x的加入y旅 MG x旅y旅合并为x旅 GT 报告x旅的战斗力 ...

  7. hdu 1198 (并查集 or dfs) Farm Irrigation

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1198 有题目图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇 ...

  8. hdu 1598 (并查集加贪心) 速度与激情

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1598 一道带有贪心思想的并查集 所以说像二分,贪心这类基础的要掌握的很扎实才行. 用结构体数组储存公 ...

  9. hdu 4496(并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4496. 思路:简单并查集应用,从后往前算就可以了. #include<iostream> ...

随机推荐

  1. cocos2d-x 3.0环境配置(转)

    cocos2d-x 3.0发布有一段时间了,作为一个初学者,我一直觉得cocos2d-x很坑.每个比较大的版本变动,都会有不一样的项目创建方式,每次的跨度都挺大…… 但是凭心而论,3.0RC版本开始 ...

  2. unity2D技术学习与整理

    目前有关unity2D的教程以及原理几乎都是国外的.我在这方面也是新手,看了一些例子有很多不懂的地方. 这个网站提供的教程很有参考价值 http://brackeys.com/ 还有这个 http:/ ...

  3. DOS程序员手册(七)

    第11章      中断处理程序 本章将深入到DOS系统内部探讨中断处理程序的内容.与其他计算机编程不一样, 中断处理程序这个名词听起来就很难懂.用最简单的话来说,中断处理程序就是对应于中 断激活的程 ...

  4. js限制文本框只能输入特定字符

    限制只能输入数字 // ---------------------------------------------------------------------- // <summary> ...

  5. 【Radial Basis Function Network】林轩田机器学习技法

    这节课主要讲述了RBF这类的神经网络+Kmeans聚类算法,以及二者的结合使用. 首先回归的了Gaussian SVM这个模型: 其中的Gaussian kernel又叫做Radial Basis F ...

  6. windows下Tomcat安装

    环境Windows 64位 jdk1.8 1.Tomcat安装 官网地址:http://tomcat.apache.org/download-90.cgi 下载安装包,安装之后进行解压 2.修改htt ...

  7. js 请求异常重连或断线后联网重连机制(ajax)

    转到到 https://blog.csdn.net/mengtoumingren/article/details/80296788

  8. C# Socket通信的服务器与客户端

    客户端代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using S ...

  9. servlet 图片流

    http://www.cnblogs.com/focusj/archive/2011/04/30/2057577.html https://segmentfault.com/q/10100000004 ...

  10. npm & npm config

    npm command show npm config https://docs.npmjs.com/cli/config https://docs.npmjs.com/cli/ls https:// ...