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 ...
随机推荐
- Linux入门(六)ubuntu下vim编辑器安装与使用
原文链接:http://jingyan.baidu.com/article/219f4bf7d03187de452d3862.html 一.安装 vim 控制台版本的vim随ubuntu一起发行,因此 ...
- memo用法总结
添加 mmo1.Lines.add('新加的一行');//追加一行文字 mmo1.Lines.Insert(1,'新插入一行');//在指定位置插入一行 删除 mmo1.Lines.Delete(1) ...
- golang的linux安装
1.wget https://storage.googleapis.com/golang/go1.6.2.linux-amd64.tar.gz tar -zxvf go1.6.2.linux-amd6 ...
- 基于多线程多用户的FTP服务器与客户端功能实现
项目介绍: 用户加密认证 允许同时多用户登录 每个用户有自己的家目录 ,且只能访问自己的家目录 对用户进行磁盘配额,每个用户的可用空间不同 允许用户在ftp server上随意切换目录 允许用户查看当 ...
- ThinkPHP框架基础
ThinkPHP 一.php框架基础介绍 真实项目开发步骤: 多人同时开发项目,协作开发项目.分工合理.效率有提高(代码风格不一样.分工不好) 测试阶段 上线运行 对项目进行维护.修改.升级(单个人维 ...
- wpf 数据绑定的4种形式
1.source 2.element 3.relativesource 4.datacontent
- opencv使用convexityDefects计算轮廓凸缺陷
引自:http://www.xuebuyuan.com/1684976.html http://blog.csdn.net/lichengyu/article/details/38392473 htt ...
- 计算机网络 NAT
NAT(Network Address Translation,网络地址转换)是1994年提出的.当在专用网内部的一些主机本来已经分配到了本地IP地址(即仅在本专用网内使用的专用地址),但现在又想和因 ...
- chrome浏览器调试工具的使用
废话不多说,给大家介绍一下最基本的浏览器调试工具
- 二〇一五年五月二十二日--bug--启动页面出现模糊的问题
启动页面出现模糊的问题: 原因是 :android:theme="@style/TranslucentTheme" <application android:name=&qu ...