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

#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. Percona XtraDB Cluster 5.7安装配置

    优点:1.准同步复制2.多个可同时读写节点,可实现写扩展,较分片方案更进一步3.自动节点管理4.数据严格一致5.服务高可用缺点:1.只支持innodb引擎2.所有表都要有主键3.所有的写操作都将发生在 ...

  2. Spring Boot2.4双数据源的配置

    相较于单数据源,双数据源配置有时候在数据分库的时候可能更加有利 但是在参考诸多博客以及书籍(汪云飞的实战书)的时候,发现对于spring boot1.X是完全没问题的,一旦切换到spring boot ...

  3. Mysql错误积累001-load data导入文件数据出现1290错误

    错误出现情景 在cmd中使用mysql命令,学生信息表添加数据.使用load data方式简单批量导入数据. 准备好文本数据: xueshengxinxi.txt 文件  数据之间以tab键进行分割 ...

  4. responsive grid

    http://csswizardry.com/csswizardry-grids/ http://unsemantic.com/demo-responsive http://getbootstrap. ...

  5. 如何从“点子”落地到“执行”?—完整解析1个手游传播类mini项目的进化

    本文来自网易云社区 作者:林玮园 从点子到落地,是不确定到确定的过程,是从模糊概念到具体现实的实现过程.无论什么点子,在落地变现的过程中都会有很多疑问产生. 首先,不确定点子本身是否成立.点子的背后是 ...

  6. 获取.jar文件运行时所处的路径

    在Windows控制台中运行.jar文件时的两种环境: (1)控制台当前所在目录是.jar文件所在的目录 (2)控制台当前所在目录不是.jar文件所在的目录 我的期望: 我希望在上述两种环境下均可以得 ...

  7. Android中StackOverflow的问题

    最近出现了一个让人抓狂的问题. 现在的项目中,制作了一个界面非常复杂.Fragment中嵌套下拉刷新的Listview 这样一个布局,在3.0以上的手机上都表现良好问题!但是在2.x的比较弱爆的手机上 ...

  8. python 网络编程(远程执行命令与粘包)

    远程执行命令 先来学习一个新模块 , 一会用到的.. 新模块: subprocess 执行系统命令 r = subprocess.Popen('ls',shell=True,stdout=subpro ...

  9. 3.爬虫 urlib库讲解 总结

    urllib库的总结: 用ProcessOn(安利这个软件,够用了)根据前面的几节内容做了个思维导图. urllib库一共有四个模块: request:它是最基本的模块,可以用来模拟发送请求 erro ...

  10. LeetCode - 3. Longest Substring Without Repeating Characters(388ms)

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...