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 ...
随机推荐
- Find the Celebrity 解答
Question Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there ma ...
- Payssion,海外本地支付_海外本地收款_小语种本地支付_外贸收款_外贸网店收款_欧洲本地支付_俄罗斯本地支付_巴西支付_跨境支付_PAYSSION,让跨境支付更轻松!
Payssion,海外本地支付_海外本地收款_小语种本地支付_外贸收款_外贸网店收款_欧洲本地支付_俄罗斯本地支付_巴西支付_跨境支付_PAYSSION,让跨境支付更轻松! 首页 / 关于 ...
- [转载]C宏定义的小结
FROM:http://blog.csdn.net/sunboy_2050/article/details/6103530 实现代码实例 程序代码: #include <stdio.h> ...
- POI导入数据
package util; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.I ...
- 虚拟机设备直通的两种方式(Working in Progress)
声明: 本博客欢迎转发.但请保留原作者信息! 博客地址:http://blog.csdn.net/halcyonbaby 内容系本人学习.研究和总结,如有雷同,实属荣幸! pci passthroug ...
- J2EE之普通类载入web资源文件的方法
在WEB中普通类并不能像Servlet那样通过this.getServletContext().getResourceAsStream()获取web资源,须要通过类载入器载入,这里有两种方式,这两种方 ...
- Analyzing the Analyzers 分析分析师 —— 数据科学部门如何建
很多牛逼的公司都宣称在建立数据科学部门,这个部门该如何组建,大家都在摸石头过河. O‘reilly Strata今年 六月份发布了报告 <Analyzing the Analyzers>, ...
- maven报错cannot change version of project facet
用Eclipse创建Maven结构的web项目的时候选择了默认的catalog,由于这个catalog比较老,用的servlet还是2.3,而现在最少也是2.5,所以经常会出现问题,在Projecdt ...
- scrapy使用crontab定时任务不能自动执行的调试
在用crontab进行定时任务时,发现任务并没有执行.而手动bash yourshell.sh时可以正常的执行程序.以下是个人的解决流程. 一.将错误打印打out.log */10 * * * * b ...
- JS判断RadioButtonList是否有选中项
提交表单之前对RadioButtonList控件的选中项进行判断: 方法一: <script type="text/javascript"> function chec ...