BZOJ4690 Never Wait for Weights(并查集)
带权并查集按秩合并即可维护。
#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(并查集)的更多相关文章
- 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; 在合并两个集 ...
- 【BZOJ-4690】Never Wait For Weights 带权并查集
4690: Never Wait for Weights Time Limit: 15 Sec Memory Limit: 256 MBSubmit: 88 Solved: 41[Submit][ ...
- UVALive 6187 Never Wait for Weights 带权并查集
题意:每次给出每两个数之间的大小差值.在给出关系的过程中插入询问:数a和数b的差值,若不能确定,输出UNKNOWN 解法:相对大小关系的处理:并查集 1.给出两点的相对大小关系后,找到两个点的根节点, ...
- AtCoder NIKKEI Programming Contest 2019 E. Weights on Vertices and Edges (并查集)
题目链接:https://atcoder.jp/contests/nikkei2019-qual/tasks/nikkei2019_qual_e 题意:给出一个 n 个点 m 条边的无向图,每个点和每 ...
- 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 ...
- 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 ...
- 1053 Path of Equal Weight (30分)(并查集)
Given a non-empty tree with root R, and with weight Wi assigned to each tree node Ti. The weig ...
- LeetCode:并查集
并查集 这部分主要是学习了 labuladong 公众号中对于并查集的讲解,文章链接如下: Union-Find 并查集算法详解 Union-Find 算法怎么应用? 概述 并查集用于解决图论中「动态 ...
- BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]
4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
随机推荐
- phpstudy apache启动失败,80端口占用问题解决方案
安装phpstydy,启动apache时,启动失败,提示80端口占用,需要将占用80端口的服务进程关闭 1.运行cmd, netstat -ano 找到80端口对应的pid 4 2.一般都是调用 h ...
- Ubunut18.04与Windows传输文件的方式
ubunut18.04与Windows传输文件的方式 开发环境:ubuntu18.04; 虚拟机:virtual box; 操作系统:Win10_64bits/专业版 在以前使用的ubuntu12.0 ...
- python中string,time,datetime三者之间的转化
这里time特指import time中的对象,datetime 特指from datetime import datetime中的对象,string指python自带的字符数据类型. 从使用的情况来 ...
- ts包、表、子表、section的关系
我们经常接触到创建 DEMUX,注册 Filter 过滤数据, 通过回调过滤出 section 数据,然后我们对 section 数据做具体的解析或者其他操作. 我们这里说的 section 就是段的 ...
- Infinite Maze CodeForces - 196B
We've got a rectangular n × m-cell maze. Each cell is either passable, or is a wall (impassable). A ...
- 002---Python基本数据类型--字符串
字符串 .caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { border: 1p ...
- CentOS(Linux)安装KETTLE教程 并配置执行定时任务
1,首先是安装jdk,并设置环境变量 采用yum安装可不设置环境变量 2,下载kettle https://sourceforge.net/projects/pentaho/files/Data%20 ...
- go学习笔记-语言基础
语言基础 结构 基础组成: 包声明 引入包 函数 变量 语句 & 表达式 注释 程序 在开始编写应用之前,我们先从最基本的程序开始,在学习大部分语言之前,都会编写一个可以输出hello wor ...
- 20145202马超《JAVA》预备作业1
20145202马超<JAVA>预备作业1 你觉得自己专业吗?对专业的期望是什么? 我觉得自己很不专业,我对专业的期望:老师之前讲过德国的一个研究,学习分为5个档次,第三个档是能够自己发现 ...
- SQL Server中2008及以上 ldf 文件过大的解决方法
在SQL Server中经常遇到事务日志变大的情况,除了将数据库设置为“自动收缩”外,还可以使用下面的SQL命令进行快速清除数据库中的事务日志,命令如下: - 第一步:清空日志 ALTER DAT ...