UVALive - 3027 Corporative Network (并查集)
这题比较简单,注意路径压缩即可。
AC代码
//#define LOCAL
#include <stdio.h>
#include <algorithm>
using namespace std;
const int maxn = 20000+5;
int par[maxn], dis[maxn];
void init(int n) {
for(int i = 0; i <= n; i++) {
par[i] = i;
dis[i] = 0;
}
}
int findRoot(int x) {
if(x == par[x]) {
return x;
} else {
int root = findRoot(par[x]);
dis[x] += dis[par[x]];
return par[x] = root;
}
}
int main() {
#ifdef LOCAL
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif // LOCAL
int T, n;
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
init(n);
char cmd[10];
int x, y;
while(scanf("%s", cmd) == 1 && cmd[0] != 'O') {
char tag = cmd[0];
if(tag == 'E') {
scanf("%d", &x);
findRoot(x);
printf("%d\n", dis[x]);
} else {
scanf("%d%d", &x, &y);
par[x] = y;
dis[x] = abs(x-y) % 1000;
}
}
}
return 0;
}
如有不当之处欢迎指出!
UVALive - 3027 Corporative Network (并查集)的更多相关文章
- [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 ...
- 【暑假】[实用数据结构]UVAlive 3027 Corporative Network
UVAlive 3027 Corporative Network 题目: Corporative Network Time Limit: 3000MS Memory Limit: 30000K ...
- UVALive 3027 Corporative Network 带权并查集
Corporative Network A very big corporation is developing its corporative networ ...
- 并查集 + 路径压缩(经典) UVALive 3027 Corporative Network
Corporative Network Problem's Link Mean: 有n个结点,一开始所有结点都是相互独立的,有两种操作: I u v:把v设为u的父节点,edge(u,v)的距离为ab ...
- UVALive 3027 Corporative Network (带权并查集)
题意: 有 n 个节点,初始时每个节点的父节点都不存在,你的任务是执行一次 I 操作 和 E 操作,含义如下: I u v : 把节点 u 的父节点设为 v ,距离为| u - v | ...
- UVALive 3027 Corporative Network
---恢复内容开始--- Corporative Network Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld ...
- 3027 - Corporative Network
3027 - Corporative Network 思路:并查集: cost记录当前点到根节点的距离,每次合并时路径压缩将cost更新. 1 #include<stdio.h> 2 #i ...
- 3027 - Corporative Network(并差集)
3027 - Corporative Network A very big corporation is developing its corporative network. In the begi ...
随机推荐
- hibernate_xml映射exception
错误原因:在通过hibernate指示生成两个表之间的外键关系之后,一个表中引用的外键不在另一个表的参考范围里面. 解决:使之满足参考完整性 org.hibernate.TransientObject ...
- ORACLE对象大小写问题
在数据库新建一个测试表(数据库版本为ORACLE 10.2.0.1.0),表名为小写的test. 脚本如下所示: CREATE TABLE test( id NUMBER(10), ...
- PDO错误调试
在服务器上用PDO操作数据库,怎么都获取不到数据,query语句返回null,但是同样的代码在本地运行无误.SO,开始找bug. <?php $host='localhost'; $dbname ...
- java SimpleDateFormat日期与时间戳的相互转换
自我总结,有什么不到位的地方,各位可以帮忙纠正补充一下,感激不尽! 目的:SimpleDateFormat类可以很随意的组合日期时间的格式,不止单纯的yyyy-MM-dd这种格式 废话不多说,上代码 ...
- pwd 的“P”选项
1.目录是链接目录时,pwd -P 显示出实际路径,而非使用连接(link)路径:pwd显示的是连接路径 例: [root@localhost soft]# cd /etc/init.d [root ...
- Electron 打包Mac安装包代码签名问题解决方案Could not get code signature for running application
最近一直在做electron应用的打包,集成mac版本的自动更新时出现了问题. Error: Could not get code signature for running application ...
- Java中的代码点和代码单元(转)
文章来源:http://blog.csdn.net/weizhaozhe/article/details/3909079 这篇文章讲的很细,但是对于初学者也很难理解,在后面的笔记中,我会陈述自己的简单 ...
- 自己做的notepad++ FTP同步插件
平时修改FTP上的文件的时候,想先备份一下,没有合适的方法,尝试做了一个小插件. 差点忘了,下载地址 https://pan.baidu.com/s/1htdtT9E 下面是 具体的说明 Fa ...
- JAVA中默认的编码方式
转:http://blog.csdn.net/scyatcs/article/details/31356823 编码问题存在两个方面:JVM之内和JVM之外.1.Java文件编译后形成class这里J ...
- hexo搭建博客上传至github
准备工作 ssh key 要先申请好,并且添加至github的ssh库中 安装插件 hexo-deployer-git npm install hexo-deployer-git --save 配置 ...