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 ...
随机推荐
- 【Mysql优化三章】第一章
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/Bv5f4b8Peex/article/details/78130845 如今网上的好多mysql优化 ...
- android动态设置边框颜色
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http: ...
- 简述ASP.NET的页面运行机制
在深入学习ASP.NET之前,首先需要先了解一下ASP.NET的页面运行机制: 浏览以下内容需要对ASP.NET管道有一定的了解,附上很不错的几个链接: 选择HttpHandler还是HttpModu ...
- 怎样解决VMware虚拟机无法连接外网问题
安装上虚拟机之后,再安装上linux之后,有时会出现ping不通物理机的ip和任何外网包括网关的ip的问题.虚拟机的网卡是桥接状态.下面就让我为大家演示下一下,怎么让虚拟机重新ping通物理机. 工具 ...
- 十天精通CSS3(12)
自由缩放属性resize 为了增强用户体验,CSS3增加了很多新的属性,其中resize就是一个重要的属性,它允许用户通过拖动的方式来修改元素的尺寸来改变元素的大小.到目前为止,可以使用overflo ...
- [LeetCode] 458. Poor Pigs_Easy tag: Math
There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...
- 修改class文件
http://yucaifu1989.iteye.com/blog/1850500 http://blog.csdn.net/hexin373/article/details/6669813 使用ja ...
- map() 方法
1. 方法概述 map() 方法返回一个由原数组中的每个元素调用一个指定方法后的返回值组成的新数组. 2. 例子 2.1 在字符串中使用map 在一个 String 上使用 map 方法获取字符串中每 ...
- C# 查看所有的隐藏文件
方法1 通过 位与 static void Main(string[] Args) { //假设扫描C:\Test中—— string[] hiddenfiles = Directory ...
- VS中出现“链接器工具错误,XXX工具模块对于SAFESEH映像是不安全的”的解决方法
1.“调试”菜单->“属性”->“配置属性”->“链接器”->“命令行” 2.在“其它选项”的输入框里输入 /SAFESEH:NO ,点击“应用”