LA 3027 Corporative Network(并查集)

有n个点,一开始都是孤立的,然后有I,E两种操作 
I        u v,把u的父节点设为v,距离为abs(u-v) % 1000,保证u之前没有父节点 
E      询问u到根节点的距离

#include<iostream>
#include<cmath>
using namespace std;
const int maxn=2e4+5;
int par[maxn],dis[maxn];
void init(int n)
{
    for(int i=0;i<=n;i++)
    {
        par[i]=i;
        dis[i]=0;
    }
}
int find(int x)
{
    if(x==par[x])
        return x;
    int root=find(par[x]);
    dis[x]+=dis[par[x]];
    return par[x]=root;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        init(n);
        char s[5];
        while(cin>>s,s[0]!=‘O‘)
        {
            int p,q;
            if(s[0]==‘E‘)
            {
                cin>>p;
                find(p);
                cout<<dis[p]<<endl;
            }
            if(s[0]==‘I‘)
            {
                cin>>p>>q;
                par[p]=q;
                dis[p]=abs(p-q)%1000;
            }
        }

    }
    return 0;
}
时间: 2024-08-28 08:40:38

LA 3027 Corporative Network(并查集)的相关文章

[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

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

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

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

·一些很可爱的询问和修改,放松地去用并查集解决. ·英文题,述大意: 输入n(5<=n<=20000)表示树有n个节点,并且会EOF结束地读入不超过 20000个操作,一共有两种:    ①I v u:表示将v的父亲节点设置为u(在这之前v没有爸爸),边权设置为abs(v-u)%1000.    ②E u:表示询问u到当前它所在树的根节点的距离. ·分析:      为了记录当前一系列加边操作后所有的点的位置情况(因为你随时可能回答询问啊),根据这道题的点关系特点[只在乎点和其根节点的信息],

并查集(路径更新) LA 3027 Corporative Network

题目传送门 题意:训练指南P192 分析:主要就是一个在路径压缩的过程中,更新点i到根的距离 #include <bits/stdc++.h> using namespace std; const int N = 2e4 + 5; struct DSU { int rt[N], d[N]; void init(void) { memset (rt, -1, sizeof (rt)); memset (d, 0, sizeof (d)); } int Find(int x) { if (rt[x

LA 3027 Corporative Network

这题感觉和 POJ 1988 Cube Stacking 很像,在路径压缩的同时递归出来的时候跟新distant数组 1 //#define LOCAL 2 #include <algorithm> 3 #include <cstdio> 4 using namespace std; 5 6 const int maxn = 20000 + 10; 7 int parent[maxn], distant[maxn]; 8 9 int GetParent(int a) 10 { 11

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

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