UVALive - 3027Corporative Network(带权并查集)

题目: UVALive - 3027Corporative Network(带权并查集)

题目大意:有n和节点,初始时每个节点的父节点都不存在,然后有下面两种操作:I 操作 I a,b 将a的父节点变成b。E操作 E a,查询a到它的父节点的距离。

解题思路:带权并查集。注意这里距离的变化是a -> b,那么a到根节点的距离就是a到b的距离的绝对值 % 1000 + b到它的根节点的距离。

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn = 2e5 + 5;
int p[maxn], c[maxn];
int n;

void init () {

	for (int i = 1; i <= n; i++) {
		p[i] = i;
		c[i] = 0;
	}
}

int getParent (int a) {

	if (a == p[a])
		return a;
	int t = p[a];
	p[a] = getParent (p[a]);
	c[a] += c[t];
	return p[a];
}

int main () {

	int T;
	int a, b;
	char str[10];
	scanf ("%d", &T);
	while (T--) {

		scanf ("%d", &n);
		init();
		while (scanf ("%s", str) != EOF) {

			if (str[0] == 'O')
				break;
			if (str[0] == 'E') {

				scanf ("%d", &a);
				int q1 = getParent (a);
				printf ("%d\n", c[a]);
			} else {

				scanf ("%d%d", &a, &b);
				p[a] = b;
				c[a] = abs (a - b) % 1000;
			}
		}
	}
	return 0;
}
时间: 2024-08-24 16:27:48

UVALive - 3027Corporative Network(带权并查集)的相关文章

UVALive 3027 Corporative Network 带权并查集

                     Corporative Network A very big corporation is developing its corporative network. In the beginning each of the N enterprisesof the corporation, numerated from 1 to N, organized its own computing and telecommunication center.Soon,

UVA 3027 Corporative Network 带权并查集、

题意:一个企业要去收购一些公司把,使的每个企业之间互联,刚开始每个公司互相独立 给出n个公司,两种操作 E I:询问I到I它连接点最后一个公司的距离 I I J:将I公司指向J公司,也就是J公司是I公司的上级,距离为abs(I-J)%1000(貌似G++不支持abs,PE了两发) 思路:转化一下题意就行了,首先刚开始的时候每个公司都是独立的,I操作就是并查集中合并操作,将I这课树并到J这个树上, E操作要求的东西就是 I到I的根节点的距离,先看一个没有路径压缩直接暴力的方法把.(本以为不会过的,

POJ1962Corporative Network[带权并查集]

Corporative Network Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 3945   Accepted: 1416 Description A very big corporation is developing its corporative network. In the beginning each of the N enterprises of the corporation, numerated

Corporative Network(带权并查集)

这个题的题意是  当输入'E'是查找操作,查找从后面这个数到他的父亲这边的值,'I'代表把后面的数作为前面数的父亲 然后他们两个的差值代表这两个边的权值 水水的题 #include <stdio.h> #include <string.h> int par[20005]; int rank1[20005]; int abs(int hh) { return (hh>0)?hh:-hh; } void init() { for(int i=0;i<20005;i++) {

poj 1962 Corporative Network(带权并查集)

题意: 在n个站点间建电线:两种操作: I a b表示以a为中心站点建线: E a表示查询以a站点为中心,相连的电线总长度: 思路: 带权并查集:中心站点就是父亲,电线长度为权值: #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; int fa[500010],w[500010]; int t,n,m; char ch[5];

HDU_3172_带权并查集

Virtual Friends Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8229    Accepted Submission(s): 2363 Problem Description These days, you can do all sorts of things online. For example, you can u

并查集——poj2236(带权并查集)

题目:Wireless Network 题意:给定n台已损坏计算机的位置和计算机最远通信距离d,然后分别根据命令执行以下两种操作: "O p" (1 <= p <= N) :表示修理计算机p: "S p q" (1 <= p, q <= N) :表示检测计算机p和计算机q能否通信. 输出:能通信则输出"SUCCESS",否则输出"FAIL" 题解: 带权并查集还是那个重要的知识点--关系. 此题,我们使

hdu3038(带权并查集)

题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=3038 题意: n表示有一个长度为n的数组, 接下来有m行形如x, y, d的输入, 表示从第x,个元素到第y个元素的和为d(包括x, 和y), 问m行输入里面有几个是错误的(第一个输入是正确的); 思路: 很显然带权并查集咯,我们可以用距离的概念代替和的概念比较好理解一点,d表示x到y的和即x到y的距离; 可以用rank[x]表示x到其父亲节点的距离,  将正确的距离关系合并到并查集中

【POJ1182】 食物链 (带权并查集)

Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种. 有人用两种说法对这N个动物所构成的食物链关系进行描述: 第一种说法是"1 X Y",表示X和Y是同类. 第二种说法是"2 X Y",表示X吃Y. 此人对N个动物,用上述两种说法,一句接一句地说出K句话,这K句话有的是真的,有的是假的.当一句话满足下列三条之