加权并查集

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#define MAXN 20000+10
#define pii pair<int,int>
using namespace std;
int fa[MAXN];
int d[MAXN];
int n;
int Abs(int x){
return (x>?x:-x);
}
pii find(int x){
if(fa[x]!=x){
pii t=find(fa[x]);
fa[x]=t.first;
d[x]+=t.second;
}
return make_pair(fa[x],d[x]);
}
void solve(){
scanf("%d",&n);
for(int i=;i<=n;i++){
fa[i]=i,d[i]=;
}
char ch[]={};
while(scanf("%s",ch)&&ch[]!='O'){
if(ch[]=='E'){
int x;
scanf("%d",&x);
printf("%d\n",find(x).second);
}
else{
int x,y;
scanf("%d%d",&x,&y);
fa[x]=y;
d[x]=Abs(x-y)%;
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
solve();
}
return ;
}

UVALive - 3027:Corporative Network的更多相关文章

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

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

  2. UVALive 3027 Corporative Network 带权并查集

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

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

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

  4. UVALive 3027 Corporative Network

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

  5. 3027 - Corporative Network(并差集)

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

  6. 3027 - Corporative Network

    3027 - Corporative Network 思路:并查集: cost记录当前点到根节点的距离,每次合并时路径压缩将cost更新. 1 #include<stdio.h> 2 #i ...

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

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

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

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

  9. 二分+最短路 uvalive 3270 Simplified GSM Network(推荐)

    // 二分+最短路 uvalive 3270 Simplified GSM Network(推荐) // 题意:已知B(1≤B≤50)个信号站和C(1≤C≤50)座城市的坐标,坐标的绝对值不大于100 ...

随机推荐

  1. 高级软件工程2017第3次作业——结对项目:四则运算题目生成程序(基于GUI)

    Deadline:2017-10-11(周三)21:00pm (注:以下内容参考集大作业 ) 前言 想过和别人一起探索世界吗?多么希望,遇到困难时,有人能一起探讨:想要懈怠时,有人推你一把:当你专注于 ...

  2. JVM笔记7-内存分配与回收策略

    1.对象优先在Eden分配 大多数情况下,对象在新生代Eden区中分配.当Eden区中没有足够空间分配时,虚拟机将发起一次Minor GC.虚拟机提供了-XX:PrintGCDetails 这个收集器 ...

  3. 15-TypeScript策略模式

    在前面的简单工厂模式中,通常将每个类.接口定义到不同的文件中.在面向对象开发思想中有一个重要的原则就是封装变化点,在实际操作过程中, 通常被调用方的代码不要去更改,而是增加,这是面向对象的开闭原则.在 ...

  4. Windows中添加自己的程序到开机启动中(添加服务,添加注册表)

    在系统启动的时候启动自己想要启动的程序: 方法一:利用开机启动文件夹 将exe文件或exe文件的快捷方式复制到(启动)文件夹下 以win7为例:开始→所有程序→启动→鼠标右键打开 方法二:添加系统服务 ...

  5. Nagios监控的部署与配置

    [安装Nagios] yum install -y httpd httpd-devel httpd-tools mysql mysql-devel mysql-server php php-devel ...

  6. Python内置函数(40)——dir

    英文文档: dir([object]) Without arguments, return the list of names in the current local scope. With an ...

  7. mosquitto安装和测试

    一.安装 1.windows安装 安装完毕,更新安装目录的dll文件 2.linux安装 编译保存用户数据到数据库的插件 安装 3.启动 mosquitto mosquitto mosquitto_p ...

  8. spring data redis template GenericJackson2JsonRedisSerializer的使用

    配置 <!-- redis template definition --> <bean id="myRedisTemplate" class="org. ...

  9. Linux磁盘分区-rpm-yum

    一.磁盘分区 1.开启Linux系统前添加一块大小为15G的SCSI硬盘 2.开启系统,右击桌面,打开终端 3.为新加的硬盘分区,一个主分区大小为5G,剩余空间给扩展分区,在扩展分区上划分1个逻辑分区 ...

  10. 不错的ngix/redis/java/android学习地址

    http://blog.csdn.net/xlgen157387/article/details/50051543 徐刘根的博客,好像是“Java后端技术”微信公众号的建立者,反正看到不少关于他的博文 ...