【暑假】[实用数据结构]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 ...
随机推荐
- HTTP POST GET 区别
一 原理区别 一般在浏览器中输入网址访问资源都是通过GET方式:在FORM提交中,可以通过Method指定提交方式为GET或者POST,默认为GET提交 Http定义了与服务器交互的不同方法,最基本的 ...
- hibernate annotation注解 主键ID自增长
@Id @SequenceGenerator(name="increment") @GeneratedValue(strategy=GenerationType.AUTO, gen ...
- adobe 蛋疼的套装, 想安装一个Flash Professional CS6,标准版还没有...
产品比较 查看内容 查看各 Creative Suite 6 版本的组件. Design Standard Design & Web Premium Production Premium Ma ...
- linux系统的crond服务
linux系统中有一个服务,用来做周期性运行的例行任务,这个服务就是crond服务.执行这项服务的命令 就是crontab命令了.而linux下的任务调度又分为系统任务调度和用户任务调度两个大类. 系 ...
- DSP6455 DSP/BIOS中断配置问题(是否需要ECM-事件组合以及实例)
2013-06-20 21:08:48 中断的配置有两种常用的方式: 一是通过CSL提供的API进行配置,这种方法相对DSP/BIOS偏底层,也比较麻烦:这种方法要求对中断系统的工作方式很清楚. 二是 ...
- Visual C#每一次新版本的变化
What's New in Visual C# .NET 2003[Visual Studio .NET 2003] What's New in Visual C# 2005 What's New i ...
- [原]Unity3D深入浅出 - 常见三维软件与Unity3D的单位比例
Unity3d系统默认单位是米 Maya:Unity3D 1M:100M 3DMax:Unity3D 100M:1M Cinema 4D:Unity3D 1M:100M Lightwavew:Uni ...
- bzoj2431:[HAOI2009]逆序对数列
单组数据比51nod的那道题还弱...而且连优化都不用了.. #include<cstdio> #include<cstring> #include<cctype> ...
- Oracle Data Guard
DG 是 Oracle Data Guard 的简称.也就是Oracle11g的 数据卫士. 由于在工作中 Oracle和 SQL SERVER2008 同时都需要维护管理.给我的感觉这里的 DG 其 ...
- (四)学习CSS之position、bottom、left、right和top属性
参考:http://www.w3school.com.cn/cssref/pr_class_position.asp position 属性规定元素的定位类型. 这个属性定义建立元素布局所用的定位机制 ...