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 }

最新文章

  1. 【leetcode】Intersection of Two Linked Lists
  2. NHibernate开发入门
  3. MyEclipse编码设置及字体设置等
  4. oracle中substr与instr
  5. REACT 学习
  6. NUC_TeamTEST_B(贪心)
  7. POJ-1979 Red and Black(DFS)
  8. Ubuntu安装Osmocom-BB一只猿多频点WEB脚本
  9. RMAN - 备份异机恢复
  10. 基于Elasticsearch进行地理检索,计算距离值
  11. Android 注意在finish Activity之后也要停止正在运行的请求
  12. 设置Windows的TCP/IP属性和内部网络号码
  13. leetcode修炼之路——83. Remove Duplicates from Sorted List
  14. 打勾显示输入的密码 --EditText与setTransformationMethod
  15. Mysql删除表名中有特殊字符的表
  16. Linux下PHP安装配置MongoDB数据库连接扩展
  17. Apache Flume 1.7.0 各个模块简介
  18. vue数据驱动作用域问题
  19. cassandra读源码---Streaming
  20. springboot连mysql报一个奇怪的错误

热门文章

  1. 取gridview中textbox的值【C#】
  2. 日常Java 2021/10/6
  3. npm下载错误解决办法
  4. Shell学习(三)——Shell条件控制和循环语句
  5. Linux基础命令---elinks文本浏览器
  6. android 调用相机拍照及相册
  7. vue-cli安装记录
  8. 【编程思想】【设计模式】【行为模式Behavioral】command
  9. java客户端的elasticSearch索引库的相关操作
  10. 快速上手ANTLR