UVa 1329 - Corporative Network Union Find题解
UVa的题目好多,本题是数据结构的运用,就是Union Find并查集的运用。主要使用路径压缩。甚至不须要合并树了,由于没有反复的连线和改动单亲节点的操作。
郁闷的就是不太熟悉这个Oj系统,竟然使用库中的abs就会WA,自己写了个abs小函数就过了。
题目:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4075
#include <stdio.h> const int MAX_N = 20005;
const int MOD = 1000;
//在UVa使用库的abs竟然WA,浪费好多时间
inline int abs(int a) { return a < 0? -a : a; } struct Subset
{
int p, w;
}; Subset subs[MAX_N]; int findParent(int x)
{
if (subs[x].p != x)
{
int p = subs[x].p;
subs[x].p = findParent(subs[x].p);
subs[x].w = (subs[x].w + subs[p].w);
}
return subs[x].p;
} void initSubs(int N)
{
for (int i = 1; i <= N; i++)
{
subs[i].p = i;
subs[i].w = 0;
}
} int main()
{
int T, N, i, j;
char cmd;
scanf("%d", &T);
while (T--)
{
scanf("%d", &N);
getchar();
initSubs(N);
while ((cmd = getchar()) && cmd != 'O')
{
if (cmd == 'E')
{
scanf("%d", &i);
subs[i].p = findParent(i);
printf("%d\n", subs[i].w);
}
else
{
scanf("%d %d", &i, &j);
subs[i].w = (abs(j - i))%MOD;
subs[i].p = j;//不存在反复连线和更改parent,故此直接连就ok
}
getchar();
}
}
return 0;
}
UVa 1329 - Corporative Network Union Find题解的更多相关文章
- UVA 1329 Corporative Network【并查集】
题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...
- UVA 1329 - Corporative Network
带权值的并查集的应用: 代码: #include<cstdio> #include<algorithm> #include<cmath> #include<c ...
- UVA 3027 Corporative Network 带权并查集、
题意:一个企业要去收购一些公司把,使的每个企业之间互联,刚开始每个公司互相独立 给出n个公司,两种操作 E I:询问I到I它连接点最后一个公司的距离 I I J:将I公司指向J公司,也就是J公司是I公 ...
- [LA] 3027 - Corporative Network [并查集]
A very big corporation is developing its corporative network. In the beginning each of the N enterpr ...
- UVALive 3027 Corporative Network 带权并查集
Corporative Network A very big corporation is developing its corporative networ ...
- 【35.86%】【POJ 1962】Corporative Network
Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 3943 Accepted: 1414 Description A very bi ...
- UVALive 3027 Corporative Network
---恢复内容开始--- Corporative Network Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld ...
- 【暑假】[实用数据结构]UVAlive 3027 Corporative Network
UVAlive 3027 Corporative Network 题目: Corporative Network Time Limit: 3000MS Memory Limit: 30000K ...
- 3027 - Corporative Network(并差集)
3027 - Corporative Network A very big corporation is developing its corporative network. In the begi ...
随机推荐
- 双系统卸载linux和装双系统的方法
卸载linux系统: 因为本人装的是windows和Ubuntu,所以引导程序在linux系统里,linux系统可以引导windows系统,而Windows不能引导linux,所以需要修改引导程序,使 ...
- PHP安全编程:register_globals的安全性
如果你还能记起早期Web应用开发中使用C开发CGI程序的话,一定会对繁琐的表单处理深有体会.当PHP的register_globals配置选项打开时,复杂的原始表单处理不复存在,公用变量会自动建立.它 ...
- s12-day04-work01 简单计算器功能实现
代码: #!/usr/local/env python3 ''' Author:@南非波波 Blog:http://www.cnblogs.com/songqingbo/ E-mail:qingbo. ...
- 关于node的path模块
const path=require('path'); //basename('绝对路径','截取的后缀') 返回文件的扩展名 let a=path.basename('C:\\temp\\myfil ...
- 双缓冲解决控制台应用程序输出“闪屏”(C/C++,Windows)
使用 C 语言编写游戏的小伙伴们想必起初都要遇到这样的问题,在不断清屏输出数据的过程中,控制台中的输出内容会不断地闪屏.出现这个问题的原因是程序对数据处理花掉的时间影响到了数据显示,或许你可以使用局部 ...
- 9 行 javascript 代码获取 QQ 群成员
昨天看到一条微博:「22 行 JavaScript 代码实现 QQ 群成员提取器」. 本着好奇心点击进去,发现没有达到效果,一是 QQ 版本升级了,二是博客里面的代码也有些繁琐. 于是自己试着写了一个 ...
- XML和HTML的区别
1.很多新手程序员总是会问HTML和XML有什么区别,接下来就解释一下: 什么是XML? XML 指可扩展标记语言(EXtensible Markup Language). XML 是一种很像HTML ...
- 长沙理工大学第十二届ACM大赛-重现赛 K - 大家一起来数二叉树吧
题目描述 某一天,Zzq正在上数据结构课.老师在讲台上面讲着二叉树,zzq在下面发着呆. 突然zzq想到一个问题:对于一个n个节点,m个叶子的二叉树,有多少种形态呐?你能告诉他吗? 对于第一组样例的解 ...
- svn代码同步脚本
碰到一个需求,主要是2个项目需要用到同一份代码,主要是域名和配置信息不一样,而且要把svn更新的代码同步过去.本来考虑提交时用钩子同步过去,但考虑到同步过去的代码还需要测试,而且另一边代码的时效性不强 ...
- aiohttp
发起请求 async def fetch(): async with aiohttp.ClientSession() as session: async with session.get('https ...