hdu Dragon Balls
这题是一道简单的并查集的运用。龙珠所在的城市、该城市龙珠数目都是很简单的问题,稍微麻烦一点的就是龙珠被移动的次数,因为每一次要移动的是一个城市中所有的龙珠,所以每次移动该城市中所有龙珠的移动次数都要加一。
一开始用二维数组存放每个城市中龙珠的编号,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的更多相关文章
- hdu 3635 Dragon Balls (带权并查集)
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- hdu 3635 Dragon Balls(并查集应用)
Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, ...
- HDU 3635 Dragon Balls(超级经典的带权并查集!!!新手入门)
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- hdu 3635 Dragon Balls(并查集)
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- hdu 3635 Dragon Balls
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- HDU 3635:Dragon Balls(并查集)
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- Dragon Balls
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- HDU 5810 Balls and Boxes(盒子与球)
Balls and Boxes(盒子与球) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/O ...
- hdoj 3635 Dragon Balls【并查集求节点转移次数+节点数+某点根节点】
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
随机推荐
- 汇编学习(二)——8086CPU
一.8086CPU 1.微处理器 (1)微控制机:也称单片机 (2)DSP芯片:数字信号处理芯片 (3)嵌入式微处理器 (4)通用微处理器:PC站.工作站.服务器使用的处理器 2.内部结构: (1)总 ...
- AIDL
在介绍跨程序进程间通信AIDL前,先看一下本程序activity与某个服务是怎么绑定在一起进行交互的. 需求:服务有两个方法.分别是播放音乐与停止播放音乐.该程序的活动要访问这两个方法,在activi ...
- Linux学习笔记(14)文件系统管理
1 回顾分区与系统 (1)分区类型 Linux的分区一般包括主分区,扩展分区和逻辑分区.其中: 主分区最多只能分4个: 扩展分区只能有一个,也算作主分区的一种,即主分区加扩展分区最多有4个.但扩展分区 ...
- vs 中怎么用c改变部分字体颜色
// test.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <windows.h> #include< ...
- 改了哪里vs调试直接用iis运行
OAS2Web.csproj 中的useiis为true,但必须要先配置好iis网站指向文件目录
- CSS3_loading效果
写个div给他个基本样式: <body> <div class="load-container load" id="loader" > ...
- 用div,ul,input模拟select下拉框
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- Codeforces Round #375 (Div. 2) - B
题目链接:http://codeforces.com/contest/723/problem/B 题意:给定一个字符串.只包含_,大小写字母,左右括号(保证不会出现括号里面套括号的情况),_分隔开单词 ...
- 《DSP using MATLAB》示例Example4.7
ROC分三种情况:
- windows安装python问题
在安装python过程中,直接下一步,安装好了,配置环境变量的时候如果找不到安装路径在哪,打开开始菜单,在左边的程序栏找到: 这样就找到了,然后在系统变量中加入即可.如果找不到的话,以联想电脑为例一般 ...