/*
题意:有N个城市, 每一个城市都有一个龙珠(编号与城市的编号相同),有两个操作
T A ,B 将标号为A龙珠所在城市的所有的龙珠移动到B龙珠所在城市中! 思路:并查集 (压缩路径的时候将龙珠移动的次数进行更新)
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define M 10005
using namespace std; int f[M];//表示龙珠 i 所在的城市标号
int Tcnt[M];//记录每个龙珠移动的次数
int Scnt[M];//记录每个城市中龙珠总个数 int getFather(int x){
if(x==f[x])
return x; int ff=getFather(f[x]);
Tcnt[x]+=Tcnt[f[x]];//每一个龙珠移动的次数+=其依附的父亲龙珠移动的次数
f[x]=ff;
return f[x];
} void Union(int a, int b){
int fa=getFather(a);
int fb=getFather(b);
if(fa==fb) return;
f[fa]=fb;
Scnt[fb]+=Scnt[fa];//将fa城市的龙珠全部移动到fb城市中!
Scnt[fa]=;
Tcnt[fa]+=;//a球移动次数+1
} int main(){
int t, a, b;
int n, m;
char ch[];
scanf("%d", &t);
for(int cc=; cc<=t; ++cc){
printf("Case %d:\n", cc);
scanf("%d%d", &n, &m);
memset(Tcnt, , sizeof(int)*(n+));
for(int i=; i<=n; ++i)
f[i]=i, Scnt[i]=;
while(m--){
scanf("%s", ch);
if(ch[]=='T'){
scanf("%d%d", &a, &b);
Union(a, b);
}
else {
scanf("%d", &a);
int ff = getFather(a);
printf("%d %d %d\n", ff, Scnt[ff], Tcnt[a]);
}
}
}
return ;
}

hdu3635 Dragon Balls(带权并查集)的更多相关文章

  1. hdu 3635 Dragon Balls (带权并查集)

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  2. POJ 1703 Find them, Catch them(带权并查集)

    传送门 Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42463   Accep ...

  3. (中等) POJ 1703 Find them, Catch them,带权并查集。

    Description The police office in Tadu City decides to say ends to the chaos, as launch actions to ro ...

  4. POJ 1703 Find them, Catch them【种类/带权并查集+判断两元素是否在同一集合/不同集合/无法确定+类似食物链】

      The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the ...

  5. [NOIP摸你赛]Hzwer的陨石(带权并查集)

    题目描述: 经过不懈的努力,Hzwer召唤了很多陨石.已知Hzwer的地图上共有n个区域,且一开始的时候第i个陨石掉在了第i个区域.有电力喷射背包的ndsf很自豪,他认为搬陨石很容易,所以他将一些区域 ...

  6. poj1417 带权并查集 + 背包 + 记录路径

    True Liars Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2713   Accepted: 868 Descrip ...

  7. poj1984 带权并查集(向量处理)

    Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 5939   Accepted: 2 ...

  8. 【BZOJ-4690】Never Wait For Weights 带权并查集

    4690: Never Wait for Weights Time Limit: 15 Sec  Memory Limit: 256 MBSubmit: 88  Solved: 41[Submit][ ...

  9. hdu3038(带权并查集)

    题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=3038 题意: n表示有一个长度为n的数组, 接下来有m行形如x, y, d的输入, 表示 ...

随机推荐

  1. 我的Sharepoint表单使用

    采用客户端验证和后台异步验证.

  2. 网络流EK

    #include <iostream> #include <queue> #include <string.h> #define MAX 302 using nam ...

  3. 'MAMapKit/MAMapKit.h' file not found

    1.应该是derived data没清导致的.在Window -> Organizer -> Projects,找到你这个项目,然后点下右边derived data后边的delete按钮. ...

  4. Memcached & Redis使用

    Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度.Memcached ...

  5. TextView支持的XML属性及相关方法

    1.android:auyoLink----->setAutoLinkMask(int) ----->是否将符合指定格式的文字转换为可单击的超链接形式 2.android:autoText ...

  6. iOS开发--Block

    iOS开发--Block 1.什么是Block,block 的作用 ui开发和网络常见功能实现回调,按钮的事件处理方法是回调方法以及网络下载后的回调处理 (1)按钮 target-action   一 ...

  7. 修改TFS2013服务账户或者密码

    修改TFS2013服务账户或者密码 TFS作为微软软件开发的全生命周期管理解决方案,可以很好的与windows的域管理结合使用,方便多系统下用户的管理和授权.如果TFS使用的服务账户设置的域账户密码过 ...

  8. 扒皮下GitHub 404的图片层次轴动特效

    今天要克隆的前端特效非常有意思,可以参见GitHub404页面 https://github.com/vajoy/master/index.html 记得之前华为在站酷发布EMUI设计大赛的主页也用了 ...

  9. ddms(基于 Express 的表单管理系统)源码学习

    ddms是基于express的一个表单管理系统,今天抽时间看了下它的代码,其实算不上源码学习,只是对它其中一些小的开发技巧做一些记录,希望以后在项目开发中能够实践下. 数据层封装 模块只对外暴露mod ...

  10. Linux split拆分文件

    200 ? "200px" : this.width)!important;} --> 介绍 split可以将一个大文件拆分成指定大小的多个文件,并且拆分速度非常的快,拆分一 ...