这题是一道简单的并查集的运用。龙珠所在的城市、该城市龙珠数目都是很简单的问题,稍微麻烦一点的就是龙珠被移动的次数,因为每一次要移动的是一个城市中所有的龙珠,所以每次移动该城市中所有龙珠的移动次数都要加一。

一开始用二维数组存放每个城市中龙珠的编号,MLE了。接着改用map嵌套queue,却TLE了,不过这个是意料之中的。后来再想想,发现自己有点蠢,其实每个龙珠移动的次数等于它移动的次数加上它根节点移动的次数,这样问题就变得简单了。这就类似于从根节点到当前结点的一个权值累积过程,这时候的Find()函数用递归写会使整个程序变得简单明了。

#include"iostream"
#include"stdio.h"
#include"string.h"
#include"string"
#include"algorithm"
#include"cmath"
#include"vector"
#include"queue"
#include"map"
using namespace std;
const int mx=;
int N,Q;
int fa[mx];
int move_times[mx];
int ball_count[mx]; void Set()
{
int i;
for(i=;i<=N;i++)
{
fa[i]=i;
move_times[i]=;
ball_count[i]=;
}
} int Find(int x)
{
if(x==fa[x]) return x;
int t=fa[x];
fa[x]=Find(fa[x]);
move_times[x]+=move_times[t];
return fa[x];
}
void Union(int x,int y)
{
int fx=Find(x);
int fy=Find(y);
if(fx!=fy)
{
move_times[fx]++;
ball_count[fy]+=ball_count[fx];
//这个可以去掉,因为不可能在像龙珠个数为零的城市移动
ball_count[fx]=;
fa[fx]=fy;
}
}
void IO()
{
int T,i,m,n,icase=;;
char ope;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&N,&Q);
Set();
printf("Case %d:\n",icase++);
while(Q--)
{
getchar();
scanf("%c",&ope);
switch(ope)
{
case 'T':
scanf("%d%d",&m,&n);
Union(m,n);
break;
case 'Q':
scanf("%d",&m);
int fm=Find(m);
printf("%d% d% d\n",fm,ball_count[fm],move_times[m]);
break;
}
}
}
} int main()
{
// freopen("E:\\in.txt","r",stdin);
IO();
return ;
}

hdu 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. hdu 3635 Dragon Balls(并查集应用)

    Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, ...

  3. HDU 3635 Dragon Balls(超级经典的带权并查集!!!新手入门)

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

  4. hdu 3635 Dragon Balls(并查集)

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

  5. hdu 3635 Dragon Balls

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

  6. HDU 3635:Dragon Balls(并查集)

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

  7. Dragon Balls

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

  8. HDU 5810 Balls and Boxes(盒子与球)

     Balls and Boxes(盒子与球) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/O ...

  9. hdoj 3635 Dragon Balls【并查集求节点转移次数+节点数+某点根节点】

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

随机推荐

  1. python客户端编程

    上一篇说了最为底层的用来网络通讯的套接字.有很多基于套接字的一些协议,这些协议构成了当今互联网大多数客户服务器应用的核心 其实这些协议时在套接字上面的进一层封装用来完成特定的应用,这些应用主要包括: ...

  2. 转载 linux内核 asmlinkage宏

    转载http://blog.chinaunix.net/uid-7390305-id-2057287.html 看一下/usr/include/asm/linkage.h里面的定义:#define a ...

  3. 【java基础】面向过程~面向对象

    相信大家都知道这两个东西,可是大家是如何知道的呢?我们又该如何区分这个东西到底是面向过程还是面向对象的呢? 那,我们首先就要知道什么是面向过程,什么是面向对象: 面向过程"(Procedur ...

  4. PowerDesigner 16.5

    PowerDesigner165_破解文件.rar    链接:http://pan.baidu.com/s/1hqEDUCG    636KB PowerDesigner165_Evaluation ...

  5. 【maven + hibernate(注解) +spring +springMVC】 使用maven搭建项目

    研究,百度,查资料+好友帮助,使用MyEcplise2015工具,通过maven搭建hibernate+springMVC+spring的项目,数据库采用MySql5.5 不过使用的版本会在项目搭建过 ...

  6. c#写windows服务(转)

    序言 前段时间做一个数据迁移项目,刚开始用B/S架构做的项目,但B/S要寄存在IIs中,而IIs又不稳定因素,如果重启IIs就要打开页面才能运行项目.有不便之处,就改用Windows服务实现.这篇就总 ...

  7. 02_Java语言基础部分【总结】

    1. 数据的输入/输出 标准输入输出流 字符输入: char c = (char)System.in.read(); 字符串输入: BufferedReader buf = new BufferedR ...

  8. PHP 传值和传引用、传地址的区别

    传值,   是把实参的值赋值给行参   那么对行参的修改,不会影响实参的值   传地址   是传值的一种特殊方式,只是他传递的是地址,不是普通的如int   那么传地址以后,实参和行参都指向同一个对象 ...

  9. Delphi 包的设计思想及它与PAS、BPL、DCU、DLL、OXC的关系。

    DCP ,BPL分别是什么文件,起什么作用?你在DELPHI中建立一个package然后保存一下,看看. bpl和Dll比较相似.只是BPL是BORLAND自己弄出来的东西!!!调用也和调用DLL相似 ...

  10. 解决ie8和ie7显示不一致

    解决ie8和ie7显示不一致 当使用 Microsoft Internet Explorer 8 Beta 1 版本时,可能会遇到以下问题之一: • 网页布局不整齐 • 文本或图像重叠 • JavaS ...