/*
题意:有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. easyui的datagrid行的某一列添加链接

    通过formatter方法给easyui 的datagrid 每行增加操作链接. 效果图 jsp代码: <th field="url" width="100&quo ...

  2. java面向对象(封装-继承-多态)

    框架图 理解面向对象 面向对象是相对面向过程而言 面向对象和面向过程都是一种思想 面向过程强调的是功能行为 面向对象将功能封装进对象,强调具备了功能的对象. 面向对象是基于面向过程的. 面向对象的特点 ...

  3. CSS浮动(float,clear)通俗讲解

    首先要知道,div是块级元素,在页面中独占一行,自上而下排列,也就是传说中的流.如下图: 可以看出,即使div1的宽度很小,页面中一行可以容下div1和div2,div2也不会排在div1后边,因为d ...

  4. Secret Codes

    Secret Codes   This is a list of codes that can be entered into the dialer to output the listed info ...

  5. mysql客户端导入sql文件命令

    mysql -h localhost -u root -p dbname < filename

  6. 我的ORM之十二 -- 支持的数据库及差别

    我的ORM索引 支持最好的是SqlServer2005,Sqlserver2008,SqlServer2012 ,后续将支持:MySql,Sqlite,Oracle. 1.分页差别 MsSql 200 ...

  7. 在VS2015 RC打开CTP中创建的工程

    VS2015终于出了RC了!小伙伴们快来安装试用吧,地址在这里,还有新的Windows 10开发工具哦,要不然是开发不了Universal Windows App的,安装前记得卸载CTP版本. 新的R ...

  8. 在GitHub注册账户的过程

    (1)第一步:首先起一个属于自己用户的名字(username),用户名字只能包含字母数字的字符或者单个连字符,不能只用单个连字符开始或者结束(only contain alphanumeric cha ...

  9. Entity Framework 5.0系列之约定配置

    Code First之所以能够让开发人员以一种更加高效.灵活的方式进行数据操作有一个重要的原因在于它的约定配置.现在软件开发越来复杂,大家也都试图将软件设计的越来越灵活,很多内容我们都希望是可配置的, ...

  10. FusionCharts教程文档下载

    FusionCharts是一个Flash的图表组件,它可以用来制作数据动画图表,其中动画效果用的是Adobe Flash 8 (原Macromedia Flash的)制作的flash , Fusion ...