UVA 1329 Corporative Network【并查集】
题目链接:
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4075
题意:
有n个结点,開始都是单独的结点,如今有I操作和E操作,I u v表示吧u的父亲结点设为,距离为|u - v| % 1000,E操作询问u到根的距离
代码:
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <queue>
#include <stack>
#include <map>
using namespace std;
int fa[1000010];
int d[1000010];
int fd(int x)
{
if (fa[x] != -1)
{
int rot = fd(fa[x]);
d[x] += d[fa[x]];
return fa[x] = fd(fa[x]);
}
else return x;
}
int main()
{
int a, b;
int t, n;
scanf("%d",&t);
char cmd[10];
while (t--)
{
memset(fa, -1, sizeof(fa));
memset(d,0,sizeof(d));
scanf("%d", &n);
while (scanf("%s", cmd) && cmd[0] != 'O')
{
if (cmd[0] == 'E')
{
scanf("%d", &a);
fd(a);
printf("%d\n", d[a]);
}
else
{
scanf("%d%d", &a, &b);
fa[a] = b;
d[a] = abs(a - b) % 1000;
}
}
}
return 0;
}
UVA 1329 Corporative Network【并查集】的更多相关文章
- [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 ...
- UVa 1329 - Corporative Network Union Find题解
UVa的题目好多,本题是数据结构的运用,就是Union Find并查集的运用.主要使用路径压缩.甚至不须要合并树了,由于没有反复的连线和改动单亲节点的操作. 郁闷的就是不太熟悉这个Oj系统,竟然使用库 ...
- UVA 1329 - Corporative Network
带权值的并查集的应用: 代码: #include<cstdio> #include<algorithm> #include<cmath> #include<c ...
- UVALive - 3027 Corporative Network (并查集)
这题比较简单,注意路径压缩即可. AC代码 //#define LOCAL #include <stdio.h> #include <algorithm> using name ...
- UVA 3027 Corporative Network 带权并查集、
题意:一个企业要去收购一些公司把,使的每个企业之间互联,刚开始每个公司互相独立 给出n个公司,两种操作 E I:询问I到I它连接点最后一个公司的距离 I I J:将I公司指向J公司,也就是J公司是I公 ...
- Corporative Network_并查集
Description A very big corporation is developing its corporative network. In the beginning each of t ...
- POJ 2236 Wireless Network (并查集)
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 18066 Accepted: 761 ...
- UVA 11987 - Almost Union-Find(并查集)
UVA 11987 - Almost Union-Find 题目链接 题意:给定一些集合,操作1是合并集合,操作2是把集合中一个元素移动到还有一个集合,操作3输出集合的个数和总和 思路:并查集,关键在 ...
随机推荐
- Ubuntu16.04添加源的地址
打开terminal,输入sudo gedit /etc/apt/sources.list,向该文件中添加源的地址即可,如,可添加如下地址 deb-src http://archive.ubuntu. ...
- 去哪网实习总结:如何配置数据库连接(JavaWeb)
本来是以做数据挖掘的目的进去哪网的,结构却成了系统开发.. . 只是还是比較认真的做了三个月,老师非常认同我的工作态度和成果. .. 实习立即就要结束了.总结一下几点之前没有注意过的变成习惯和问题,分 ...
- 使用Handler在子线程中更新UI
Android规定仅仅能在主线程中更新UI.假设在子线程中更新UI 的话会提演示样例如以下错误:Only the original thread that created a view hierach ...
- IOC与DI区别
(1)IOC:控制反转,把对象创建交给spring进行配置. (2)DI:依赖注入,向类里面的属性中设置值. (3)关系:依赖注入不能单独存在,需要在IOC的基础之上完成操作.
- 使用CNN做电影评论的负面检测——本质上感觉和ngram或者LSTM同,因为CNN里图像检测卷积一般是3x3,而文本分类的话是直接是一维的3、4、5
代码如下: from __future__ import division, print_function, absolute_import import tensorflow as tf impor ...
- webform 下使用autofac
官网介绍: http://docs.autofac.org/en/latest/integration/webforms.html#quick-start HTTP 错误 500.22 - Inter ...
- Spark任务调度
不多说,直接上干货! Spark任务调度 DAGScheduler 构建Stage—碰到shuffle就split 记录哪个RDD 或者Stage 输出被物化 重新提交shuffle 输出丢失的sta ...
- HD-ACM算法专攻系列(23)——Crixalis's Equipment
题目描述: AC源码:此次考察贪心算法,解题思路:贪心的原则是使留下的空间最大,优先选择Bi与Ai差值最大的,至于为什么?这里用只有2个设备为例,(A1,B1)与(A2,B2),假设先搬运A1,搬运的 ...
- 弹出ifame页面(jquery.reveal.js)
<body> <a data-reveal-id="myModalDailyModify" data-animation="fade" tit ...
- java与javascript对cookie操作的工具类
Java对cookie的操作 package cn.utils; import java.util.HashMap; import java.util.Map; import javax.servle ...