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 ...
随机推荐
- js_7_dom文本
dom编程核心? 绑定事件 找到标签(innerText找标签里文本内容,innerHTML找标签里所有内容) 特殊的标签:input,select,textarea ,通过.value来获取值 3. ...
- android在一个应用程序员启动另一个程序
一般我们知道了另一个应用的包名和MainActivity的名字之后便可以直接通过如下代码来启动: Intent intent = new Intent(Intent.ACTION_MAIN); int ...
- java类路径classpath和包
*/ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...
- zabbix_sender用法实例
环境centos6.8 zabbix版本3.2.4 需求: 要远程监控一台服务器A,但只能通过远程服务器连接本地服务器B,但B不能主动连A(因为A没有固定公网ip) 使用了zabbix_agent的a ...
- 【转】Shell执行MySql操作
mysql -hhostname -Pport -uusername -ppassword -e 相关mysql的sql语句,不用在mysql的提示符下运行mysql,即可以在shell中操作m ...
- JS学习--DOM
1.概念 文档对象模型DOM,定义访问和处理HTML文档的标准方法.DOM将HTML呈现为带有元素.属性和文本的树结构(节点树). 2.document.getElementById("id ...
- print,printf,println
1.print,打印你要打印的东西. 2.printf,可以定义要打印数据的格式,弄个%d,%f之类的.而print不行. 3.println, 会在打印完内容后换行. println和print的差 ...
- OS模块的常用内置方法
chdir 修改当前工作目录到指定目录 Change the current working directory to the specified path. chmod 修改一个文件的访问权限 Ch ...
- mvn mybatis-generator:generate postgresql
postgresql 配置文件 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE genera ...
- Linux history命令
history命令主要用于显示历史命令, 重新执行历史命令. Linux系统当你在shell(控制台)中输入并执行命令时,shell会自动把你的命令记录到历史列表中,一般保存在用户目录下的.bash_ ...