题意: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. Java设计模式(八)观察者模式 迭代器模式

    (十五)观察者模式 观察者模式,定义对象间一对多关系,一个对象状态发生改变,全部依赖于它的对象都收到通知而且自己主动更新,观察者与被观察者分开.比如邮件订阅.RSS订阅,假设有更新就会邮件通知你. i ...

  2. PHP实例——输出安全的HTML代码

    原文:PHP实例--输出安全的HTML代码 //输出安全的htmlfunction h($text, $tags = null){ $text = trim($text); //完全过滤注释 $tex ...

  3. java.lang.IllegalAccessError: tried to access field org.slf4j.impl.StaticLoggerBinder.SINGLETON from class org.slf4j.LoggerFactory

    java.lang.IllegalAccessError: tried to access field org.slf4j.impl.Static.. java.lang.IllegalAccessE ...

  4. elasticsearch集群搭建实例

    elasticsearch集群搭建实例 下个月又开始搞搜索了,几个月没动这块还好没有落下. 晚上在自己虚拟机上搭建了一个简易搜索集群,分享一下. 操作系统环境: Red Hat 4.8.2-16 el ...

  5. Android Studio之同一应用创建多个Activity(一)

    1. 2. 3. 4. 本文主要是构建多个Activity的过程.

  6. Java集合之ArrayList源码分析

    1.简介 List在数据结构中表现为是线性表的方式,其元素以线性方式存储,集合中允许存放重复的对象,List接口主要的实现类有ArrayList和LinkedList.Java中分别提供了这两种结构的 ...

  7. Unity3D第三人称摄像机控制脚本

    好久没有敲Blog该.感谢您的留言.注意.私人信件和其他支持,但我似乎没有办法继续自己曾经写了一篇博客系列,因为我在网上找到有关unity3D太少的内容,U3D相关的文章!.. 第三人称视角 第三人称 ...

  8. 【转】android webview设置内容的字体大小

    Enum for specifying the text size. SMALLEST is 50% SMALLER is 75% NORMAL is 100% LARGER is 150% LARG ...

  9. 使用winform控件注意线程绘制界面冲突

    在用.NET Framework框架的WinForm构建GUI程序界面时,如果要在控件的事件响应函数中改变控件的状态,例如:某个按钮上的文本原先叫“打开”,单击之后按钮上的文本显示“关闭”,初学者往往 ...

  10. 自己动手实现Expression翻译器 – Part Ⅱ

    上一节我们了解了Linq查询大体上是如何运转的,并针对SQL表达式进行建模(DbExpression),这一节的重点在于如何将表达式转换为DbExpression. 可以说只要能生成结构清晰的DbEx ...