UVALive 6187 Never Wait for Weights 带权并查集
题意:每次给出每两个数之间的大小差值。在给出关系的过程中插入询问:数a和数b的差值,若不能确定,输出UNKNOWN
解法:相对大小关系的处理:并查集
1.给出两点的相对大小关系后,找到两个点的根节点,并利用路径压缩,将两个点父亲指向根节点。然后将根节点进行合并,并给出根节点之间的相对大小关系
2.询问时,同时找到该点到根节点的距离,相减即可得到相对大小。
//meek
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <sstream>
#include <vector>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
#define fi first
#define se second
#define MP make_pair
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//**************************************** const int N=+;
const ll inf = 1ll<<;
const int mod= ; char ch[];
int d[N],parent[N],flag,n;
ll ans;
void init () {
for(int i=;i<=n;i++) parent[i]=i;
mem(d);
}
int finds(int x){
if(x==parent[x])return x;
int fy=finds(parent[x]);
d[x] = d[parent[x]] + d[x];
return parent[x]=finds(parent[x]);
}
int main() {
int a,b,m,w;
while(scanf("%d%d",&n,&m)!=EOF) {
if( n== &&m==) break;
init();
for(int i=;i<=m;i++) {
scanf("%s",ch);
if(ch[]=='!') {
scanf("%d%d%d",&a,&b,&w);
int fx=finds(a);
int fy=finds(b);
if(fx!=fy) parent[fy]=fx,d[fy]=d[a]-w-d[b];
}
else {
scanf("%d%d",&a,&b);
int fx=finds(a);
int fy=finds(b);
if(fx==fy) {
cout<<d[a]-d[b]<<endl;
}
else {
printf("UNKNOWN\n");
}
}
}
}
return ;
}
代码
UVALive 6187 Never Wait for Weights 带权并查集的更多相关文章
- 【BZOJ-4690】Never Wait For Weights 带权并查集
4690: Never Wait for Weights Time Limit: 15 Sec Memory Limit: 256 MBSubmit: 88 Solved: 41[Submit][ ...
- UVALive 3027 Corporative Network 带权并查集
Corporative Network A very big corporation is developing its corporative networ ...
- POJ 1703 Find them, Catch them(带权并查集)
传送门 Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42463 Accep ...
- [NOIP摸你赛]Hzwer的陨石(带权并查集)
题目描述: 经过不懈的努力,Hzwer召唤了很多陨石.已知Hzwer的地图上共有n个区域,且一开始的时候第i个陨石掉在了第i个区域.有电力喷射背包的ndsf很自豪,他认为搬陨石很容易,所以他将一些区域 ...
- poj1417 带权并查集 + 背包 + 记录路径
True Liars Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2713 Accepted: 868 Descrip ...
- poj1984 带权并查集(向量处理)
Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 5939 Accepted: 2 ...
- hdu3038(带权并查集)
题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=3038 题意: n表示有一个长度为n的数组, 接下来有m行形如x, y, d的输入, 表示 ...
- 洛谷OJ P1196 银河英雄传说(带权并查集)
题目描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦 创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩星域爆发战争.泰山 ...
- poj1984 带权并查集
题意:有多个点,在平面上位于坐标点上,给出一些关系,表示某个点在某个点的正东/西/南/北方向多少距离,然后给出一系列询问,表示在第几个关系给出后询问某两点的曼哈顿距离,或者未知则输出-1. 只要在元素 ...
随机推荐
- OpenStack:初识
OpenStack提纲:-------------------------------------------初识OpenStack, 千头万绪, 不知所措. 逐渐剥茧抽丝, 厘清思路...一. Op ...
- golang与C交互:cgo
1. 在Go中引用C代码很简单, 在 import "C"前用注释引入标准的C代码, 然后使用C.xxx的伪包引用C代码空间的标识符即可. 需要注意, import"C& ...
- mysql存储过程执行权限问题
tags: mysql PROCEDURE 存储过程 definer SECURITY 权限 以下存储过程,限定了DEFINER为root,也就是root之外的账户是无法调用这个存储过程的. 1 2 ...
- iOS开发之Pch预编译文件的创建
在Xcode6之前,创建一个新工程xcode会在Supporting files文件夹下面自动创建一个“工程名-Prefix.pch”文件,也是一个头文件,pch头文件的内容能被项目中的其他所有源文件 ...
- UIView Programming Guide学习笔记
|View |Creating and Configuring View Objects |Creating and Managing a View Hierarchy |Adjusting the ...
- [shell基础]——if/for/while/until/case 语句
for语句 do echo $loop done ` do echo $loop done for loop in `ls /tmp` do echo $loop done while语句 while ...
- Querying mergeinfo requires version 3 of the FSFS filesystem schema
环境: jdk 1.7; svn 3.0.4; TortoiseSVN 1.7.13 Subversion 1.7.10; IntelliJ IDEA 13.1.1;win7 64位系统 之前那个 ...
- boostrap中lg,md,sm,xs
boostrap中lg,md,sm,xs分别表示多少px? .col-xs- 超小屏幕 手机 (<768px).col-sm- 小屏幕 平板 (≥768px).col-md- 中等屏幕 桌面显示 ...
- php调用微信发送自定义模版接口
function sendWechatmodel($openid,$data,$go_url)//接受消息的用户openid,发送的消息,点击详情跳转的url { ...
- C#委托详解(2):实现方式大全
本系列文章将详细探讨C#中的委托,列举其主要的实现方式,并分析其在设计层面和编码层面带来的好处,最后会讨论其安全性和执行效率等. 接上篇(C#委托详解(1):什么是委托)介绍完什么是委托之后,来看看C ...