思路

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

代码

#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. linux 压缩和解压

    xz 压缩和解压 tar -Jcvf backup.tar.xz backup/ tar -Jxvf backup.tar.xz 加参数 p,使文件保持原来的权限. tar -Jcvpf timesh ...

  2. phpcms栏目标签调用

    $CATEGORY[$catid][catid]                栏目id   $CATEGORY[$catid][module]                栏目所在的模块   $C ...

  3. java.io.UTFDataFormatException: encoded string too long:

    java.io.UTFDataFormatException: encoded string too long: 259553 bytes 按如下修改可避开此问题. - output.writeUTF ...

  4. Luogu 1309 - 瑞士轮 - [归并排序]

    题目链接:https://www.luogu.org/problemnew/show/P1309 题解: 每次比赛前,每个人都是按照分数降序排好的,那么比赛完后,将选手按输赢分成两组,顺序依然按照原顺 ...

  5. Git忽略提交规则 - .gitignore配置运维总结

    在使用Git的过程中,我们喜欢有的文件比如日志,临时文件,编译的中间文件等不要提交到代码仓库,这时就要设置相应的忽略规则,来忽略这些文件的提交.简单来说一个场景:在你使用git add .的时候,遇到 ...

  6. 数组/Array/Tuple/yield

    数组 如果需要使用同一类型的多个对象,就可以考虑使用集合和数组.如果需要使用不同类型的多个对象,可以考虑使用Tuple(元组) 数组的声明 在声明数组时,应先定义数组元素中的类型,其后是一对空方括号和 ...

  7. SVN修改已经提交过记录的日志和作者

    原 SVN修改已经提交过记录的日志和作者 使用TortoiseSVN管理代码,对于已经提交的记录,可以修改提交作者和提交日志,不过会报如下错误: Repository has not been ena ...

  8. block,inline和inline-block概念和区别(转载)

    转自: http://www.cnblogs.com/KeithWang/p/3139517.html 总体概念 block和inline这两个概念是简略的说法,完整确切的说应该是 block-lev ...

  9. LDO选型注意事项

    以前选择LDO时因为要求不高,只会考虑输入电压Vin,输出电压Vout以及最大输出电流Ioutmax,其他的参数基本不做考虑,后来发现,考虑的太不周到,现在做个笔记记录自己的一些心得. 1.考虑最大输 ...

  10. java解答:有17个人围成一圈(编号0~16),从第0号的人开始从1报数,凡报到3的倍数的人离开圈子,然后再数下去,直到最后只剩下一个人为止,问此人原来的位置是多少号?

    package ttt; import java.util.HashMap; import java.util.Map.Entry; /** * 有17个人围成一圈(编号0~16),从第0号的人开始从 ...