洛谷 P4234 最小差值生成树(LCT)
题面
题解
LCT
考虑先按边权排序,从小到大加边
如果构成一颗树了,就更新答案
当加入一条边,会形成环.
贪心地想,我们要最大边权-最小边权最小
最大边权固定就是新加入的这条边,我们要让最小边权尽量地大
那么我们可以去掉原先路径上最小的那一条边,这样一定不会差
以上,可以用LCT维护
ps:LCT只有点权,所以对于每条边,新建一个节点
Code
#include<bits/stdc++.h>
#define mp make_pair
#define LL long long
#define RG register
const int inf = 2147483647;
using namespace std;
template<class T> inline void read(T &x) {
x = 0; RG char c = getchar(); bool f = 0;
while (c != '-' && (c < '0' || c > '9')) c = getchar(); if (c == '-') c = getchar(), f = 1;
while (c >= '0' && c <= '9') x = x*10+c-48, c = getchar();
x = f ? -x : x;
return ;
}
template<class T> inline void write(T x) {
if (!x) {putchar(48);return ;}
if (x < 0) x = -x, putchar('-');
int len = -1, z[20]; while (x > 0) z[++len] = x%10, x /= 10;
for (RG int i = len; i >= 0; i--) putchar(z[i]+48);return ;
}
const int N = 250000 + 10;
struct node {
int v, m, fa, ch[2], w;//m为最小值,w为位置
bool rev;
}t[N];
int S[N], top, val[N];
set<pair<int, int> > Min;
void putrev(int x) {
swap(t[x].ch[0], t[x].ch[1]);
t[x].rev ^= 1;
}
void pushup(int x) {
t[x].m = val[x], t[x].w = x;
if (t[x].m > t[t[x].ch[0]].m && t[x].ch[0]) t[x].m = t[t[x].ch[0]].m, t[x].w = t[t[x].ch[0]].w;
if (t[x].m > t[t[x].ch[1]].m && t[x].ch[1]) t[x].m = t[t[x].ch[1]].m, t[x].w = t[t[x].ch[1]].w;
}
#define get(x) (t[t[x].fa].ch[1] == x)
bool isroot(int x) {
return (t[t[x].fa].ch[0] != x) && (t[t[x].fa].ch[1] != x);
}
void pushdown(int x) {
if (t[x].rev) {
t[x].rev = 0;
if (t[x].ch[0]) putrev(t[x].ch[0]);
if (t[x].ch[1]) putrev(t[x].ch[1]);
}
}
void rotate(int x) {
int k = get(x), y = t[x].fa, z = t[y].fa;
if (!isroot(y)) t[z].ch[get(y)] = x;
t[x].fa = z;
t[t[x].ch[k^1]].fa = y; t[y].ch[k] = t[x].ch[k^1];
t[y].fa = x; t[x].ch[k^1] = y;
pushup(y);
}
void splay(int x) {
S[top = 1] = x;
for (RG int i = x; !isroot(i); i = t[i].fa) S[++top] = t[i].fa;
for (RG int i = top; i; i--) pushdown(S[i]);
while (!isroot(x)) {
int y = t[x].fa;
if (!isroot(y))
(get(x) ^ get(y)) ? rotate(x) : rotate(y);
rotate(x);
}
pushup(x);
}
void access(int x) {for (int y = 0; x; y = x, x = t[x].fa)splay(x), t[x].ch[1] = y, pushup(x);}
void makeroot(int x) {access(x); splay(x); putrev(x);}
void link(int x, int y) {
makeroot(x);
t[x].fa = y;
}
void cut(int x, int y) {
makeroot(x);
access(y);
splay(y);
t[x].fa = t[y].ch[0] = 0; pushup(y);
}
void split(int x, int y) {makeroot(x); access(y); splay(y);}
struct Node {
int u, v, w;
bool operator <(Node z) const {
return w < z.w;
}
}p[N];
int fa[N];
int find(int x) {
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
int main() {
int n, m, cnt = 0, ans = inf;
read(n), read(m);
for (int i = 1; i <= m; i++)
read(p[i].u), read(p[i].v), read(p[i].w);
sort(p+1, p+1+m);
for (int i = 1; i <= n; i++) fa[i] = i, val[i] = inf;
for (int i = 1; i <= m; i++) {
int x = p[i].u, y = p[i].v;
if (x == y) continue;
val[i + n] = p[i].w;
if (find(x) != find(y)) {
cnt++; fa[find(y)] = find(x);
link(i + n, x); link(i + n, y);
Min.insert(mp(p[i].w, i));
} else {
split(x, y);
int wz = t[y].w;
cut(wz, p[wz - n].u); cut(wz, p[wz - n].v);
link(i + n, x); link(i + n, y);
Min.erase(mp(p[wz - n].w, wz - n));
Min.insert(mp(p[i].w, i));
}
if (cnt == n-1)
ans = min(ans, p[i].w - (Min.begin()->first));
}
printf("%d\n", ans);
return 0;
}
洛谷 P4234 最小差值生成树(LCT)的更多相关文章
- 洛谷P4234 最小差值生成树(LCT,生成树)
洛谷题目传送门 和魔法森林有点像,都是动态维护最小生成树(可参考一下Blog的LCT总结相关部分) 至于从小到大还是从大到小当然无所谓啦,我是从小到大排序,每次枚举边,还没连通就连,已连通就替换环上最 ...
- 洛谷.4234.最小差值生成树(LCT)
题目链接 先将边排序,这样就可以按从小到大的顺序维护生成树,枚举到一条未连通的边就连上,已连通则(用当前更大的)替换掉路径上最小的边,这样一定不会更差. 每次构成树时更新答案.答案就是当前边减去生成树 ...
- 洛谷P4234 最小差值生成树(lct动态维护最小生成树)
题目描述 给定一个标号为从 11 到 nn 的.有 mm 条边的无向图,求边权最大值与最小值的差值最小的生成树. 输入输出格式 输入格式: 第一行两个数 n, mn,m ,表示图的点和边的数量. ...
- 【刷题】洛谷 P4234 最小差值生成树
题目描述 给定一个标号为从 \(1\) 到 \(n\) 的.有 \(m\) 条边的无向图,求边权最大值与最小值的差值最小的生成树. 输入输出格式 输入格式: 第一行两个数 \(n, m\) ,表示图的 ...
- [洛谷P4234] 最小差值生成树
题目类型:\(LCT\)动态维护最小生成树 传送门:>Here< 题意:求一棵生成树,其最大边权减最小边权最小 解题思路 和魔法森林非常像.先对所有边进行排序,每次加边的时候删除环上的最小 ...
- 洛谷4234最小差值生成树 (LCT维护生成树)
这也是一道LCT维护生成树的题. 那么我们还是按照套路,先对边进行排序,然后顺次加入. 不过和别的题有所不同的是: 在本题中,我们需要保证LCT中正好有\(n-1\)条边的时候,才能更新\(ans\) ...
- P4234 最小差值生成树 LCT维护边权
\(\color{#0066ff}{ 题目描述 }\) 给定一个标号为从 \(1\) 到 \(n\) 的.有 \(m\) 条边的无向图,求边权最大值与最小值的差值最小的生成树. \(\color{#0 ...
- P4234 最小差值生成树
题目 P4234 最小差值生成树 做法 和这题解法差不多,稍微变了一点,还不懂就直接看代码吧 \(update(2019.2):\)还是具体说一下吧,排序,直接加入,到了成环情况下,显然我们要把此边代 ...
- 【Luogu】P4234最小差值生成树(LCT)
题目链接 能把LCT打得每个函数都恰有一个错误也是挺令我惊讶的. 本题使用LCT维护生成树,具体做法是对原图中的每个边建一个点,然后连边的时候相当于是将边的起点跟“边”这个点连起来,边的终点也跟它连起 ...
随机推荐
- STREAMING #5 题解 3.高位网络
高维网络 [题目描述] 现在有一个 d 维的坐标网格,其中第 i 维坐标的范围是[0,a_i].在这个范围内建立一个有向图:我们把范围内的每个整点(每一维坐标均为整数的点)当做图上的顶点.设点 A(0 ...
- [Jenkins] 执行SoapUI的task,设置邮件内容为HTML+CSS
设置邮件内容:Default Content <span style="font-family:verdana;font-size:16px;color:black;font-weig ...
- BCompare 4重置试用天数
BCompare安装后有30天试用期,试用结束后,你可以卸载重装,以重新获得30天试用天数. BCompare的使用天数记录保存在注册表中,如果不想每次重装,也可删除对应的注册表值来重置激活天数. 命 ...
- Java SimpleDateFormat用法
? Java中怎么才能把日期转换成想要的格式呢,或把字符串转换成一定格式的日期,如把数据库中的日期或时间转换成自己想要的格式,JAVA中提供了SimpleDateFormat类可以实现,以下是Si ...
- ConsoleAppender
http://logback.qos.ch/manual/appenders.html#ConsoleAppender <configuration> <appender name= ...
- g++中宏NULL究竟是什么?
NULL是个指针,还是个整数?0?或(void*)0?答案是和g++版本有关.g++ 4.6支持C++11,引入了nullptr,也许会发生变化. 可以写段简单代码求证一下: #include < ...
- iOS基础教程:在建好的项目中加入CoreData[转]
这几天在做一个ios的小项目,项目中需要对数据进行基本的增删改查操作.于是就想用一把CoreData.但在创建项目初期,没有包含进CoreData.于是就在已建好的项目中加入CoreData.由于第一 ...
- 一个简单的编译tex的Makefile
tex编译成pdf通常要经过以下步骤:tex-->dvi-->ps-->pdf.如果修改了tex文件想看一下效果,就要把命令重新敲一遍.虽然就几行命令,反复敲还是很烦人的.最直接的办 ...
- git 只merge一个commit的方法
https://git-scm.com/book/tr/v2/Git-Basics-Viewing-the-Commit-History gil log 来查看commit的记录 Other main ...
- XJOI 3601 技能(贪心+二分)
题目描述: 有一个oier,他有n个算法技能,每个技能有一个水平值,每个技能的水平上限都是A,设这个oier有cnt个技能达到了A, 设所有水平值的最小值为mi,那么这个oier的战斗力为cnt×Cf ...