【并查集】UVALive3027 Corporative Network

【并查集】UVALive3027 Corporative Network

并查集——维护到根节点距离的d数组

题目大意

对n个节点操作,加边 or 询问某节点到根节点的距离


说一下思路

之前做过一道求连通分支最大元素个数的题目,维护的是一个cnt[ ]数组(在加边的过程中);比较这道题,可以考虑维护到根节点的距离d[ ]数组。

思路:记下每个节点到父亲节点的距离为d[i],然后在路径压缩时维护这个d数组;

在加边时只有这两个节点中的父亲节点的d需要维护,并不需要查操作,这一点的区别是相当大的!而怎样得到某节点u到根节点的距离呢?查操作。查操作本身带路径压缩的,所以不可能直接得到最终答案,必须一边路径压缩,一边维护数组d,最后输出d[u]即可

★维护过程与路径压缩同时进行,想想还是有点小麻烦!


参考代码

#include<bits/stdc++.h>
using namespace std;

const int _max = 2e4 + 10;
char cmd[2];
int n,a,b,pre[_max],d[_max];

void init(){
  for(int i = 1; i <= n; ++ i){
    pre[i] = i;//每个元素所在集合为其自身
    d[i] = 0;//每个节点到父亲节点的距离为0
  }
}

int find(int x){//返回元素x的根节点,路径压缩过程中维护d[]数组
  if( x == pre[x]) return x;
  int root = find(pre[x]);
  d[x] += d[pre[x]];
  return  pre[x] = root;
}

void join(int a,int b){//并,按要求合并边(a,b)
  pre[a] = b;
  d[a] = (abs(a - b))%1000;
}

int main(){
 #ifndef ONLINE_JUDGE
 freopen("input.txt","r",stdin);
 #endif // ONLINE_JUDGE
 int T;cin>>T;
 while(T--){
    scanf("%d",&n);
    init();
    while(scanf("%s",cmd) == 1 && cmd[0] != ‘O‘){
        if(cmd[0] == ‘I‘){
            scanf("%d%d",&a,&b);
            join(a,b);
        }
        else{
          scanf("%d",&a);
          find(a);
          printf("%d\n",d[a]);
        }
    }
 }
 return 0;
}
  • 加粗 Ctrl + B
  • 斜体 Ctrl + I
  • 引用 Ctrl + Q
  • 插入链接 Ctrl + L
  • 插入代码 Ctrl + K
  • 插入图片 Ctrl + G
  • 提升标题 Ctrl + H
  • 有序列表 Ctrl + O
  • 无序列表 Ctrl + U
  • 横线 Ctrl + R
  • 撤销 Ctrl + Z
  • 重做 Ctrl + Y

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-05 18:51:24

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

【并查集】POJ2236-Wireless Network

[题目大意] 已知每一台电脑只能与它距离为d的电脑相连通,但是两台电脑间可以以第三台作为媒介连接.现在电脑全被损坏.每次可以进行两个操作中的一个,或是修好一台电脑,或是查询两台电脑是否连通. [思路] 显然是并查集.每次修好一台新电脑,就与之前修好的每一台电脑进行判断,距离在d以内就合并. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 const

【连通图|边双连通分量+Tarjan+并查集】POJ-3694 Network(400+ms)

Network Time Limit: 5000MS Memory Limit: 65536K Description A network administrator manages a large network. The network consists of N computers and M links between pairs of computers. Any pair of computers are connected directly or indirectly by suc

(并查集) Wireless Network --POJ --2236

链接: http://poj.org/problem?id=2236 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#problem/A 代码: #include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h> #include<algorithm> #include<iostream>

并查集&amp;MST

[HDU] 1198 Farm Irrigation 基础最小生成树★ 1598 find the most comfortable road 枚举+最小生成树★★ 1811 Rank of Tetris 并查集+拓扑排序★★ 3926 Hand in Hand 同构图★ 3938 Portal 离线+并查集★★ 2489     Minimal Ratio Tree dfs枚举组合情况+最小生成树★ 4081     Qin Shi Huang's National Road System 最

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

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

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