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 ...
随机推荐
- node.js 开发桌面程序, 10个令人惊讶的NodeJS开源项目
用 node-webkit 开源框架. 做企业站,杠杠地 包括电子书和支付宝系统都是node开发的,. 接收传感器发送的数据再运算...对水泵.风机.空调这些硬件进行远程控制. 细数10个令人惊讶的N ...
- 1025 PAT Ranking[排序][一般]
1025 PAT Ranking (25)(25 分) Programming Ability Test (PAT) is organized by the College of Computer S ...
- 来自阿里妈妈的iconfont(转)
转自http://www.augsky.com/775.html 随便说说两者的优缺点 其实主要是说iconfont的优点和Font Awesome的缺点.-_-|||iconfont的图标库相当巨大 ...
- MySQL从删库到跑路_高级(五)——触发器
作者:天山老妖S 链接:http://blog.51cto.com/9291927 一.触发器简介 1.触发器简介 触发器是和表关联的特殊的存储过程,可以再插入,删除或修改表中的数据时触发执行,比数据 ...
- linux基础命令---touch
touch 将文件的访问时间和修改时间修改为当前时间.如果指定的文件不存在,那么将会创造空文件,除非指定-c或-h选项.文件参数字符串‘-‘被专门处理,并导致touch更改与标准输出相关联的文件的时间 ...
- iPhone手机获取uuid 安装测试app
iPhone手机获取uuid 安装测试app UDID是一种iOS设备的特殊识别码.除序号之外,每台ios装置都另有一组独一无二的号码,我们就称之为识别码( Unique Device Identif ...
- js如何模拟multipart/form-data类型的请求
var temp = document.createElement('form'); temp.action = this.data.testURL; temp.method = 'post'; te ...
- corejDay1
1.内部类: 有什么用? 1.可以访问该类定义所在作用域中的数据,包括私有数据. 2.当想定义一个回调函数而不想编写大量代码时,使用匿名内部类比较便捷. 3.内部类可以对同一个包中的其他类隐藏起来. ...
- (四)github之Git的初始设置
设置姓名与邮箱地址 这里的姓名和邮箱地址会用在git的提交日志之中,在github上公开git仓库时会随着提交日志一起公开. 有两种方式, 第一种,在git bash下设置 第二种, 通过直接编辑.g ...
- Python入门之Python中的logging模块
基本用法 下面的代码展示了logging最基本的用法. import logging import sys # 获取logger实例,如果参数为空则返回root logger logger = log ...