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>
#include <algorithm>
#include <string.h>
#include <queue>
#include <stack>
#include <map>

using namespace std;

int fa[1000010];
int d[1000010];

int fd(int x)
{
    if (fa[x] != -1)
    {
        int rot = fd(fa[x]);
        d[x] += d[fa[x]];
        return fa[x] = fd(fa[x]);
    }
    else return x;
}

int main()
{
    int a, b;
    int t, n;
    scanf("%d",&t);
    char cmd[10];
    while (t--)
    {
        memset(fa, -1, sizeof(fa));
        memset(d,0,sizeof(d));
        scanf("%d", &n);
        while (scanf("%s", cmd) && cmd[0] != ‘O‘)
        {
            if (cmd[0] == ‘E‘)
            {
                scanf("%d", &a);
                fd(a);
                printf("%d\n", d[a]);
            }
            else
            {
                scanf("%d%d", &a, &b);
                fa[a] = b;
                d[a] = abs(a - b) % 1000;
            }
        }
    }
    return 0;
}
时间: 2024-10-12 09:14:47

UVA 1329 Corporative Network【并查集】的相关文章

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 <s

[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

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

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

UVA 10158 War(并查集)

War A war is being lead between two countries, A and B. As a loyal citizen of C, you decide to help your country's espionage by attending the peace-talks taking place these days (incognito, of course). There are n people at the talks (not including y

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

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

POJ 2236 Wireless Network (并查集)

Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 18066   Accepted: 7618 Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computer

uva 10608 Friends(并查集)

uva 10608 Friends 题目大意:给出两两之间的关系,求出最大的关系网. 解题思路:并查集裸体. #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <cstdlib> using namespace std; const int N = 30005; typedef long long ll; int n, m; in