题目链接:uva 1329 - Corporative Network
题目大意:有n个节点,初始时每个节点的父节点都不存在,每次执行一个I操作和E操作:
- I操作:吧节点u的父节点设为v,距离为|u-v| % 1000,输入保证u没有父节点
- E操作:询问u到根节点的距离。
解题思路:裸的加权并查集。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int maxn = 20005;
const int MOD = 1000;
int N, f[maxn], d[maxn];
int getfar (int x) {
if (x == f[x])
return x;
int root = getfar(f[x]);
d[x] += d[f[x]];
return f[x] = root;
}
int main () {
int cas;
scanf("%d", &cas);
while (cas--) {
scanf("%d", &N);
for (int i = 0; i <= N; i++) {
f[i] = i;
d[i] = 0;
}
int u, v;
char str[10];
while (scanf("%s", str), str[0] != 'O') {
if (str[0] == 'E') {
scanf("%d", &u);
getfar(u);
printf("%d\n", d[u]);
} else if (str[0] == 'I') {
scanf("%d%d", &u, &v);
f[u] = v;
d[u] = abs(u - v) % MOD;
}
}
}
return 0;
}
分享到:
相关推荐
企业出版系统。 企业级内容管理系统,需要通过网络发布大量内容。 该系统被建模为使用模块化权限架构支持大型开发环境。
马西拉商店 该项目有2个层:Store(django)和Corporative Website(node),每个文件夹都有自己的自述文件。
学习障碍儿童和普通教育儿童在麦卡锡儿童能力量表上的比较 P.vyrhology in the Srhools 1980, 17. 429-436 比较学习障碍儿童和普通教育儿童的麦卡锡儿童...对目前的研究结果进行了详细讨论,并确定了学习障碍儿童在 MS
Corporative Lorem ipsum dolor sit amet, consectetur adipiscing elit. Delivery 中国领先的移动应用分发平台,致力于为用户在移动互联网时代提供丰富、安全、个性化的手机应用软件、游戏和内容。同时,依托...
Comparison of learning disabled and general education ...Porter County Special Education Corporative This study examined the diagnostic value of the MSCA in discriminating between learning disabled an