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 ...
随机推荐
- slide.js使用文档
<!doctype html> <head> <script src="js/jquery-latest.min.js"></script ...
- MPAndroidChart饼图属性及相关设置
公司最近在做统计功能,所以用到了饼图,在网上查了一些资料最终决定使用MPAndroidChart,使用起来非常方便,还有一些问题通过各种查找,终于解决...废话不多说,先看下效果图: 布局文件: &l ...
- loadrunner资源过滤器
通过该功能可以实现排除某个资源,很实用 Download Filters功能 帮助在回放脚本的时候对某些特定的访问进行屏蔽,解决页面读取中跨服务器带来数据影响的问题. 过滤规则中有3中策略,即URL. ...
- mybatis中foreach的用法(转)
foreach一共有三种类型,分别为List,[](array),Map三种. foreach属性 属性 描述 item 循环体中的具体对象.支持属性的点路径访问,如item.age,item.inf ...
- 宫格布局实例(注意jquery的版本号要统一)
<!DOCTYPE html><html><head><meta charset="utf-8" /><style> * ...
- PyCharm默认快捷键
转自:http://www.2cto.com/os/201410/341542.html 1.编辑(Editing)Ctrl + Space 基本的代码完成(类.方法.属性)Ctrl + Alt + ...
- jquery笔记(效果)
显示/隐藏: $(selector).hide(speed, function()):隐藏 $(selector).show(speed, function()):隐藏 $(selector).tog ...
- ios开发学习笔记(这里一定有你想要的东西,全部免费)
1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现clear Color无法使用). 其实在代码里还是可以设置的,那就是删除背景view [ ...
- The 13th Zhejiang Provincial Collegiate Contest(2016年浙江省赛)
前4道水题就不说了,其中我做了C题,1Y,小心仔细写代码并且提交之前得确认无误后提交才能减少出错率. 结果后面2题都由波神做掉,学长带我们飞~ 终榜 官方题解 ZOJ 3946 Highway ...
- Codeforces Round #341 (Div. 2)
在家都变的懒惰了,好久没写题解了,补补CF 模拟 A - Wet Shark and Odd and Even #include <bits/stdc++.h> typedef long ...