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 ...
随机推荐
- 支付顺序-->微信支付到公司账户-->待出票
支付顺序-->微信支付到公司账户-->待出票-->查询所有待出票订单 -->遍历提交订单-->火车票接口放回订单id-->存入order订单表中 -->读取订 ...
- COCOA® PROGRAMMING FOR MAC® OS X (2)- Speak Line
1.NSControl是所有控件的父类,NSControl继承自NSView,因此NSControl是一个能够独立响应事件的NSView,每个NSControl含有一个Target和Action,当用 ...
- ESFramework 4.0 性能测试
本实验用于测试ESFramework服务端引擎的性能,测试程序使用ESFramework 4.0版本. 一.准备工作 测试的机器总共有3台,都是普通的PC,一台作为服务器,两台作为客户端. 作为服务器 ...
- USB 设备插拔事件处理
Windows 系统下,设备连接至电脑或从电脑移除,系统会广播一条 WM_DEVICECHANGE 消息到所有应用程序,在程序的消息处理函数中可以对事件进行相应. 1: class C ...
- CentOS系统更换软件安装源aliyun的
CentOS系统更换软件安装源第一步:备份你的原镜像文件,以免出错后可以恢复. mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS ...
- A - 小彭玉的扫荡食堂计划
A - 小彭玉的扫荡食堂计划 Time Limit: 20000/10000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) ...
- 配置mac自带的Apache服务器
第一步: 484 cd /etc/apache2 备份httpd.conf文件,以防万一 486 sudo cp httpd.conf httpd.conf.bak 如果操作错误,可以通过 491 ...
- MySQL DATE_FORMAT
MySQL DATE_FORMAT(date,format) 根据format字符串格式化date值 (在format字符串中可用标志符: %M 月名字(January……December) %W ...
- 多态性Polymorphism
一.多态性的概念: 1.多态:在面向对象方法中一般是这样表述多态性的: 向不同的对象发送同一个消息,不同的对象在接收时会产生不同的行为(即方法).也可以说,多态性是“一个接口,多种方法”. 2.从 ...
- A. Night at the Museum Round#376 (Div. 2)
A. Night at the Museum time limit per test 1 second memory limit per test 256 megabytes input standa ...