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 enterprises of the corporation, numerated from 1 to N, organized its own computing and telecommunication center. Soon, for amelioration of the services, the corporation started to collect some enterprises in clusters, each of them served by a single computing and telecommunication center as follow. The corporation chose one of the existing centers I (serving the cluster A) and one of the enterprises J in some other cluster B (not necessarily the center) and link them with telecommunication line. The length of the line between the enterprises I and J is |I – J|(mod 1000).In such a way the two old clusters are joined in a new cluster, served by the center of the old cluster B. Unfortunately after each join the sum of the lengths of the lines linking an enterprise to its serving center could be changed and the end users would like to know what is the new length. Write a program to keep trace of the changes in the organization of the network that is able in each moment to answer the questions of the users.

Input

Your program has to be ready to solve more than one test case. The first line of the input will contains only the number T of the test cases. Each test will start with the number N of enterprises (5<=N<=20000). Then some number of lines (no more than 200000) will follow with one of the commands:
E I – asking the length of the path from the enterprise I to its serving center in the moment;

I I J – informing that the serving center I is linked to the enterprise J.

The test case finishes with a line containing the word O. The I commands are less than N.

Output

The
output should contain as many lines as the number of E commands in all
test cases with a single number each – the asked sum of length of lines
connecting the corresponding enterprise with its serving center.

Sample Input

1
4
E 3
I 3 1
E 3
I 1 2
E 3
I 2 4
E 3
O

Sample Output

0
2
3
5 ----------------------------------------------------------------------------------------------------------------------------------------------------------- 思路:
操作只有询问到root距离以及改变父结点,因此可以用并查集实现。为每个结点添加信息d表示到父结点的距离,对于I操作,将d初始化为abs(u-v)%1000,对于每次询问E添加功能:压缩路径的同时改变d,即在p[u]=root的同时有d[u]=dist(u->root) 代码:
 #include<cstdio>
#include<cstring>
#include<algorithm>
#define FOR(a,b,c) for(int a=(b);a<(c);a++)
using namespace std;
const int maxn= +; int p[maxn],d[maxn];
int find_set(int u){
//寻找root结点->路径压缩+维护d
if(u==p[u]) return u;
else
{
int root=find_set(p[u]);
d[u] += d[p[u]]; //更新d为d->root的距离
return p[u]=root; //路径压缩
}
} int main(){
int T,n; scanf("%d",&T);
while (T--){
scanf("%d",&n);
FOR(i,,n+){ p[i]=i; d[i]=; }
char ch[];int x,y;
while(scanf("%s",ch) && ch[]!='O'){
if(ch[]=='E'){ scanf("%d",&x);find_set(x);printf("%d\n",d[x]); }
else { scanf("%d%d",&x,&y); p[x]=y; d[x]=abs(x-y) % ; }
}
}
return ;
}

												

【暑假】[实用数据结构]UVAlive 3027 Corporative Network的更多相关文章

  1. UVALive 3027 Corporative Network

    ---恢复内容开始--- Corporative Network Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld ...

  2. UVALive 3027 Corporative Network 带权并查集

                         Corporative Network A very big corporation is developing its corporative networ ...

  3. 并查集 + 路径压缩(经典) UVALive 3027 Corporative Network

    Corporative Network Problem's Link Mean: 有n个结点,一开始所有结点都是相互独立的,有两种操作: I u v:把v设为u的父节点,edge(u,v)的距离为ab ...

  4. UVALive - 3027 Corporative Network (并查集)

    这题比较简单,注意路径压缩即可. AC代码 //#define LOCAL #include <stdio.h> #include <algorithm> using name ...

  5. UVALive 3027 Corporative Network (带权并查集)

    题意: 有 n 个节点,初始时每个节点的父节点都不存在,你的任务是执行一次 I 操作 和 E 操作,含义如下: I  u  v   :  把节点 u  的父节点设为 v  ,距离为| u - v | ...

  6. 3027 - Corporative Network(并差集)

    3027 - Corporative Network A very big corporation is developing its corporative network. In the begi ...

  7. 3027 - Corporative Network

    3027 - Corporative Network 思路:并查集: cost记录当前点到根节点的距离,每次合并时路径压缩将cost更新. 1 #include<stdio.h> 2 #i ...

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

    A very big corporation is developing its corporative network. In the beginning each of the N enterpr ...

  9. LA 3027 Corporative Network 并查集记录点到根的距离

    Corporative Network Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [S ...

随机推荐

  1. L​i​n​u​x​环​境​变​量​的​设​置​和​查​看​方​法

    L​i​n​u​x​环​境​变​量​的​设​置​和​查​看​方​法 1. 显示环境变量HOME [root@AY1404171530212980a0Z ~]# echo $HOME /root 2. ...

  2. uva 11461

    简单 打个表 case数不超过200 数据比较水  木有超时的风险~~ /*************************************************************** ...

  3. 写给 iOS 开发者的 Hopper + lldb 简介

    最近,关于 @Steipete 在Radar发布的帖子,笔者看到很多人在问「你是怎么理解那个伪代码的」.笔者想写博客已经有一段时间了,现在正好就此发表第一篇博文.笔者在一个叫 Hopper 的工具上花 ...

  4. json中jobject

    Json.net codeplex :http://www.codeplex.com/Json 原本感觉Newtonsoft.Json和.net自己的JavaScriptSerializer相差无几, ...

  5. 【mysql的设计与优化专题(4)】表的垂直拆分和水平拆分

    垂直拆分 垂直拆分是指数据表列的拆分,把一张列比较多的表拆分为多张表 通常我们按以下原则进行垂直拆分: 把不常用的字段单独放在一张表; 把text,blob等大字段拆分出来放在附表中; 经常组合查询的 ...

  6. Servlet课程0424(三) 通过继承HttpServlet来开发Servlet

    //这是第三种开发servlet的方法,通过继承httpservlet package com.tsinghua; import javax.servlet.http.*; import java.i ...

  7. Java泛型:泛型类、泛型接口和泛型方法

    根据<Java编程思想 (第4版)>中的描述,泛型出现的动机在于: 有许多原因促成了泛型的出现,而最引人注意的一个原因,就是为了创建容器类. 泛型类 容器类应该算得上最具重用性的类库之一. ...

  8. Qt: 绘图基础(非常简洁明了,全面)

    QPainter 能绘制: point, line, rectangle, ellipse, arc, chord, polygon, pie segment, Bezier curve, QPixm ...

  9. FocusWriter

    2. FocusWriter 如果你正在从事某种写作——小说.博客.文档等——你绝对希望认识一下FocusWriter.它已经有近十年的发布历史了,但是一直是我们最喜欢的无分心写作应用之一.如果你希望 ...

  10. javeWeb常用快捷键 Junit for changeableargs enumn reflect

    *1 工具常用的快捷键 1) Eclipse和MyEclipse,IBM,2001,Java编写,开源,跨平台跨语言   2)Alt+/快速内容提示   3)Ctrl+1快速修补错误   4)Syso ...