UVALive - 3027:Corporative Network

加权并查集

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#define MAXN 20000+10
#define pii pair<int,int>
using namespace std;
int fa[MAXN];
int d[MAXN];
int n;
int Abs(int x){
    return (x>0?x:-x);
}
pii find(int x){
    if(fa[x]!=x){
        pii t=find(fa[x]);
        fa[x]=t.first;
        d[x]+=t.second;
    }
    return make_pair(fa[x],d[x]);
}
void solve(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        fa[i]=i,d[i]=0;
    }
    char ch[9]={0};
    while(scanf("%s",ch)&&ch[0]!=‘O‘){
        if(ch[0]==‘E‘){
            int x;
            scanf("%d",&x);
            printf("%d\n",find(x).second);
        }
        else{
            int x,y;
            scanf("%d%d",&x,&y);
            fa[x]=y;
            d[x]=Abs(x-y)%1000;
        }
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        solve();
    }
    return 0;
}
时间: 2024-10-14 23:59:04

UVALive - 3027:Corporative Network的相关文章

【暑假】[实用数据结构]UVAlive 3027 Corporative Network

UVAlive 3027 Corporative Network 题目:   Corporative Network Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 3450   Accepted: 1259 Description A very big corporation is developing its corporative network. In the beginning each of the N ent

UVALive - 3027 - Corporative Network (并查集!!)

UVALive - 3027 Corporative Network Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A very big corporation is developing its corporative network. In the beginning each of the N enterprises of the cor

[2016-03-19][UVALive][3027][Corporative Network]

时间:2016-03-19 13:24:23 星期六 题目编号:[2016-03-19][UVALive][3027][Corporative Network] 题目大意:给定n个节点,I u v表示把u节点的父节点设置为v,距离为|u-v|%1000,E u表示询问u到根节点的距离,给定若干个I E操作,输出相应答案 分析:带权并查集 方法:d[maxn] 维护到父节点的距离,每次询问的时候,把当前节点压缩到根节点输出对应距离即可 #ifdef _WORK_ #include <algorit

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,

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

[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

(DS 《算法竞赛入门经典》)LA 3027 Corporative Network(查询某一个节点到根节点之间的距离)

题目大意: 查询某一个节点到根节点之间的距离 解题思路: 加权并查集问题.之前做的题目是"查看两个或多个节点是否在同一个集合下",现在的题目是"查询某个节点到 根节点之间的距离".之前只需要使用到father[x]这个数组,用来表示x的父亲节点是谁.现在引入dist[x]数组,用来记录 x节点到根节点的距离 1)在并查集中,根节点不懂,其他节点都可以动. A very big corporation is developing its corporative net

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