题意:n个城市有n个龙珠,T  a,b 代表将龙珠a移动到b城市,Q a代表查询龙珠a所在城市,该城市有多少颗龙珠,该龙珠移动了多少次。

注意:移动时是将龙珠所在该城市所有龙珠都移动,每个龙珠不会再回到自己以前呆过的城市。就是说假如龙珠1,2,3,都在城市2,那么龙珠就不能再移动了

在路径压缩的时候更新他的距离值

rootxx --> rootx 移动是3,rootx -->x的移动是5 那么 rootxx --> x的移动就是rootxx --> rootx+rootx -->x =  3 + 5 = 8

所以还是向量

在find操作的时候

 int temp = p[x].father;
p[x].father = Find_set(p[x].father);
p[x].trap += p[temp].trap;

先保存他以前父亲的节点,然后更新时将他自己的节点和他原父亲的节点运输次数相加。

这样一层一层更新就可以了

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct bian
{
int sum;
int father;
int trap;
}p[10005]; void Make_set(int n)
{
int i;
for(i = 1; i <= n; i++)
{
p[i].sum = 1;
p[i].father = i;
p[i].trap = 0;
}
} int Find_set(int x)
{
if(x != p[x].father)
{
int temp = p[x].father;
p[x].father = Find_set(p[x].father);
p[x].trap += p[temp].trap;
}
return p[x].father;
} void Union(int a,int b)
{
int x = Find_set(a);
int y = Find_set(b); if(x == y)
return ;
else
{
p[x].father = y;
p[x].trap++;
p[y].sum += p[x].sum;
}
}
int main()
{
int t,cas = 1;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
int i;
Make_set(n);printf("Case %d:\n",cas++);
for(i = 0; i < m; i++)
{
char c[5];
int a,b;
scanf("%s",c);
if(c[0] == 'T')
{
scanf("%d%d",&a,&b);
Union(a,b);
}
else
{
scanf("%d",&a);
int temp = Find_set(a);
printf("%d %d %d\n",temp,p[temp].sum,p[a].trap);
}
}
}
return 0;
}

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

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

    http://acm.hdu.edu.cn/showproblem.php?pid=3635 题意: 有n颗龙珠和n座城市,一开始第i颗龙珠就位于第i座城市,现在有2种操作,第一种操作是将x龙珠所在城 ...

  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. js 控制radio选项

    <input type="radio" name="rk" value="aaa" />1<input type=&quo ...

  2. .net mvc mssql easyui treegrid 及时 编辑 ,支持拖拽

    这里提到了,1个问题,怎么扩展 Easyui 参见: http://blog.csdn.net/chenkai6529/article/details/17528833 @{ ViewBag.Titl ...

  3. AngularJS + CoffeeScript

    AngularJS + CoffeeScript 前端开发环境配置详解 AngularJS 号称 '第一框架' ('The first framework') 确实是名不虚传.由其从jQuery中完全 ...

  4. 间支付系统,DataGridView

    我们通常看到很多的学习使用控制数据库和接口连接--DataGridView,在我们的房间,当我们敲开使用第一遍阶段似该控件--MSHFlexGrid,随着学习的深入,发现我们用到的平台越来越人性化了, ...

  5. 我看PS求职简历照

    看新闻<大学生求职简历照片不PS 连面试机会都没有>,忍不住也谈些自己的看法. 个人意见,尽量求真,这个求真要有平时努力的保证.长相一般的,知道自己是普通一员,按普通的方式行事就可以.即使 ...

  6. 手机发送短信JS验证

    function tj() { var phone = jQuery('#phone').val(); var code = jQuery('#verificationcode').val(); va ...

  7. C++中怎样获取类成员的指针

     我们都知道C++ class中有三种成员函数,static(静态的),nonstatic(非静态的),virtual(虚拟的)各种成员函数的指针各有差别,下面是一个完整的样例:(注意红颜色的差别 ...

  8. javascript 学习总结(二)Array数组

    1.数组常用方法 var colors = ["red", "blue", "green"]; //creates an array wit ...

  9. Strongly connected(hdu4635(强连通分量))

    /* http://acm.hdu.edu.cn/showproblem.php?pid=4635 Strongly connected Time Limit: 2000/1000 MS (Java/ ...

  10. FileWriter字符输出流和FileReader字符输出流

    //FileWriter public class FileWriterDemo { //字符流:适用于文本文件,以字符为单位进行操作,经常和缓冲流一起使用 /**  * 字符流操作步骤:  * 1. ...