UESTC_秋实大哥打游戏 2015 UESTC Training for Data Structures<Problem H>
H - 秋实大哥打游戏
Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)
”也许人生就是游戏,你却执意耕耘着春秋。” —— 秋实大哥叹道。
秋实大哥是一个喜欢玩游戏的人,相较于其他种类的游戏,秋实大哥更喜欢自由开放的沙盒游戏,尤其是minecraft。
现在,秋实大哥发现了N个独立的小岛(编号1,2,3.....N),于是他要把这些小岛连起来。
每一次,秋实大哥会选择两个不同的小岛x(x是所在集合的中心)和y(y不一定是集合的中心),如果小岛x和小岛y不在一个集合里,就建立一条距离为|x−y| mod 1000的边,
把这两片小岛合并为一个新的集合,中心为y原来所在的集合中心。
但,秋实大哥想实时知道某一个小岛距当前所在集合中心的距离。由于秋实大哥忙着过节,所以他想请你帮忙。
Input
第一行有一个整数N表示小岛的个数。
接下来有若干行,每一行为以下两种操作中的一种:
I x y : 表示秋实大哥想要在x和y之间建立一条边。
E x : 询问x到当前集合中心的距离。
输入以一个大写字母O结束。
1≤N≤100000,操作数≤200000。
Output
对于每一次询问,输出一个整数,即x到当前集合中心的距离,占一行。
Sample input and output
| Sample Input | Sample Output |
|---|---|
3 |
1 |
解题报告
容易联想到并查集,使用dis[]数组来维护每个点到根的距离,唯一需要注意的是先修改距离,再更新其父亲,否则会出错.
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
typedef long long ll;
using namespace std;
const ll maxn = 6e5 + ;
/*
Dis is the distance between node and his root
*/
ll pre[maxn];
ll dis[maxn]; ll getfather(ll cur)
{
if (cur == pre[cur])
return cur;
ll fat = getfather(pre[cur]);
dis[cur] += dis[pre[cur]];
pre[cur] = fat;
return fat;
} char temp[]; int main(int argc,char *argv[])
{
ll n;
scanf("%lld",&n);
memset(dis,,sizeof(dis));
for(int i = ; i <= n ; ++ i)
pre[i] = (ll)i;
while()
{
scanf("%s",temp);
if (temp[] == 'O')
break;
if (temp[] == 'I')
{
ll x,y;
scanf("%lld%lld",&x,&y);
if (getfather(y) != x)
{
dis[x] = abs(x-y) % ;
pre[x] = y;
}
}
else
{
ll x;
scanf("%lld",&x);
getfather(x); // caculate ans
printf("%lld\n",dis[x]);
}
}
return ;
}
UESTC_秋实大哥打游戏 2015 UESTC Training for Data Structures<Problem H>的更多相关文章
- UESTC_秋实大哥与战争 2015 UESTC Training for Data Structures<Problem D>
D - 秋实大哥与战争 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Subm ...
- UESTC_秋实大哥搞算数 2015 UESTC Training for Data Structures<Problem N>
N - 秋实大哥搞算数 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Subm ...
- UESTC_秋实大哥去打工 2015 UESTC Training for Data Structures<Problem G>
G - 秋实大哥去打工 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Subm ...
- UESTC_秋实大哥与家 2015 UESTC Training for Data Structures<Problem E>
E - 秋实大哥与家 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submi ...
- UESTC_秋实大哥与快餐店 2015 UESTC Training for Data Structures<Problem C>
C - 秋实大哥与快餐店 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Sub ...
- UESTC_秋实大哥与花 2015 UESTC Training for Data Structures<Problem B>
B - 秋实大哥与花 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submi ...
- UESTC_秋实大哥与小朋友 2015 UESTC Training for Data Structures<Problem A>
A - 秋实大哥与小朋友 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Sub ...
- UESTC_秋实大哥掰手指 2015 UESTC Training for Dynamic Programming<Problem B>
B - 秋实大哥掰手指 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 2048/1024KB (Java/Others) Submit ...
- UESTC_秋实大哥与线段树 2015 UESTC Training for Data Structures<Problem M>
M - 秋实大哥与线段树 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Sub ...
随机推荐
- 【转】使用miniupnpd-->upnp协议 映射本地端口到外网
miniupnpc的主要函数介绍 1>.miniupnpc库主要使用的头文件有 #include"miniwget.h" #include"miniupnpc.h& ...
- 学艺不精,又被shell的管道给坑了
我用过bash shell,而且时间不短了.但我从来没学过shell,至少没有像C++这么认真去学.平时写些基本的脚本没问题,不懂也可以google.百度.可在2014最后一天,掉坑里了. 其实脚本也 ...
- python高级编程之(类级):子类内建类型
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #类级 #在2.2中,提出了类型(type0与类(class)统一( ...
- web前端之 CSS
CSS概述 CSS 指层叠样式表 (Cascading Style Sheets),说白了就是给html代码穿上好看的衣服,让页面变得好看 CSS存在形式 1.在标签的属性中设置,优先级较高 代码如下 ...
- Ruby新手教程和技巧
Ruby真的比Java更好? Ruby On Rails 创始人:对Java 说再见 这两周以来环绕Java发生的两件大事:EclipseCon 和TheServerSide Java Sympo ...
- SVG Loading
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" width="64&qu ...
- webservice和.net remoting浅谈
服务器端向客户端发送一个进程编号,一个程序域编号,以确定对象的位置. webservice和.net remoting都是用来通信的框架,它们最大的优点是可以像调用本地对象一样调用远程对象,比如: ...
- PHP学习笔记二十二【静态方法二】
<?PHP class Student{ public static $fee; public $name; //构造函数 function __construct($name) { $this ...
- 1203.4——循环语句 之 for
for循环的一般形式为:for(表达式1; 表达式2; 表达式3){ 语句块} 它的执行过程如下:1) 先求解表达式1. 2) 求解表达式2,若其值为真(非0),则执行循环体,否则结束循环. 3 ...
- 关于iOS9之后的loadViewIfNeeded
iOS9之前 有些时候因为需要手动调用loadview 但是有风险,系统不再调用viewDidLoad 所以手动调用loadview是错误的 iOS9之后出现了loadViewIfNeeded解决了这 ...