【暑假】[实用数据结构]UVAlive 3027 Corporative Network
UVAlive 3027 Corporative Network
题目:
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 3450 | Accepted: 1259 |
Description
Input
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
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的更多相关文章
- UVALive 3027 Corporative Network
---恢复内容开始--- Corporative Network Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld ...
- UVALive 3027 Corporative Network 带权并查集
Corporative Network A very big corporation is developing its corporative networ ...
- 并查集 + 路径压缩(经典) UVALive 3027 Corporative Network
Corporative Network Problem's Link Mean: 有n个结点,一开始所有结点都是相互独立的,有两种操作: I u v:把v设为u的父节点,edge(u,v)的距离为ab ...
- UVALive - 3027 Corporative Network (并查集)
这题比较简单,注意路径压缩即可. AC代码 //#define LOCAL #include <stdio.h> #include <algorithm> using name ...
- UVALive 3027 Corporative Network (带权并查集)
题意: 有 n 个节点,初始时每个节点的父节点都不存在,你的任务是执行一次 I 操作 和 E 操作,含义如下: I u v : 把节点 u 的父节点设为 v ,距离为| u - v | ...
- 3027 - Corporative Network(并差集)
3027 - Corporative Network A very big corporation is developing its corporative network. In the begi ...
- 3027 - Corporative Network
3027 - Corporative Network 思路:并查集: cost记录当前点到根节点的距离,每次合并时路径压缩将cost更新. 1 #include<stdio.h> 2 #i ...
- [LA] 3027 - Corporative Network [并查集]
A very big corporation is developing its corporative network. In the beginning each of the N enterpr ...
- LA 3027 Corporative Network 并查集记录点到根的距离
Corporative Network Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu [S ...
随机推荐
- DX 的.x 文件
template Header { <3D82AB43-62DA-11cf-AB39-0020AF71E433> WORD major; WORD minor; DWORD flags;} ...
- 关于ref与out的区别
写在最前面 这几天一直在公司接受培训,都是一些基础的知识,同时也乘着这个机会巩固一下自己的基础,基础太重要了.前些时一直看的是多线程方面的知识,接下来我会写一些其他方面的知识,毕竟作为一个实习新人得和 ...
- 论反馈信息如何推动 IT 运维团队进步?
我们还记得<快乐大本营>中经典游戏----快乐传真吗?游戏规则是:很多人站一排,只有第一个人才看到最准确的信息,用东西隔着,戴耳机,一一将从前一个人获得的信息传递下去,最后一个人说出推测的 ...
- c/c++ define用法
define,无参宏定义的一般形式为:#define 标识符 字符串 外文名 define 词条范围 计算机专业用语 无参一般形式 #define 标识符 字符串 带参一般形式 #define 宏名( ...
- 【前端学习】【CSS选择器】
CSS选择器 CSS选择器 CSS(Cascading Style Sheets)是一项出色的技术,它使得网页的结构和表现样式完全分离.利用CSS选择器能轻松地对某个元素添加样式而不改动HTM ...
- 使用头文件climits中的符号常量获知整型数据的表数范围---gyy整理
在头文件climits(limits.h)以宏定义的方式定义了各种符号常量来表示各种整型类型表示数的范围,如int的最大最小值,long的最大最小值等. 符号常量 表示 CHAR_BIT char 的 ...
- Qt: 访问容器(三种方法,加上for循环就四种了)good
#include <iostream>#include <QString>#include <QList>#include <QListIterator> ...
- QT插件开发方式(作者有RemOjbects文档翻译(48)篇)
创建一个QT的库项目,删除自动生成的.h和.cpp文件,添加一个接口定义.h文件和一个接口实现类(一个.h一个.cpp).代码如下: 1.接口文件源码 #ifndef PLUGININTERFACE_ ...
- Qt之QtSoap(访问WebService)
http://blog.csdn.net/u011012932/article/details/51673800
- QListWidget代码刷新界面
我有一个特殊效果要求实现(其实很弱智,也变成特殊效果,汗一下自己):两个QRadioButton切换的时候,让旁边的QListWidget自动变化不同的背景色.想了很多办法: 1. 控件自己刷新,不行 ...