UVa 1329 - Corporative Network Union Find题解

UVa的题目好多,本题是数据结构的运用,就是Union Find并查集的运用。主要使用路径压缩。甚至不需要合并树了,因为没有重复的连线和修改单亲节点的操作。

郁闷的就是不太熟悉这个Oj系统,居然使用库中的abs就会WA,自己写了个abs小函数就过了。

题目:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4075

#include <stdio.h>

const int MAX_N = 20005;
const int MOD = 1000;
//在UVa使用库的abs居然WA,浪费好多时间
inline int abs(int a) { return a < 0? -a : a; }

struct Subset
{
	int p, w;
};

Subset subs[MAX_N];

int findParent(int x)
{
	if (subs[x].p != x)
	{
		int p = subs[x].p;
		subs[x].p = findParent(subs[x].p);
		subs[x].w = (subs[x].w + subs[p].w);
	}
	return subs[x].p;
}

void initSubs(int N)
{
	for (int i = 1; i <= N; i++)
	{
		subs[i].p = i;
		subs[i].w = 0;
	}
}

int main()
{
	int T, N, i, j;
	char cmd;
	scanf("%d", &T);
	while (T--)
	{
		scanf("%d", &N);
		getchar();
		initSubs(N);
		while ((cmd = getchar()) && cmd != 'O')
		{
			if (cmd == 'E')
			{
				scanf("%d", &i);
				subs[i].p = findParent(i);
				printf("%d\n", subs[i].w);
			}
			else
			{
				scanf("%d %d", &i, &j);
				subs[i].w = (abs(j - i))%MOD;
				subs[i].p = j;//不存在重复连线和更改parent,故此直接连就ok
			}
			getchar();
		}
	}
	return 0;
}

UVa 1329 - Corporative Network Union Find题解,布布扣,bubuko.com

时间: 2024-10-10 03:03:44

UVa 1329 - Corporative Network Union Find题解的相关文章

UVA 1329 Corporative Network【并查集】

题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4075 题意: 有n个结点,開始都是单独的结点,如今有I操作和E操作,I u v表示吧u的父亲结点设为,距离为|u - v| % 1000,E操作询问u到根的距离 代码: #include <stdio.h> #include <iostream> #in

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的根节点的距离,先看一个没有路径压缩直接暴力的方法把.(本以为不会过的,

[LA] 3027 - Corporative Network [并查集]

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

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,

Corporative Network

Description A very big corporation is developing its corporative network. In the beginning each of the N enterprises of the corporation, numerated from 1 to N, organized its own computing and telecommunication center. Soon, for amelioration of the se

LA 3027 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, for amelioration of the services, the co

3027 - Corporative Network

3027 - Corporative Network 思路:并查集: cost记录当前点到根节点的距离,每次合并时路径压缩将cost更新. 1 #include<stdio.h> 2 #include<algorithm> 3 #include<iostream> 4 #include<string.h> 5 #include<math.h> 6 #include<stack> 7 #include<set> 8 #inc

POJ1962:Corporative Network(并查集)

Description A very big corporation is developing its corporative network. In the beginning each of the N enterprises of the corporation, numerated from 1 to N, organized its own computing and telecommunication center. Soon, for amelioration of the se

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++) {