UVALive 3027 并查集
#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 并查集的更多相关文章
- F - Number of Connected Components UVALive - 7638 (并查集 + 思维)
题目链接:https://cn.vjudge.net/contest/275589#problem/F 题目大意:就是给你n个数,如果说两个数之间的gcd!=1,那么就将这两个点连起来,问你最终这些点 ...
- UVALive 3027 Corporative Network (带权并查集)
题意: 有 n 个节点,初始时每个节点的父节点都不存在,你的任务是执行一次 I 操作 和 E 操作,含义如下: I u v : 把节点 u 的父节点设为 v ,距离为| u - v | ...
- UVALive 3027 Corporative Network 带权并查集
Corporative Network A very big corporation is developing its corporative networ ...
- 并查集 + 路径压缩(经典) UVALive 3027 Corporative Network
Corporative Network Problem's Link Mean: 有n个结点,一开始所有结点都是相互独立的,有两种操作: I u v:把v设为u的父节点,edge(u,v)的距离为ab ...
- UVALive 6187 Never Wait for Weights 带权并查集
题意:每次给出每两个数之间的大小差值.在给出关系的过程中插入询问:数a和数b的差值,若不能确定,输出UNKNOWN 解法:相对大小关系的处理:并查集 1.给出两点的相对大小关系后,找到两个点的根节点, ...
- UVALive 7456 Least Crucial Node (并查集)
Least Crucial Node 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/C Description http://7 ...
- UVALive 6910 Cutting Tree(离线逆序并查集)
[题目]:(地址:) http://acm.hust.edu.cn/vjudge/contest/view.action?cid=97671#problem/E [题意]: 给出多棵树和两类操作:操作 ...
- [LA] 3027 - Corporative Network [并查集]
A very big corporation is developing its corporative network. In the beginning each of the N enterpr ...
- LA 3027 Corporative Network 并查集记录点到根的距离
Corporative Network Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu [S ...
随机推荐
- MongoDB数据模型(三)
六.数据模型引用 文档 我们已经知道MongoDB以文档的形式存储数据,而文档是JSON风格的数据结构,由一系列的“字段名-值”对组成,如下所示 { "item": "p ...
- openwrt 串口无法登陆
打印这个消息后串口无法进行交互 procd: - init complete - 主要问题是 /etc下inittab脚本中 ::sysinit:/etc/init.d/rcS S boot::shu ...
- Excel教程(13) - 统计函数
AVEDEV 用途:返回一组数据与其平均值的绝对偏差的平均值,该 函数可以评测数据(例如学生的某科考试成绩)的离散度. 语法:AVEDEV(number1,number2,...) 参数:Number ...
- hdu 1028 Ignatius and the Princess III(母函数入门+模板)
Description "Well, it seems the first problem is too easy. I will let you know how foolish you ...
- HttpReceiveRequestEntityBody 使用应注意的地方
如果EntityBody数据很大,调用此函数是不能完全接收全部数据的,我们不能简单的判断 1: BYTE* pBuffer = new BYTE[4096]; 2: ZeroMemory(pBuffe ...
- SDK does not contain any platforms. error (android)
By default sdk was installed under the C:\Users\<user_name>\AppData\Local\Android\sdk\ directo ...
- 关于dubbo服务产生异常之:Caused by: com.alibaba.dubbo.remoting.TimeoutException: Waiting server-side response timeout by scan timer.
简单来说就是dubbo超时,因为dubbo默认的时间是500ms,超过这个时间它会重新访问service层,最多尝试三次. 所以我在测试的时候日志显示出来的异常为……timeout……. 开始设置开始 ...
- UIScrollView 加载多个view view还可以交换顺序(2)
在上的代码中 UIview都在一个页面 要是每一个view对应一个页面 操作每一个view代码都在viewcontroller里面就会很乱,那么我们就将view重新做一下 创建想要的页面继承与view ...
- caffe层解读系列-softmax_loss
转自:http://blog.csdn.net/shuzfan/article/details/51460895 Loss Function softmax_loss的计算包含2步: (1)计算sof ...
- elipse图标注解
Java中的访问修饰符(访问控制符)包括:public,protected,default,private.分别代表了不同的访问权限.如果省略,则被视为使用了默认的default作为访问修饰符.从字面 ...