LA 3027 合作网络
https://vjudge.net/problem/UVALive-3027
题意:
有n个结点,初始时每个结点的父节点都不存在。你的任务是执行一次I操作和E操作,格式如下:
I u v:把结点u的父节点设为v,距离为|u-v|除以1000的余数。输入保证执行指令前u没有父节点。
E u:询问u到根结点的距离。
思路:
主要思想是并查集,记录一下每个结点到根结点的距离即可。
#include<iostream>
#include<cstring>
using namespace std; const int maxn = + ; int p[maxn], d[maxn];
int n;
char c; int find(int x)
{
if (p[x] != x)
return d[x] + find(p[x]);
else
return d[x];
} int main()
{
//freopen("D:\\txt.txt", "r", stdin);
int T;
scanf("%d", &T);
while (T--)
{
memset(d, , sizeof(d));
scanf("%d", &n);
for (int i = ; i <= n; i++)
p[i] = i;
int u, v;
while (scanf("%c",&c) && c != 'O')
{
if (c == 'E')
{
scanf("%d", &u);
int sum=find(u);
cout << sum << endl;
}
else if (c == 'I')
{
scanf("%d%d", &u, &v);
p[u] = v;
d[u] = abs(v - u) % ;
}
}
}
}
LA 3027 合作网络的更多相关文章
- LA 3027 合作网络 并查集
题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...
- 并查集(路径更新) LA 3027 Corporative Network
题目传送门 题意:训练指南P192 分析:主要就是一个在路径压缩的过程中,更新点i到根的距离 #include <bits/stdc++.h> using namespace std; c ...
- LA 3027 Corporative Network
这题感觉和 POJ 1988 Cube Stacking 很像,在路径压缩的同时递归出来的时候跟新distant数组 我发现我一直WA的原因是,命令结束是以字母o结束的,而不是数字0!! //#def ...
- [LA] 3027 - Corporative Network [并查集]
A very big corporation is developing its corporative network. In the beginning each of the N enterpr ...
- LA 3027 Corporative Network 并查集记录点到根的距离
Corporative Network Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu [S ...
- 【LA 3027 Corporative Network】
·一些很可爱的询问和修改,放松地去用并查集解决. ·英文题,述大意: 输入n(5<=n<=20000)表示树有n个节点,并且会EOF结束地读入不超过 20000个操作,一共有两种: ...
- ACM数据结构相关资料整理【未完成,待补充】
在网上总是查不到很系统的练ACM需要学习的数据结构资料,于是参考看过的东西,自己整理了一份. 能力有限,欢迎大家指正补充. 分类主要参考<算法竞赛入门经典训练指南>(刘汝佳),山东大学数据 ...
- leggere la nostra recensione del primo e del secondo
La terra di mezzo in trail running sembra essere distorto leggermente massima di recente, e gli aggi ...
- Le lié à la légèreté semblait être et donc plus simple
Il est toutefois vraiment à partir www.runmasterfr.com/free-40-flyknit-2015-hommes-c-1_58_59.html de ...
随机推荐
- UIAlertview 添加图片
- (void)willPresentAlertView:(UIAlertView *)alertView { 在这个方法中, 绘制需要的东西 uiview *myView = [uiview all ...
- [py]编码-强力理解版
py编码骨灰级总结 思路: python执行py文件步骤--py2/3定义变量时unicode差异 1,py2 py3执行py文件的步骤 2,py2 定义变量x='mao' 1.x='mao', # ...
- 修改class文件
http://yucaifu1989.iteye.com/blog/1850500 http://blog.csdn.net/hexin373/article/details/6669813 使用ja ...
- pandas使用drop_duplicates去除DataFrame重复项
DataFrame中存在重复的行或者几行中某几列的值重复,这时候需要去掉重复行,示例如下: data.drop_duplicates(subset=['A','B'],keep='first',inp ...
- liferay笑傲江湖-API之参数的工具类(ParamUtil)
public class ParamUtil { 036 037 public static boolean get( 038 HttpServletRequest request, String p ...
- bootstrap3浏览器支持情况
Internet Explorer 8 和 9 是被支持的,但是还是有很多CSS3属性和HTML5元素 -- 例如,圆角矩形和投影 -- 是肯定不被支持的.另外,Internet Explorer 8 ...
- UVA 475
/* 通过这题 学会了 两个词组 immediately to the left 是左邻的意思 immediately to the right 这个是右邻的意思 */ #include <io ...
- Impala shell详解
不多说,直接上干货! 查看帮助文档 impala-shell -h 刷新整个云数据 impala-shell -ruse impala;show tables; 去格式化,查询大数据量时可以提高性能 ...
- 处理内容有&特殊字符thinkphp返回xml无法解析的问题<![CDATA[xxx]]>
处理内容有&特殊字符thinkphp返回xml无法解析的问题<![CDATA[xxx]]> // xml 转义特殊字符 如&'" <![CDATA[&quo ...
- 安全测试工具之Burpsuite
端口即服务,每一个服务对应一个或多个端口.端口扫描即通过一些方法检测到一台主机的一段特定端口是否提供相应的服务.利用这些扫描结果,正常用户可以访问系统所提供的服务,而黑客却可以利用这些服务中的漏洞对系 ...