思路

用带权并查集维护到根的距离即可

代码

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
int fa[101000],dis[101000],n,T;
void init(void){
for(int i=1;i<=100000;i++)
fa[i]=i,dis[i]=0;
}
int find(int x){
if(fa[x]==x){
dis[x]=0;
return x;
}
else{
int t=find(fa[x]);
dis[x]+=dis[fa[x]];
return fa[x]=t;
}
}
void merge(int x,int y){//x->y
fa[x]=y;
dis[x]=abs(y-x)%1000;
}
int main(){
scanf("%d",&T);
while(T--){
init();
scanf("%d",&n);
char c=getchar();
while(c!='E'&&c!='I'&&c!='O')
c=getchar();
while(c!='O'){
if(c=='E'){
int u;
scanf("%d",&u);
find(u);
printf("%d\n",dis[u]);
}
else{
int u,v;
scanf("%d %d",&u,&v);
merge(u,v);
}
c=getchar();
while(c!='E'&&c!='I'&&c!='O')
c=getchar();
}
}
return 0;
}

UVA1329 Corporative Network的更多相关文章

  1. UVALive 3027 Corporative Network

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

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

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

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

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

  4. 3027 - Corporative Network(并差集)

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

  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. 【35.86%】【POJ 1962】Corporative Network

    Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 3943 Accepted: 1414 Description A very bi ...

  9. Corporative Network (有n个节点,然后执行I u,v(把u的父节点设为v)和E u(询问u到根节点的距离))并查集

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

随机推荐

  1. __super:: 使用

    它会调用 基类的函数方法实现. __super::Close(); __super::Find();

  2. Javascript htmldecode

    // HtmlDecode http://lab.msdn.microsoft.com/annotations/htmldecode.js // client side version of the ...

  3. 编译安装centos6.9 php7.0 mysql5.6 nginx1.8

    2018年3月15日 11:41:40 注意文章时效! 此文章不是给新用户参考的,没有每一步的操作,请注意! yum -y groupinstall "Development Tools&q ...

  4. bootstrap 中关于 HTML5 aria-* and role的用法

    HTML5 aria-* and role 在bootstrap中看到role和aria-*,不知道干嘛的.google一下,发现aria的意思是Accessible Rich Internet Ap ...

  5. jQuery Mobile的默认配置项具体解释,jQuery Mobile的中文配置api,jQuery Mobile的配置说明,配置大全

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/xmt1139057136/article/details/35258199 学习jQuery Mob ...

  6. python tkinter button

    """小白随笔,大佬勿喷""" '''Button按钮 点击执行对应的命令''' import tkinter as tk #初始化窗口 w ...

  7. [CSS] Frequently used method or solutions for issues

    Stick button in right side in html Solution: //In the html <div class="float__button" & ...

  8. AppFabric查询工作流实例

    安装 通过IIS查询工作流实例,可以操作挂起,首先打开WF+WCF的站点: 这里可以搜索工作流实例:例如根据工作流ID.创建日期.状态等查询 下方的搜索结果可以查看结果 资源 Windows Serv ...

  9. List查询重复值的个数,并根据重复的数目从多到少排列

    package ttt; import java.nio.MappedByteBuffer; import java.util.ArrayList; import java.util.Collecti ...

  10. html5 javascript 事件练习2

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...