题目链接:

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【并查集】的更多相关文章

  1. [LA] 3027 - Corporative Network [并查集]

    A very big corporation is developing its corporative network. In the beginning each of the N enterpr ...

  2. LA 3027 Corporative Network 并查集记录点到根的距离

    Corporative Network Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [S ...

  3. UVa 1329 - Corporative Network Union Find题解

    UVa的题目好多,本题是数据结构的运用,就是Union Find并查集的运用.主要使用路径压缩.甚至不须要合并树了,由于没有反复的连线和改动单亲节点的操作. 郁闷的就是不太熟悉这个Oj系统,竟然使用库 ...

  4. UVA 1329 - Corporative Network

    带权值的并查集的应用: 代码: #include<cstdio> #include<algorithm> #include<cmath> #include<c ...

  5. UVALive - 3027 Corporative Network (并查集)

    这题比较简单,注意路径压缩即可. AC代码 //#define LOCAL #include <stdio.h> #include <algorithm> using name ...

  6. UVA 3027 Corporative Network 带权并查集、

    题意:一个企业要去收购一些公司把,使的每个企业之间互联,刚开始每个公司互相独立 给出n个公司,两种操作 E I:询问I到I它连接点最后一个公司的距离 I I J:将I公司指向J公司,也就是J公司是I公 ...

  7. Corporative Network_并查集

    Description A very big corporation is developing its corporative network. In the beginning each of t ...

  8. POJ 2236 Wireless Network (并查集)

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 18066   Accepted: 761 ...

  9. UVA 11987 - Almost Union-Find(并查集)

    UVA 11987 - Almost Union-Find 题目链接 题意:给定一些集合,操作1是合并集合,操作2是把集合中一个元素移动到还有一个集合,操作3输出集合的个数和总和 思路:并查集,关键在 ...

随机推荐

  1. SSAO + FXAA

    如今已经完毕了渲染器的屏幕空间环境光遮挡(SSAO)算法和FXAA高速反走样算法,等有时间就把当中的相关原理和当中遇到的问题进行总结发表.

  2. Flex AsDoc 完整版

    Flex 生成AsDoc用的是SDK自带的asdoc.exe工具 生成AsDoc文档的方式有两种:ant或者FlashBuilder 外部配置工具 方法一:外部配置工具 新增一个外部配置工具.过程例如 ...

  3. OpenSSL简单介绍及在Windows、Linux、Mac系统上的编译步骤

    OpenSSL介绍:OpenSSL是一个强大的安全套接字层password库,囊括基本的password算法.经常使用的密钥和证书封装管理功能及SSL协议.并提供丰富的应用程序供測试或其他目的使用. ...

  4. hdoj--3440--House Man(差分约束)

    House Man Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  5. docker初安装的血泪史

    最近docker很火,不管是朋友圈内还是公司内聊天都离不开docker,于是对docker产生了极大的好奇心,凭着一颗程序猿的好奇心开始了docker的安装血泪史. 首先我有一台从公司退役的本本x22 ...

  6. gdal集成kml库的做法

    作者:朱金灿 来源:http://blog.csdn.net/clever101 最近要读取kml文件,具体就是把kml文件当作一个矢量文件来读取.我发现gdal是支持集成kml库的.不过集成这个km ...

  7. python爬虫:爬取医药数据库drugbank

    这个是帮朋友做的,难点就是他们有一个反爬虫机制,用request一直不行,后面我就用selenium直接把网页copy下来,然后再来解析本地的html文件,就木有问题啦. 现在看来,写得有点傻,多包涵 ...

  8. CREATE TABLE 语句后的 ON [PRIMARY] 起什么作用

    CREATE   TABLE   [dbo].[table1]   ( [gh]   [char]   (10)   COLLATE   Chinese_PRC_CI_AS   NOT   NULL ...

  9. spring mvc 项目 相关配置文件小结

    web.xml文件主要配置如下: 需要加载的配置文件: 类路径下,可以使用通配符配置  类似:classpath:conf/spring/*/*.xml, <context-param> ...

  10. ZOJ 3019 Puzzle

    解题思路:给出两个数列an,bn,其中an,bn中元素的顺序可以任意改变,求an,bn的LCS 因为数列中的元素可以按任意顺序排列,所以只需要求出an,bn中的元素有多少个是相同的即可. 反思:一开始 ...