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 }

3027 - Corporative Network的更多相关文章

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

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

  2. 3027 - Corporative Network(并差集)

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

  3. UVALive 3027 Corporative Network

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

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

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

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

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

  6. UVALive 3027 Corporative Network 带权并查集

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

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

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

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

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

  9. LA 3027 Corporative Network

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

随机推荐

  1. 数据库(database)介绍

    0.数据定义:除了文本类型的数据,图像.音乐.声音都是数据. 数据分类:结构化数据.非结构化数据.1.数据库定义:"电子化的文件柜","数据仓库".数据库是一个 ...

  2. C/C++ Qt StandardItemModel 数据模型应用

    QStandardItemModel 是标准的以项数据为单位的基于M/V模型的一种标准数据管理方式,Model/View 是Qt中的一种数据编排结构,其中Model代表模型,View代表视图,视图是显 ...

  3. hbase调优

    @ 目录 一.phoenix调优 1.建立索引超时,查询超时 2.预分区 hbase shell预分区 phoenix预分区 3.在创建表的时候指定salting. 4.二级索引 建立行键与列值的映射 ...

  4. Echart显示后端mysql数据

    一.基本思想 1.将数据存储在mysql数据库中 2.后端链接数据库,将数据库中的数据保存为json格式 3.将json格式数据使用ajax传到前端JSP页面中的Echarts 二.实现的关键点 1. ...

  5. GO 总章

    GO 学习资源 go 代理 GO 语言结构 GO 数字运算 GO 时间处理 GO 定时器 GO 异常处理 go recover让崩溃的程序继续执行 GO Exit Fatal panic GO 通过进 ...

  6. spring boot集成swagger文档

    pom <!-- swagger --> <dependency> <groupId>io.springfox</groupId> <artifa ...

  7. Can a C++ class have an object of self type?

    A class declaration can contain static object of self type,it can also have pointer to self type,but ...

  8. 【Linux】【Services】【SaaS】Docker+kubernetes(10. 利用反向代理实现服务高可用)

    1. 简介 1.1. 由于K8S并没有自己的集群,所以需要借助其他软件来实现,公司的生产环境使用的是Nginx,想要支持TCP转发要额外安装模块,测试环境中我就使用HAPROXY了 1.2. 由于是做 ...

  9. CountDownLatch原理

    正如每个Java文档所描述的那样,CountDownLatch是一个同步工具类,它允许一个或多个线程一直等待,直到其他线程的操作执行完后再执行.在Java并发中,countdownlatch的概念是一 ...

  10. liunx 安装ActiveMQ 及 spring boot 初步整合 activemq

    源码地址:  https://gitee.com/kevin9401/microservice.git 一.安装 ActiveMQ: 1. 下载 ActiveMQ wget  https://arch ...