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输出集合的个数和总和 思路:并查集,关键在 ...
随机推荐
- AOP经典2种配置演示样例
第一种: 使用aop指定切面aspect. <bean id="LogAdvice" class="com.thinkmore.framework.monitor. ...
- Linux--对文件夹下的配置文件批量改动IP
sed -i 's/10.1.1.1/10.1.1.2/g' `grep -ir 10.1.1.1 * |grep -E '.xml:|.cfg:|.ini:|.wsdl:|.properties:' ...
- 安卓系统底层C语言算法之測试參数是几个long型的算法
#include <stdio.h> #define BITS_PER_LONG (sizeof(unsigned long) * 8) //求一个数x是几个long的长度 #define ...
- webRequest
chrome.webRequest 描述: 使用 chrome.webRequest API 监控与分析流量,还可以实时地拦截.阻止或修改请求. 可用版本: 从 Chrome 17 开始支持. 权 ...
- Windows下Word.exe在哪?
在这里: C:\Program Files\Microsoft Office\root\Office16
- Android开发:ImageView阴影和图层效果
import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import ...
- 图片词典 Picture Dictionary
图片词典/可视词典 Picture Dictionary 某些 APP 又有新功能可以加入了.
- 多年js学习累计总结
http://www.codesec.net/list/6/ 大神http://www.cnblogs.com/tylerdonet/p/5543813.html
- 『转』How to Think About Your Career
开始工作的伊始,逐渐转载及阅读Medium上知名华裔设计师Julie Zhuo的文章,这是她在medium上的介绍:Product design VP @ Facebook. Lover of foo ...
- php基础------将二维数组转三维数组
将二维数组转为三维数组 /** * 二维数组转三维数组(指定键为三维数组的键名) * @param [type] $arr [要排序的数组] * @param [type] $key [指定的键] * ...