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. jsUnpacker

    EVAL function executeEval(){ let evalCodeElt = document.getElementById("eval_code"); let e ...

  2. 为 DirectAccess 设计 DNS 基础结构

    TechNet 库Windows ServerWindows Server 2008 R2 und Windows Server 2008浏览 Windows Server 技术NetworkingD ...

  3. 从事IT业一个8年老兵转行前的自我总结1——初爻

    现在,本人已离开这个呆了8年的软件行业了.回想自己从半路出家,从实施开始做起,最终在一家外企做项目经理PM结束了自己的软件职业生涯.从一张白纸的自学开始,做过项目实施,客户培训,拿过需求,开发,架构设 ...

  4. 《Cracking the Coding Interview》——第10章:可扩展性和存储空间限制——题目7

    2014-04-24 22:06 题目:搜索引擎问题,如果有一列100台服务器的集群,用来响应查询请求,你要如何设计query分发和cache策略? 解法:query分发可以用计算数字签名并对机器数取 ...

  5. 【java下午茶】12306的双人票

    明天下午就要和客户谈需求了,今天还在列车上假象着明天的情景,由于这是一个旅游的项目,所以想尽可能设计得人性化一些. 不过有件很不爽的事情就是和老公的位子是分开的,虽然我们订的是连坐号.就这个问题也是我 ...

  6. Python学习4,字符串

    字符串这个东西主要靠记,多打打就好了. _author_ = "Happyboy" name = "my \tname is happyboy and i am 66 y ...

  7. inspect流程

    当node节点state为manage时,可执行inspector ironic node-set-provision-state <node_uuid> manage ironic no ...

  8. mac book pro 安装keras (无gpu)

    转自http://www.jianshu.com/p/01dc42595733 注:Macbook pro 13' 没有NVIDIA的显卡,没办法CUDA编程,所以下面都是CPU编程. 1. 安装ho ...

  9. (转)彻底隐藏Nginx版本号的安全性与方法

    Nginx默认是显示版本号的,如: [root@bkjz ~]# curl -I www.nginx.orgHTTP/1.1 200 OKServer: nginx/0.8.44Date: Tue, ...

  10. MySQL常用客户端 命令

    登录MySQL mysql -h localhost -uroot -p 授权指定用户访问指定数据库 GRANT ALL ON cookbook.* TO 'cbuser'@'localhost' I ...