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

一开始用二维数组存放每个城市中龙珠的编号,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. 类模板Queue的实现

    #include <iostream> #include <vector> using namespace std; template <class Type> c ...

  2. Linux内核创建一个新进程的过程

    “平安的祝福 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ” 进程在 ...

  3. js循环添加事件

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 【java IO】使用Java输入输出流 读取txt文件内数据,进行拼接后写入到另一个文件中

    package com.sxd.test.util; import java.io.BufferedReader; import java.io.BufferedWriter; import java ...

  5. Angular JS 学习之服务(Service)

    1.AngularJS中,可以创建自己的服务,或使用内建服务: 2.在AngularJS中,服务是一个函数或对象,可在你的AngularJS应用中使用: AngularJS内建了30多个服务:有个$l ...

  6. express随记01

    系统变量的设置 app.get(env) | process.env.NODE_ENV: 会自动判断当前环境类型; app.get(port) | process.env.PORT: 必须手动设置; ...

  7. Redis Tools

    1. Resources Redis Desktop Manager http://redisdesktop.com/ Redis命令的中文文档 http://redisdoc.com/ Redis安 ...

  8. unity 解析xml

    using UnityEngine; using System.Collections; using System.IO; using System.Xml; public class xml : M ...

  9. [R]R的工作流

    最近处理数据时,一直在纠结程序的结构该如何构建,以减少很多简单又很耗时的工作. 刚好把Rob J Hyndman的blog给浏览了一遍,发现一篇2009年的文章,很有启发. 原文: Workflow ...

  10. Android 摇一摇 之 传感器片

    要监视原始的传感器数据,你需要实现两个通过SensorEventListener接口暴露的回调方法:onAccuracyChanged()和onSensorChanged(). 传感器数据的速度值,这 ...