题意:每次给出每两个数之间的大小差值。在给出关系的过程中插入询问:数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 带权并查集的更多相关文章

  1. 【BZOJ-4690】Never Wait For Weights 带权并查集

    4690: Never Wait for Weights Time Limit: 15 Sec  Memory Limit: 256 MBSubmit: 88  Solved: 41[Submit][ ...

  2. UVALive 3027 Corporative Network 带权并查集

                         Corporative Network A very big corporation is developing its corporative networ ...

  3. POJ 1703 Find them, Catch them(带权并查集)

    传送门 Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42463   Accep ...

  4. [NOIP摸你赛]Hzwer的陨石(带权并查集)

    题目描述: 经过不懈的努力,Hzwer召唤了很多陨石.已知Hzwer的地图上共有n个区域,且一开始的时候第i个陨石掉在了第i个区域.有电力喷射背包的ndsf很自豪,他认为搬陨石很容易,所以他将一些区域 ...

  5. poj1417 带权并查集 + 背包 + 记录路径

    True Liars Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2713   Accepted: 868 Descrip ...

  6. poj1984 带权并查集(向量处理)

    Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 5939   Accepted: 2 ...

  7. hdu3038(带权并查集)

    题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=3038 题意: n表示有一个长度为n的数组, 接下来有m行形如x, y, d的输入, 表示 ...

  8. 洛谷OJ P1196 银河英雄传说(带权并查集)

    题目描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦 创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩星域爆发战争.泰山 ...

  9. poj1984 带权并查集

    题意:有多个点,在平面上位于坐标点上,给出一些关系,表示某个点在某个点的正东/西/南/北方向多少距离,然后给出一系列询问,表示在第几个关系给出后询问某两点的曼哈顿距离,或者未知则输出-1. 只要在元素 ...

随机推荐

  1. Oracle用户密码过期问题解决

    一.用户密码即将过期,导致autotrace无法打开           如果用户密码即将过期,在登录数据库时会收到如下提示:           ERROR:            ORA-2800 ...

  2. Tutorial: Importing and analyzing data from a Web Page using Power BI Desktop

    In this tutorial, you will learn how to import a table of data from a Web page and create a report t ...

  3. Golang的iota的特性

    Golang的iota的特性: 1. iota在每个ConstBlock中自动归0. 2. iota在每个ConstSpec后自动增1. 换言之: iota是ConstBlock中ConstSpec的 ...

  4. [转]JSON与XML的区别比较

    1.定义介绍 (1).XML定义扩展标记语言 (Extensible Markup Language, XML) ,用于标记电子文件使其具有结构性的标记语言,可以用来标记数据.定义数据类型,是一种允许 ...

  5. Python实现NN(神经网络)

    Python实现NN(神经网络) 参考自Github开源代码:https://github.com/dennybritz/nn-from-scratch 运行环境 Pyhton3 numpy(科学计算 ...

  6. C中的一些函数

    简述:printf.sprintf函数 转载自http://www.cnblogs.com/adslg/archive/2008/08/22/1274164.html 部分进行了修改,参考http:/ ...

  7. 【每日scrum】NO.1

    今天我们小组正式开始了冲刺,问题还是不少的,有必要记录下来来指导和改进后续的工作: 1.负责去找地图,却发现地图不能很好的适应我们的软件,略心塞>>> 2.本来计划把迪杰斯特拉算法看 ...

  8. 校园导游之NABC个人分析

    校园导游之NABC个人分析 Need: 为不熟悉校园环境的人们(如新生,来咱们学校参观滴)提供便利. Approach: 了解Andriod应用开发:导航功能之外还可以对学校进行宣传,比如拍一些学校的 ...

  9. 多种方法实现H5网页图片动画效果;

    在web开发中,GIF动画效果是随处可见,比如常见的loading加载.人物奔跑的gif图片等等,那么这些都是怎么实现的呢?其实实现的原理很简单,简而言之,这些所谓的动画都是一帧一帧的图片经过一段时间 ...

  10. 【LRU Cache】cpp

    题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...