带权并查集按秩合并即可维护。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')) c=getchar();return c;}
int gcd(int n,int m){return m==?n:gcd(m,n%m);}
int read()
{
int x=,f=;char c=getchar();
while (c<''||c>'') {if (c=='-') f=-;c=getchar();}
while (c>=''&&c<='') x=(x<<)+(x<<)+(c^),c=getchar();
return x*f;
}
#define N 100010
int n,m,fa[N],deep[N],weight[N],len[N];
int find(int x)
{
if (fa[x]==x) return x;
int p=find(fa[x]);
weight[x]=weight[fa[x]]+len[x];
return p;
}
void merge(int x,int y,int z)
{
int p=find(x),q=find(y);
if (p!=q)
{
if (deep[p]<deep[q]) swap(p,q),swap(x,y),z=-z;
fa[q]=p;if (deep[p]==deep[q]) deep[p]++;
weight[q]=len[q]=weight[x]-weight[y]-z;
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("bzoj4690.in","r",stdin);
freopen("bzoj4690.out","w",stdout);
const char LL[]="%I64d\n";
#else
const char LL[]="%lld\n";
#endif
n=read(),m=read();
while (n)
{
for (int i=;i<=n;i++) fa[i]=i,deep[i]=,weight[i]=len[i]=;
while (m--)
{
char c=getchar();
while (c!='!'&&c!='?') c=getchar();
if (c=='!')
{
int x=read(),y=read(),z=read();
merge(x,y,z);
}
else
{
int x=read(),y=read();
if (find(x)!=find(y)) printf("UNKNOWN\n");
else printf("%d\n",weight[x]-weight[y]);
}
}
n=read(),m=read();
}
return ;
}

BZOJ4690 Never Wait for Weights(并查集)的更多相关文章

  1. LA 6187 - Never Wait for Weights 并查集的带权路径压缩

    只有一个地方需要注意: 设节点a的根为u,b的跟为v,则:a = u + d[a];  b = v + d[b]; 已知:b-a=w.所以v - u = d[a] - d[b] + w; 在合并两个集 ...

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

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

  3. UVALive 6187 Never Wait for Weights 带权并查集

    题意:每次给出每两个数之间的大小差值.在给出关系的过程中插入询问:数a和数b的差值,若不能确定,输出UNKNOWN 解法:相对大小关系的处理:并查集 1.给出两点的相对大小关系后,找到两个点的根节点, ...

  4. AtCoder NIKKEI Programming Contest 2019 E. Weights on Vertices and Edges (并查集)

    题目链接:https://atcoder.jp/contests/nikkei2019-qual/tasks/nikkei2019_qual_e 题意:给出一个 n 个点 m 条边的无向图,每个点和每 ...

  5. Codeforces Round #383 (Div. 2) A,B,C,D 循环节,标记,暴力,并查集+分组背包

    A. Arpa’s hard exam and Mehrdad’s naive cheat time limit per test 1 second memory limit per test 256 ...

  6. Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1)A B C 水 并查集 思路

    A. Bear and Big Brother time limit per test 1 second memory limit per test 256 megabytes input stand ...

  7. 1053 Path of Equal Weight (30分)(并查集)

    Given a non-empty tree with root R, and with weight W​i​​ assigned to each tree node T​i​​. The weig ...

  8. LeetCode:并查集

    并查集 这部分主要是学习了 labuladong 公众号中对于并查集的讲解,文章链接如下: Union-Find 并查集算法详解 Union-Find 算法怎么应用? 概述 并查集用于解决图论中「动态 ...

  9. BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]

    4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...

随机推荐

  1. 因为之前完全没有接触过Spring,所以准备先把spring实战看完再落实项目

    因为之前完全没有接触过Spring,所以准备先把spring实战看完再落实项目

  2. python元组操作

    元组:(tuple)元素不可被修改,不能被增加或者删除 一般写元组的时候,建议在最后加上一个逗号 可以索引取值    可以切片取值 元组一级元素不可被修改,但是二级及以后可以被修改 count() 获 ...

  3. Python日常运维脚本

    1    扫描Windows系统CPU.内存.磁盘利用率 1.1    实现代码 #!usr/bin/env python #-*- coding:utf-8 _*- ""&quo ...

  4. 【poe设备加电配置】

    开启接口的poe功能: [interface_name]: 配置poe端口的最大功率: [interface_name[: 配置poe的端口工作模式: [interface_name[: 配置poe端 ...

  5. React学习(2)—— 组件的运用和数据传递

    React官方中文文档地址:    https://doc.react-china.org/ 了解了组件之后,就需要理解“Props”和“State”的用法.首先来介绍State,State按照字面意 ...

  6. MySQL版本详解

    一.版本说明 1.1.MySQL相关连接 MySQL官网:https://www.mysql.com/ MySQL下载:https://dev.mysql.com/downloads/mirrors/ ...

  7. Vue+webpack构建一个项目

    1.安装CLI命令的工具  推荐用淘宝的镜像 npm install -g @vue/cli @vue/cli-init 2.使用命令构建一个名为myapp的项目 vue init webpack m ...

  8. Spark-源码-Spark-StartAll Master Worler启动流程

    Spark start-all>> """Master启动流程""" Master类 class Master( host: S ...

  9. Plsql developer 怎么在打开时登陆配置oracel client?

    配置前 logon 这块是空白的,该怎么配置呢? 看下面 --> 安装完plsql 后 需要安装 oracle client, 这里不再赘述,请自行百度.下面将贴出如何使用 oracle cli ...

  10. Android log 里面快速搜索错误堆栈 ( 关键字)

    有时候,别人给你的log 文件,是一个文件夹,里面放了很多文件.但是可能你需要的log 只有几行.这时候不可能手工搜索的. 那怎么办呢?使用FileLocationPro.下载地址: https:// ...