luoguP2387 [NOI2014]魔法森林
https://www.luogu.org/problemnew/show/P2387
考虑先将所有边按 a 值排序,依次加入每一条边,如果这条边的两个端点 ( l, r ) 之间的简单路径中 b 的最大值大于这条边的 b 值,删掉最大的一条边,加入这条边,如果 l 和 r 不连通,则直接 link
需要注意的是将边权转换为点权,将第 i 条边变成编号为 i + n 的点,维护最大值的位置就可以了
这题还有自环( 坑了我半个小时 )
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
template <typename _T>
inline void read(_T &f) {
f = 0; _T fu = 1; char c = getchar();
while(c < '0' || c > '9') {if(c == '-') fu = -1; c = getchar();}
while(c >= '0' && c <= '9') {f = (f << 3) + (f << 1) + (c & 15); c = getchar();}
f *= fu;
}
const int N = 150000 + 10;
struct ele {
int u, v, a, b;
bool operator < (const ele A) const {return a < A.a;}
}p[N];
int fa[N], ch[N][2], maxn[N], val[N], rev[N], wz[N], st[N], f[N], n, m, len, ans = INT_MAX;
int isroot(int u) {return ch[fa[u]][0] != u && ch[fa[u]][1] != u;}
int get(int u) {return ch[fa[u]][1] == u;}
void update(int u) {
maxn[u] = val[u]; wz[u] = u;
if(maxn[ch[u][0]] > maxn[u] && ch[u][0]) maxn[u] = maxn[ch[u][0]], wz[u] = wz[ch[u][0]];
if(maxn[ch[u][1]] > maxn[u] && ch[u][1]) maxn[u] = maxn[ch[u][1]], wz[u] = wz[ch[u][1]];
}
void pushdown(int u) {
if(rev[u]) {
swap(ch[u][0], ch[u][1]);
rev[ch[u][0]] ^= 1;
rev[ch[u][1]] ^= 1;
rev[u] ^= 1;
}
}
void rotate(int u) {
int old = fa[u], oldd = fa[old], k = get(u);
if(!isroot(old)) ch[oldd][get(old)] = u; fa[u] = oldd;
ch[old][k] = ch[u][k ^ 1]; fa[ch[u][k ^ 1]] = old;
fa[old] = u; ch[u][k ^ 1] = old;
update(old); update(u);
}
void splay(int u) {
st[len = 1] = u;
for(int i = u; !isroot(i); i = fa[i]) st[++len] = fa[i];
for(int i = len; i >= 1; i--) pushdown(st[i]);
for(; !isroot(u); rotate(u)) if(!isroot(fa[u])) rotate(get(u) == get(fa[u]) ? fa[u] : u);
}
void access(int u) {
for(int i = 0; u; i = u, u = fa[u]) {
splay(u);
ch[u][1] = i;
update(u);
}
}
void makeroot(int u) {
access(u);
splay(u);
rev[u] ^= 1;
}
int findroot(int u) {
access(u); splay(u);
while(ch[u][0]) u = ch[u][0];
return u;
}
void link(int u, int v) {
makeroot(u);
fa[u] = v;
}
void cut(int u, int v) {
makeroot(u);
access(v);
splay(v);
fa[u] = ch[v][0] = 0;
update(v);
}
int query(int u, int v) {
makeroot(u);
access(v);
splay(v);
return wz[v];
}
int find(int x) {return f[x] == x ? x : f[x] = find(f[x]);}
int main() {
read(n); read(m);
for(int i = 1; i <= n; i++) f[i] = i;
for(int i = 1; i <= m; i++) {
read(p[i].u); read(p[i].v);
read(p[i].a); read(p[i].b);
}
sort(p + 1, p + m + 1);
for(int i = 1; i <= m; i++) {
int x = p[i].u, y = p[i].v;
int A = p[i].a, B = p[i].b;
if(find(x) != find(y)) {
f[find(x)] = find(y);
val[i + n] = maxn[i + n] = B;
link(x, i + n); link(y, i + n);
} else {
if(x == y) continue;
int weizhi = query(x, y);
if(p[weizhi - n].b > B) {
cut(p[weizhi - n].u, weizhi);
cut(p[weizhi - n].v, weizhi);
val[i + n] = maxn[i + n] = B;
link(x, i + n); link(y, i + n);
}
}
if(find(1) == find(n)) ans = min(ans, p[query(1, n) - n].b + A);
}
if(ans == INT_MAX) cout << -1 << endl; else cout << ans << endl;
return 0;
}
luoguP2387 [NOI2014]魔法森林的更多相关文章
- NOI2014 魔法森林
3669: [Noi2014]魔法森林 Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 106 Solved: 62[Submit][Status] ...
- bzoj 3669: [Noi2014]魔法森林 动态树
3669: [Noi2014]魔法森林 Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 363 Solved: 202[Submit][Status] ...
- BZOJ 3669: [Noi2014]魔法森林( LCT )
排序搞掉一维, 然后就用LCT维护加边MST. O(NlogN) ------------------------------------------------------------------- ...
- bzoj 3669: [Noi2014]魔法森林
bzoj 3669: [Noi2014]魔法森林 Description 为了得到书法大家的真传,小E同学下定决心去拜访住在魔法森林中的隐士.魔法森林可以被看成一个包含个N节点M条边的无向图,节点标号 ...
- BZOJ_3669_[Noi2014]魔法森林_LCT
BZOJ_3669_[Noi2014]魔法森林_LCT Description 为了得到书法大家的真传,小E同学下定决心去拜访住在魔法森林中的隐士.魔法森林可以被看成一个包含个N节点M条边的无向图,节 ...
- bzoj 3669: [Noi2014]魔法森林 (LCT)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3669 题面: 3669: [Noi2014]魔法森林 Time Limit: 30 Sec ...
- 「luogu2387」[NOI2014] 魔法森林
「luogu2387」[NOI2014] 魔法森林 题目大意 \(n\) 个点 \(m\) 条边的无向图,每条边上有两个权值 \(a,b\),求从 \(1\) 节点到 \(n\) 节点 \(max\{ ...
- P2387 [NOI2014]魔法森林(LCT)
P2387 [NOI2014]魔法森林 LCT边权维护经典题 咋维护呢?边化为点,边权变点权. 本题中我们把边对关键字A进行排序,动态维护关键字B的最小生成树 加边后出现环咋办? splay维护最大边 ...
- [NOI2014]魔法森林 LCT
题面 [NOI2014]魔法森林 题解 一条路径的代价为路径上的\(max(a[i]) + max(b[i])\),因为一条边同时有$a[i], b[i]$2种权值,直接处理不好同时兼顾到,所以我们考 ...
随机推荐
- Centos安装php高版本
安装 1.检查当前是否有安装php rpm -qa|grep php 如果有安装PHP,那么请先删除这些安装包: yum remove php* 2.安装php源 Centos 5 安装php源: ...
- krpano之语音介绍
语音介绍:在每进入一个场景时,播放一段该场景的语音介绍. 制作步骤: 1.定义全局事件.在关闭场景时执行stopsounds(),在打开新场景时执行automusic(). <events on ...
- Halcon学习之三:有关图像通道的函数
黑白摄像机会返回每个像素所对应的能量采用结果,这些结果组成了一幅单通道灰度值图像,而对于RGB彩色摄像机,它将返回每个像素所对应的三个采样结果,也就是一幅三通道图像.下面这些是与图像通道有关的函数: ...
- 每天一个Linux命令 - 【chkconfig】
[命令]:chkonconfig [语法]:chkconfig [选项] [功能介绍]:chkconfig 命令是Redhat兼容的Linux发行版中的系统服务管理工具,它可以查询和更新不同的运行等一 ...
- angularJS笔记之 服务
angular的服务有五种 第一种 constant 一般作为一种常量的服务 不可更改 第二种 value 用来注册服务对象或函数 可更改 第三种 factory 创建和配置服务的最快捷方式.可更改 ...
- 最长上升子序列(LIS)
最长递增子序列,Longest Increasing Subsequence 下面我们简记为 LIS.排序+LCS算法 以及 DP算法就忽略了,这两个太容易理解了. 假设存在一个序列d[1..9] = ...
- swift之xib关联UIView
有点坑爹,设置file owner 不行,搞了一早上,来说下怎么关联吧 自定义UIView要重写 required init(coder aDecoder: NSCoder) { super.init ...
- Location - BOM对象
Location 对象 Location 对象包含有关当前 URL 的信息. Location 对象是 Window 对象的一个部分,可通过 window.location 属性来访问. 例子 把用户 ...
- [bzoj3223]文艺平衡树(splay区间反转模板)
解题关键:splay模板题. #include<cstdio> #include<cstring> #include<algorithm> #include< ...
- PHP数组在循环的时候修改本身的值
这样的修改并不是修改本身,$item就相当于赋值了一份数组中的值,就跟JAVA中的值方式传递值类型一样,我只是拿了你的值,并不是拿了你的内存地址,所已$item的改变,并不会影响数组 第一种方式就是直 ...