Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

[Submit]   [Go Back]   [Status]

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

题目大意:有N个结点,初始时每个结点的父亲都不存在,你的任务时执行一次I操作和E操作,格式如下:

I u  v  :把结点u的父节点设为v,距离为|u-v|除以1000的余数,输入保证执行指令前u 没有父节点

E u   :询问u 到根节点的距离

思路:

带距离的并查集,在每次合并父亲节点的时候更新一个距离就可以了

可参考刘汝佳入门经典训练指南

#include <iostream>
using namespace std;
const int maxn = 20000 + 10;
int d[maxn], fa[maxn];
int find(int x)
{
if(x == fa[x]) return x;
else
{
int root = find(fa[x]);
d[x] += d[fa[x]];
return fa[x] = root;
}
}
int main()
{
int T, N, i, I, J;
cin>>T;
while(T--)
{
cin>>N;
for(i = 0; i <= N; i++)
{
fa[i] = i;
d[i] = 0; //自己到自己(根)的距离为0
}
char c;
bool ok = 1;
while(ok && cin>>c)
{
switch(c)
{
case 'O':
{
ok = 0;
break;
}
case 'I':
{
//int x,y;
cin>>I>>J;
/* x=find(I);
y=find(J);
if(x==y) continue;
fa[x]=y;
*/
fa[I] = J;
int ans = I > J ? (I-J) : (J-I);
d[I] =ans % 1000;
break;
}
case 'E':
{
cin>>I;
find(I);
cout<<d[I]<<endl;
break;
}
}
}
}
return 0;
}

不能按代码中注释的 那样

x=find(I);

y=find(J);

if(x==y) continue;

fa[x]=y;

代替 fa[I] = J;

一换掉 就wa

:  假设用dis表示点到根的距离

因为 为了求dis   所以谁的父亲就是谁的父亲 不能压缩 , 但是上面的find代码是进行压缩的     不过它是在递归求dis后 才压缩的    所以说 是可以压缩的

即 必须先递归 后压缩     这样就能保证求出的dis 确实是按照父亲节点算出来的

上面的那个错误  是由于先调用find函数压缩 ,这时候 点  I 的 dis还没有算出来,后面再求它的dis的时候 其就不再是按照真正的父亲节点找出的 而是按照祖先节点算的(因为压缩过了)

对于之后输入E   再次调用fand函数的时候  虽然已经压缩过了   但是所有点的dis已经求出来了  对于再加入的边 I  J   我们依旧能够通过接到已经求出的正确dis 求出来I 的dis

注意dis表示 点到根的距离

我的叙述能力比较差          想了好久才想出这么点东西  如果不对  请大家指正           希望有人能明白我讲述的意思吧

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=33982

LA 3027 Corporative Network 并查集记录点到根的距离的更多相关文章

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

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

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

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

  3. 【LA 3027 Corporative Network】

    ·一些很可爱的询问和修改,放松地去用并查集解决. ·英文题,述大意: 输入n(5<=n<=20000)表示树有n个节点,并且会EOF结束地读入不超过 20000个操作,一共有两种:    ...

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

    题目传送门 题意:训练指南P192 分析:主要就是一个在路径压缩的过程中,更新点i到根的距离 #include <bits/stdc++.h> using namespace std; c ...

  5. LA 3027 Corporative Network

    这题感觉和 POJ 1988 Cube Stacking 很像,在路径压缩的同时递归出来的时候跟新distant数组 我发现我一直WA的原因是,命令结束是以字母o结束的,而不是数字0!! //#def ...

  6. 树上战争(HDU 2545 并查集求解点到根节点长度)

    树上战争 Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  7. 3027 - Corporative Network

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

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

    UVAlive 3027 Corporative Network 题目:   Corporative Network Time Limit: 3000MS   Memory Limit: 30000K ...

  9. 3027 - Corporative Network(并差集)

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

随机推荐

  1. 在Ubuntu 12.04下编译qtiplot

    不在windows下,再加上不想用盗版,所以需要一个origin的替代品——qtiplot.虽然我非常抵抗用这种不停点来点去的软件,用R的ggplot2画图多好啊,高效.优雅.漂亮,但是终抵不过老板一 ...

  2. Hadoop应用开发实战(flume应用开发、搜索引擎算法、Pipes、集群、PageRank算法)

    Hadoop是2013年最热门的技术之一,通过北风网robby老师<深入浅出Hadoop实战开发>.<Hadoop应用开发实战>两套课程的学习,普通Java开发人员可以在最快的 ...

  3. VB与报表的交互

    还接着上次说.在上个博客中已经说到建立好表的步骤了,接下来就是怎么使表与vb连接. 先看一下代码. Option Explicit Dim WithEvents Report As grproLibC ...

  4. Android 事件处理

    目的:通过全面的分析Android的鼠标和键盘事件.了解Android中如何接收和处理键盘和鼠标事件,以及如何用代码来产生事件. 主要学习内容: 1. 接收并处理鼠标事件:按下.弹起.移动.双击.长按 ...

  5. 中间件(Middleware)

    中间件(Middleware) ASP.NET Core开发,开发并使用中间件(Middleware). 中间件是被组装成一个应用程序管道来处理请求和响应的软件组件. 每个组件选择是否传递给管道中的下 ...

  6. javascript学习笔记--迭代函数

    概要 这里的迭代函数指的是对数组对象的操作方法,js数组共有五个迭代函数:every.fifter.forEach.map.some. 1.every every方法,返回值为Boolean类型,tr ...

  7. VC++ WIN32 sdk实现按钮自绘详解 之二.

    网上找了很多,可只是给出代码,没有详细解释,不便初学者理解.我就抄回冷饭.把这个再拿出来说说. 实例图片:    首先建立一个标准的Win32 Application 工程.选择a simple Wi ...

  8. Delphi 模拟网站验证码(酷,把随机文字写道图片上)

    function TfrmLabelPages.PwdImg(img: Timage): string; var I,j,k: Integer; vPoint: TPoint; vLeft: Inte ...

  9. Qt4在linux下的安装

    1.下载SDK ftp://ftp.informatik.hu-berlin.de/pub/Mirrors/ftp.troll.no/QT/qtsdk/qt-sdk-linux-x86-opensou ...

  10. KL25开发板利用串口蓝牙与PC通信

    KL25开发板芯片本身支持三个串口,uart0,uart1,uart2.其中uart0不太一样,在数据手册里面单独一章介绍:而uart1和uart2则是一样的. 我所使用的串口蓝牙模块是BC04,支持 ...