思路

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

代码

#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. Dalvik虚拟机执行流程图

  2. Butterknife 导入项目配置

    在app的 build.gradle 文件中添加 dependencies { // Butterknifeapi 'com.jakewharton:butterknife:8.6.0'annotat ...

  3. php数组函数大全

    一.数组操作的基本函数 数组的键名和值 array_values($arr);获得数组的值 array_keys($arr);获得数组的键名 array_flip($arr);数组中的值与键名互换(如 ...

  4. HTML中的清除浮动的常用方法(转载)

    以下面的div为例: HTML: <div class="test"> <div class="test1"></div> ...

  5. 18.jwt加密

    jwt 官网https://jwt.io/ jwt:json web token jwt-simple: https://www.npmjs.com/package/jwt-simple jsonwe ...

  6. curl HTTP Header

      对于"User-Agent", "Cookie", "Host"这类标准的HTTP头部字段,通常会有另外一种设置方法.curl命令提供了 ...

  7. gui小计算器的程序写法

    import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.sw ...

  8. stm32f7699遇到的犯二问题

    没有看到stlink的驱动,难道板子坏了?? 结果:USB线的问题,换了一根用过的线,就行了:

  9. Brocade SAN交换机常用命令

    Brocade SAN交换机常用命令 使用电脑连接Brocade SAN交换机常用命令 使用电脑连接管理网口,默认IP地址为:10.77.77.77,掩码:255.255.255.0 默认用户名:ad ...

  10. linux 强制删除杀死进程 sudo pkill uwsgi -9 杀死uwsgi 关闭防火墙 iptables -F

    sudo pkill -f uwsgi -9 四.关闭防火强        iptables -F  #清空规则        systemctl stop firewalld  #关闭防火墙服务   ...