#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <ctime> using namespace std; struct Node
{
int f,d;
}; Node node[];
int T,n,u,v; /*
void findset(int x,int &d,int &f)
{
if(node[x].f==x) return;
findset(f,node[f].d,node[f].f);
d+=node[f].d;
f=node[f].f;
}
*/ int findset(int x)
{
if(node[x].f==x) return x;
int tmp=findset(node[x].f);//tmp暂时存下返回值,因为node[x].f要在node[x].d+=node[node[x].f].d后才能改变
node[x].d+=node[node[x].f].d;
return node[x].f=tmp;
} int main()
{
//freopen("/home/user/桌面/in","r",stdin);
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
char op[];
for(int i=;i<=;i++)
{
node[i].d=;
node[i].f=i;
}
while(scanf("%s",op)==&&op[]!='O')
{
if(op[]=='I')//I i j
{
scanf("%d%d",&u,&v);
node[u].f=v;
node[u].d+=abs(u-v)%;
//printf("%d %d\n",node[u].f,node[u].d);
}
else
{
scanf("%d",&u);
findset(u);
printf("%d\n",node[u].d);
//for(int i=1;i<=4;i++)
//printf("f=%d d=%d\n",node[i].f,node[i].d);
}
}
}
//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
return ;
}

UVALive 3027 并查集的更多相关文章

  1. F - Number of Connected Components UVALive - 7638 (并查集 + 思维)

    题目链接:https://cn.vjudge.net/contest/275589#problem/F 题目大意:就是给你n个数,如果说两个数之间的gcd!=1,那么就将这两个点连起来,问你最终这些点 ...

  2. UVALive 3027 Corporative Network (带权并查集)

    题意: 有 n 个节点,初始时每个节点的父节点都不存在,你的任务是执行一次 I 操作 和 E 操作,含义如下: I  u  v   :  把节点 u  的父节点设为 v  ,距离为| u - v | ...

  3. UVALive 3027 Corporative Network 带权并查集

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

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

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

  5. UVALive 6187 Never Wait for Weights 带权并查集

    题意:每次给出每两个数之间的大小差值.在给出关系的过程中插入询问:数a和数b的差值,若不能确定,输出UNKNOWN 解法:相对大小关系的处理:并查集 1.给出两点的相对大小关系后,找到两个点的根节点, ...

  6. UVALive 7456 Least Crucial Node (并查集)

    Least Crucial Node 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/C Description http://7 ...

  7. UVALive 6910 Cutting Tree(离线逆序并查集)

    [题目]:(地址:) http://acm.hust.edu.cn/vjudge/contest/view.action?cid=97671#problem/E [题意]: 给出多棵树和两类操作:操作 ...

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

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

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

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

随机推荐

  1. strpos 返回0时 ,比较false 不能加单引号

    $a =  'a.approve'; $num = strpos($a,'a.admin'); if(strpos($a,'a.approve') !== 'false'){ //不能加单引号.变字符 ...

  2. Linux(power服务器)中kettle(2)

    Hadoop集群硬件环境 4台机器 ip地址 172.16.1.131 172.16.1.132 172.16.1.133 172.16.1.134 每台内存16G 8核cpu 直接使用报错:

  3. delphi怎么实现全选的功能

    1. SelectAll 可以实现全选功能 Delphi/Pascal code edit1.SelectAll; // Delphi/Pascal code RichEdit1.SelStart:= ...

  4. poj2140(奇因数的个数)

    #include<stdio.h>int main(){ int n,s=0; scanf("%d",&n); for(int i=1;i<=n;i++) ...

  5. redis高级实用特性(2)

    事务处理: redis对事务的支持目前还是比较简单,redis只能保证一个 client发起事务中的命令可以连续执行,而中间不会插入其他 client的命令,当一个client 在一个连接中发出mul ...

  6. MySQL binlog 查看信息

    1)按时间筛选 mysqlbinlog --start-datetime="2009-09-14 0:20:00" --stop-datetim="2009-09-15 ...

  7. As3.0 TextField

    一 TextField 对象的方法 方法 说明 TextField.addListener 加入接收触发事件如文本域内容变化或滚动变化的监听对象,触发事件可以参看最后一个表. TextField.ge ...

  8. 3.Perl 多线程:Threads(exit thread_only)

    还可以在导入threads模块时设置: use threads ('exit' => 'thread_only');

  9. gridControl 部分属性

    DEVexpress GridControl 属性设置 2013年11月22日 ⁄ 综合 ⁄ 共 18319字 ⁄ 字号 小 中 大 ⁄ 评论关闭 1. 如何解决单击记录整行选中的问题 View-&g ...

  10. Chapter 1 First Sight——35

    The final bell rang at last. I walked slowly to the office to return my paperwork. 最后下课铃响了.我走到了办公室上讲 ...