3027 - Corporative Network

3027 - Corporative Network

思路:并查集;

cost记录当前点到根节点的距离,每次合并时路径压缩将cost更新。

 1 #include<stdio.h>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<string.h>
 5 #include<math.h>
 6 #include<stack>
 7 #include<set>
 8 #include<queue>
 9 using namespace std;
10 int   bin[20005];
11 int   du[20005];
12 char str[20];
13 int main(void)
14 {
15         int i,j;
16         int n;
17         scanf("%d",&n);
18         while(n--)
19         {
20                 int m;
21                 scanf("%d",&m);
22                 for(i = 1; i <= 20005; i++)
23                         bin[i] = i,du[i] = 0;
24                 while(scanf("%s",str),str[0]!=‘O‘)
25                 {
26                         int x,y;
27                         if(str[0]==‘I‘)
28                         {
29                                 scanf("%d %d",&x,&y);
30                                 int xx;
31                                 for(xx = y; bin[xx]!=xx;)
32                                 {
33                                         xx = bin[xx];
34                                         du[y]+=du[xx];
35                                 }
36                                 bin[y] = xx;
37                                 bin[x] = xx;
38                                 du[x] = du[y]+abs(x-y)%1000;
39                         }
40                         else
41                         {      int sum = 0;
42                                 int xx;scanf("%d",&x);
43                                 for(xx = x; bin[xx]!=xx;)
44                                 {
45                                         xx=bin[xx],du[x]+=du[xx];
46                                 }
47                                 bin[x] = xx;
48                                 printf("%d\n",du[x]);
49                         }
50                 }
51         }
52         return 0;
53 }
时间: 2024-10-18 07:15:48

3027 - Corporative Network的相关文章

[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

[2016-03-19][UVALive][3027][Corporative Network]

时间:2016-03-19 13:24:23 星期六 题目编号:[2016-03-19][UVALive][3027][Corporative Network] 题目大意:给定n个节点,I u v表示把u节点的父节点设置为v,距离为|u-v|%1000,E u表示询问u到根节点的距离,给定若干个I E操作,输出相应答案 分析:带权并查集 方法:d[maxn] 维护到父节点的距离,每次询问的时候,把当前节点压缩到根节点输出对应距离即可 #ifdef _WORK_ #include <algorit

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

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

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 ent

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,

(DS 《算法竞赛入门经典》)LA 3027 Corporative Network(查询某一个节点到根节点之间的距离)

题目大意: 查询某一个节点到根节点之间的距离 解题思路: 加权并查集问题.之前做的题目是"查看两个或多个节点是否在同一个集合下",现在的题目是"查询某个节点到 根节点之间的距离".之前只需要使用到father[x]这个数组,用来表示x的父亲节点是谁.现在引入dist[x]数组,用来记录 x节点到根节点的距离 1)在并查集中,根节点不懂,其他节点都可以动. A very big corporation is developing its corporative net

LA 3027 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, for amelioration of the services, the co

UVA 3027 Corporative Network 带权并查集、

题意:一个企业要去收购一些公司把,使的每个企业之间互联,刚开始每个公司互相独立 给出n个公司,两种操作 E I:询问I到I它连接点最后一个公司的距离 I I J:将I公司指向J公司,也就是J公司是I公司的上级,距离为abs(I-J)%1000(貌似G++不支持abs,PE了两发) 思路:转化一下题意就行了,首先刚开始的时候每个公司都是独立的,I操作就是并查集中合并操作,将I这课树并到J这个树上, E操作要求的东西就是 I到I的根节点的距离,先看一个没有路径压缩直接暴力的方法把.(本以为不会过的,

UVALive 3027 Corporative Network(并查集之四)

问题描述: 求出点到根的距离,带权并查集. 代码思路: 在 I 命令的执行过程中,并不路径压缩. 当执行 E 查询命令时在路径压缩的同时递归求解存储在dist数组里面. 代码: #include<stdio.h> #include<math.h> #include<stdlib.h> #include<string.h> #define MOD 1000 #define MAX 20000 int pre[MAX+7],dist[MAX+7]; int fi