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 ...
随机推荐
- _0_web_基础
创:18_3_2017修:20_3_2017 什么是前端? --在浏览器中展示内容以及处理请求 什么是浏览器? --一款能将网页内容展现给用户查看,并且让用户与网页交互的软件 什么是内核? --渲染引 ...
- _3_form_标签
什么是input标签? 处理输入信息 input有哪些类型? 选择: <input type="checkbox"> 多选框,选择爱好等 <input type= ...
- java常用类--字符串
String import java.io.IOException; import java.util.Arrays; public class Linkin { public static void ...
- springMVC中@RequestParam和@RequestBody注解的用法
springMVC中@RequestParam注解用在Controller层获解析.提取参数,当然你也可以用request.getParameter("name")来获取参数,而@ ...
- JAVA常用知识点及面试题总结
1. String.StringBuffer.StringBuilder三者区别? (1)三者在执行速率上的比较: String<StringBuffer<StringBuilder 原因 ...
- 在IAR下移植CC2650 contiki工程
与Instant Contiki相比,在Windows的IAR下开发可以在线仿真,下载也更方便,因此我想把BLEach的工程移植到IAR下.弄了好几天总算编译并且下载成功了,参考了[这篇TI的wiki ...
- 图说:为什么Java中的字符串被定义为不可变的
8张图,看懂Java字符串的不变性 字符串,想必大家最熟悉不过了,通常我们在代码中有几种方式可以创建字符串,比如:String s = "Hollis";这时,其实会在堆内存中创建 ...
- Linux history命令
history命令主要用于显示历史命令, 重新执行历史命令. Linux系统当你在shell(控制台)中输入并执行命令时,shell会自动把你的命令记录到历史列表中,一般保存在用户目录下的.bash_ ...
- BZOJ 2916: [Poi1997]Monochromatic Triangles [计数]
题意:空间中有n个点,任意3个点不共线.每两个点用红线或者蓝线连接,如果一个三角形的三边颜色相同,那么称为同色三角形.给你一组数据,计算同色三角形的总数. 考虑补集,异色三角形 每个点的边红色和蓝色两 ...
- 2018/1/21 Netty通过解码处理器和编码处理器来发送接收POJO,Zookeeper深入学习
package com.demo.netty; import org.junit.Before;import org.junit.Test; import io.netty.bootstrap.Boo ...