只有一个地方需要注意: 设节点a的根为u,b的跟为v,则:a = u + d[a];  b = v + d[b]; 已知:b-a=w.所以v - u = d[a] - d[b] + w; 在合并两个集合修改根节点时,把v的根改为u,同时v到根的距离为d[a] - d[b] + w; #include <cstdio> #include <cstring> ; int pa[MAXN]; long long int d[MAXN]; int findset( int x ) { if…
4690: Never Wait for Weights Time Limit: 15 Sec  Memory Limit: 256 MBSubmit: 88  Solved: 41[Submit][Status][Discuss] Description 在实验室中,Nathan Wada作为助手的职责是测定两个样品的重量差异.当样品的差异很小时,使用天平能比使用弹簧秤得到更精确的结果,所以他只使用天平来测得一些样品的重量差.他偶尔会被询问一些样品的重量差,而他能否回答这些问题取决于在回答相应…
带权并查集按秩合并即可维护. #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…
题意:每次给出每两个数之间的大小差值.在给出关系的过程中插入询问:数a和数b的差值,若不能确定,输出UNKNOWN 解法:相对大小关系的处理:并查集 1.给出两点的相对大小关系后,找到两个点的根节点,并利用路径压缩,将两个点父亲指向根节点.然后将根节点进行合并,并给出根节点之间的相对大小关系 2.询问时,同时找到该点到根节点的距离,相减即可得到相对大小. //meek #include <iostream> #include <cstdio> #include <cmath&…
题目链接:https://atcoder.jp/contests/nikkei2019-qual/tasks/nikkei2019_qual_e 题意:给出一个 n 个点 m 条边的无向图,每个点和每条边都有权值,让你删除一些边,使得最终的图中满足条件:一条边存在当且仅当包含该边的连通块的点权值和大于等于该边权值,问最少要移走多少条边. 题解:删边不好做,考虑加边,对于每条边,判断加入是否合法.按边权从小到大排序进行加边,要加入一条边之前,若之前两点不连通,则合并起来变成一个连通块,可以用并查集…
A. Arpa’s hard exam and Mehrdad’s naive cheat time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output There exists an island called Arpa’s land, some beautiful girls live there, as ugly ones do. Me…
A. Bear and Big Brother time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Bear Limak wants to become the largest of bears, or at least to become larger than his brother Bob. Right now, Limak…
Given a non-empty tree with root R, and with weight W​i​​ assigned to each tree node T​i​​. The weight of a path from R to L is defined to be the sum of the weights of all the nodes along the path from R to any leaf node L. Now given any weighted tre…
并查集 这部分主要是学习了 labuladong 公众号中对于并查集的讲解,文章链接如下: Union-Find 并查集算法详解 Union-Find 算法怎么应用? 概述 并查集用于解决图论中「动态连通性」问题: 主要实现以下几个API class UF { /* 将 p 和 q 连接 */ public void union(int p, int q); /* 判断 p 和 q 是否连通 */ public boolean connected(int p, int q); /* 返回图中有多…
4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品酒家”和“首席猎手”两个奖项,吸引了众多品酒师参加. 在大会的晚餐上,调酒师 Rainbow 调制了 nn 杯鸡尾酒.这 nn 杯鸡尾酒排成一行,其中第 ii 杯酒 (1≤i≤n1≤i≤n) 被贴上了一个标签 sisi,每个标签都是 2626 个小写英文字母之一.设 Str(l,r)Str(l,r)…