BZOJ 3669 魔法森林
LCT维护生成树
先按照a的权值把边排序,离线维护b的最小生成树。
将a排序后,依次动态加边,我们只需要关注b的值。要保证1-n花费最少,两点间的b值肯定是越小越好,所以我们可以考虑以b为关键字维护最小生成树。
对于新加的边b,如果1-n已经联通,需要更新答案
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
int X = 0, w = 0; char ch = 0;
while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
return w ? -X : X;
}
inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
A ans = 1;
for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
return ans;
}
const int N = 400005;
int n, m, tot, ans, mx[N], fa[N], w[N], ch[N][2], rev[N], id[N], st[N];
struct Edge {
int v, u, a, b;
bool operator < (const Edge &rhs) const {
return a < rhs.a;
}
} e[N];
int newNode(int v){
++tot;
w[tot] = mx[tot] = v, id[tot] = tot;
fa[tot] = ch[tot][0] = ch[tot][1] = 0;
return tot;
}
bool isRoot(int x){
return ch[fa[x]][0] != x && ch[fa[x]][1] != x;
}
void reverse(int x){
rev[x] ^= 1, swap(ch[x][0], ch[x][1]);
}
void push_up(int x){
int l = ch[x][0], r = ch[x][1];
mx[x] = w[x], id[x] = x;
if(mx[l] > mx[x]) mx[x] = mx[l], id[x] = id[l];
if(mx[r] > mx[x]) mx[x] = mx[r], id[x] = id[r];
}
void push_down(int x){
if(rev[x]){
rev[x] ^= 1;
if(ch[x][0]) reverse(ch[x][0]);
if(ch[x][1]) reverse(ch[x][1]);
}
}
void rotate(int x){
int y = fa[x], z = fa[y], p = (ch[y][1] == x) ^ 1;
ch[y][p^1] = ch[x][p], fa[ch[x][p]] = y;
if(!isRoot(y)) ch[z][ch[z][1] == y] = x;
fa[x] = z, fa[y] = x, ch[x][p] = y;
push_up(y), push_up(x);
}
void splay(int x){
int pos = 0; st[++pos] = x;
for(int i = x; !isRoot(i); i = fa[i]) st[++pos] = fa[i];
while(pos) push_down(st[pos--]);
while(!isRoot(x)){
int y = fa[x], z = fa[y];
if(!isRoot(y)) (ch[y][0] == x) ^ (ch[z][0] == y) ? rotate(x) : rotate(y);
rotate(x);
}
push_up(x);
}
void access(int x){
for(int p = 0; x; p = x, x = fa[x])
splay(x), ch[x][1] = p, push_up(x);
}
void makeRoot(int x){
access(x), splay(x), reverse(x);
}
void link(int x, int y){
makeRoot(x);
fa[x] = y;
}
int findRoot(int x){
access(x), splay(x);
while(ch[x][0]) push_down(x), x = ch[x][0];
splay(x);
return x;
}
void split(int x, int y){
makeRoot(x), access(y), splay(y);
}
bool isConnect(int x, int y){
makeRoot(x);
return findRoot(y) == x;
}
int main(){
ans = INF;
n = read(), m = read();
for(int i = 1; i <= n; i ++) newNode(0);
for(int i = 0; i < m; i ++){
e[i].u = read(), e[i].v = read(), e[i].a = read(), e[i].b = read();
}
sort(e, e + m);
for(int i = 0; i < m; i ++){
int u = e[i].u, v = e[i].v, t = newNode(e[i].b);
if(!isConnect(u, v)) link(u, t), link(t, v);
else{
split(u, v);
if(e[i].b > mx[v]) continue;
int tmp = id[v]; splay(tmp);
fa[ch[tmp][0]] = fa[ch[tmp][1]] = 0;
ch[tmp][0] = ch[tmp][1] = 0;
link(u, t), link(t, v);
}
if(isConnect(1, n)){
split(1, n);
ans = min(ans, mx[n] + e[i].a);
}
}
printf(ans == INF ? "-1\n" : "%d\n", ans);
return 0;
}
BZOJ 3669 魔法森林的更多相关文章
- 洛谷 2387/BZOJ 3669 魔法森林
3669: [Noi2014]魔法森林 Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 3765 Solved: 2402[Submit][Statu ...
- bzoj 3669: [Noi2014]魔法森林
bzoj 3669: [Noi2014]魔法森林 Description 为了得到书法大家的真传,小E同学下定决心去拜访住在魔法森林中的隐士.魔法森林可以被看成一个包含个N节点M条边的无向图,节点标号 ...
- 【BZOJ】3669: [Noi2014]魔法森林(lct+特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=3669 首先看到题目应该可以得到我们要最小化 min{ max{a(u, v)} + max{b(u, ...
- 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]魔法森林 (LCT)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3669 题面: 3669: [Noi2014]魔法森林 Time Limit: 30 Sec ...
- bzoj 3669: [Noi2014]魔法森林 -- 动点spfa
3669: [Noi2014]魔法森林 Time Limit: 30 Sec Memory Limit: 512 MB 动点spfa Description 为了得到书法大家的真传,小E同学下定决心 ...
- 【BZOJ 3669】 3669: [Noi2014]魔法森林 (动态spfa)
3669: [Noi2014]魔法森林 Description 为了得到书法大家的真传,小E同学下定决心去拜访住在魔法森林中的隐士.魔法森林可以被看成一个包含个N节点M条边的无向图,节点标号为1..N ...
- BZOJ 3669 【NOI2014】 魔法森林
Description 为了得到书法大家的真传,小E同学下定决心去拜访住在魔法森林中的隐士.魔法森林可以被看成一个包含个N节点M条边的无向图,节点标号为1..N,边标号为1..M.初始时小E同学在号节 ...
随机推荐
- SAP MM盘点流程里如何处理事务代码MI11 Recount过的盘点凭证?
SAP MM盘点流程里如何处理事务代码MI11 Recount过的盘点凭证? 1, MI01 create a physical inventory document, 2, MI04 to inpu ...
- BIM与GIS
BIM行业是建筑与IT结合而形成的一个新兴行业,既然能说是行业,说明它包含的内容非常丰富,懂一点和完全懂是两码事,就好像一滴水和一片大海的范围一样.现在国内有很多高校开设了BIM专业,并对口招收了学生 ...
- Bitmap上下合成图片
合成两张图片,上下叠加的效果: /** * 把两个位图覆盖合成为一个位图,以底层位图的长宽为基准 * * @param backBitmap 在底部的位图 * @param frontBitmap 盖 ...
- 【原】Java学习笔记023 - 字符串缓冲区_正则表达式
package cn.temptation; import java.util.Arrays; public class Sample01 { public static void main(Stri ...
- kmalloc分配物理内存与高端内存映射--Linux内存管理(十八)
1 前景回顾 1.1 内核映射区 尽管vmalloc函数族可用于从高端内存域向内核映射页帧(这些在内核空间中通常是无法直接看到的), 但这并不是这些函数的实际用途. 重要的是强调以下事实 : 内核提供 ...
- LeetCode算法题-Maximum Depth of N-ary Tree(Java实现)
这是悦乐书的第261次更新,第274篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第128题(顺位题号是559).给定n-ary树,找到它的最大深度.最大深度是从根节点到 ...
- 全民抵制“辱华”品牌秀,D&G神回复:呵呵~ 那不是我!
### 补发一下,前写天写的: 就在今天下午,有网友爆出知名品牌 Dolce&Gabbana(杜嘉班纳)的设计师兼创始人Stefano Gabbana在ins上公然发表辱华言论. 下面截图 可 ...
- CSS--字体|垂直居中|background
一,字体的设置 二,垂直居中 2.1,单行文本垂直居中 2.2,多行文本垂直居中 2.3,绝对定位元素垂直居中 三.颜色的表示法 四.background ---------------------- ...
- Enterprise Architect 时序图
添加时序图 1,在类图下面新建包 添加sequence时序图 点击流程控制,可以打开流程控制设计界面 我选择的是Lifeline线,你可以选择都差不多. 点击其中一条liftline连到其他上面 双击 ...
- docker 常用命令和常用容器启动
docker:systemctl start docker # docker 启动systemctl stop docker # docker 停止systemctl restart docker # ...