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. hbase调优

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

  2. A Child's History of England.11

    CHAPTER 4 ENGLAND UNDER ATHELSTAN AND THE SIX BOY-KINGS Athelstan, the son of Edward the Elder, succ ...

  3. 【Git项目管理】git新手入门——基础教程

    一.Git工作流程 直接上手看图,了解Git工具的工作流程: 以上包括一些简单而常用的命令,但是先不关心这些,先来了解下面这4个专有名词. Workspace:工作区 Index / Stage:暂存 ...

  4. Oracle学习笔记(1)

    折腾了好久 终于把oracle安装成功了.小兴奋下. 创建了一个数据库 dabook. run--> Services.msc查看服务: 可以看到DABOOK的服务已启动. 1,sys用户 在c ...

  5. Vue.js 学习

    一,Vue.js 介绍 Vue 是一套用于构建用户界面的渐进式javascript框架,与其它大型框架不同的是:Vue被设计为可以自底向上逐层应用.Vue的核心库只关注视图层,不仅易于上手,还便于与第 ...

  6. shell脚本统计多个CPU利用率

    本节主要内容:top命令统计CPU的利用率 一,问题分析 MySQL在Linux下是多线程的,而且只能将多个线程分布到一个CPU上.因此,使用小型服务器,或者PC SERVER,多个CPU利用率并不高 ...

  7. java实现数组集合转成json格式

    一.下载fastjson.jar http://repo1.maven.org/maven2/com/alibaba/fastjson 二.项目添加jar包 Java Build Path 三.导入类 ...

  8. .Net Core MVC全局过滤器验证是否需要登录

    1.新增全局登录过滤器LoginCheckAttribute 1 public class LoginCheckAttribute: ActionFilterAttribute 2 { 3 publi ...

  9. 关于使用Topshelf创建服务

    目录 0. 背景说明 1. 使用Topshelf组件创建Windows服务 1.1 依赖Quartz.net实现定时任务 1.2 依赖于Topshelf创建服务类 1.3 log4net的配置文件lo ...

  10. Docker从入门到精通(三)——概念与执行流程

    前面我们大概介绍了docker是什么以及如何安装docker,但是对里面出现的一些名词,可能大家还不熟悉,这篇文章就来为大家解惑. 1.容器化平台 Docker 是提供应用打包,部署与运行应用的容器化 ...