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 ...
随机推荐
- C/C++面试题目一
C/C++开发工程师面试题目(一)(附答案分析) 推荐:自己根据在面试中碰到做过的一些题目以及总结的题目,希望对面试的同学有所帮助. 一. 选择题 1. 下列类中( )不是输入输出流类iostrea ...
- Linux命令之dig命令挖出DNS的秘密
=== [初次见面] 我相信使用nslookup的同学一定比使用dig的同学多,所以还是有必要花些时间给大家介绍一下dig的. dig,和nslookup作用有些类似,都是DNS查询工具. dig,其 ...
- Java 单例模式的七种写法
Java 单例模式的七种写法 第一种(懒汉,线程不安全) public class Singleton { private static Singleton instance; private Sin ...
- 基于原生JS的jsonp方法的实现
基于原生JS的jsonp方法的实现 jsonp,相信大家并不陌生,是在js异步请求中解决跨域的方法之一,原理很简单,有不清楚的同学可以google下,这里就补详细解释了.在Jquery库中,jQuer ...
- Android签名打包详解
一. Android签名有什么作用? 应用程序升级:如果你希望用户无缝升级到新的版本,那么你必须用同一个证书进行签名.这是由于只有以同一个证书签名,系统才会允许安装升级的应用程序.如果你采用 ...
- HDU 6215 Brute Force Sorting(模拟链表 思维)
Brute Force Sorting Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Othe ...
- HDU 6201 transaction transaction transaction(树形DP)
transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 132768/1 ...
- Java 8中你可能没听过的10个新特性
lambda表达式,lambda表达式,还是lambda表达式.一提到Java 8就只能听到这个,但这不过是其中的一个新功能而已,Java 8还有许多新的特性——有一些功能强大的新类或者新的用法,还有 ...
- python 写一个贪吃蛇游戏
#!usr/bin/python #-*- coding:utf-8 -*- import random import curses s = curses.initscr() curses.curs_ ...
- Selenium快速入门(下)
Frame相关 import time from selenium import webdriver from selenium.common.exceptions import NoSuchElem ...