loj2245 [NOI2014]魔法森林 LCT
[NOI2014]魔法森林
链接
思路
a排序,b做动态最小生成树。
把边拆成点就可以了。
uoj98.也许lct复杂度写假了、、越卡常,越慢
代码
#include <bits/stdc++.h>
#define ls c[x][0]
#define rs c[x][1]
using namespace std;
const int N = 2e5 + 7;
int read() {
int x = 0, f = 1; char s = getchar();
for (;s > '9' || s < '0'; s = getchar()) if (s == '-') f = -1;
for (;s >= '0' && s <= '9'; s = getchar()) x = x * 10 + s - '0';
return x * f;
}
struct edge {
int x, y, a, b;
bool operator < (const edge &zz) const {
return (a^zz.a) ? a < zz.a : b < zz.b;
}
} G[N];
int f[N], c[N][2], w[N][2], ma[N][2], stak[N], lazy[N], id[N];
bool isroot(int x) {return c[f[x]][0] == x || c[f[x]][1] == x;}
void pushup(int x) {
ma[x][0] = max(max(ma[ls][0], ma[rs][0]), w[x][0]);
ma[x][1] = max(max(ma[ls][1], ma[rs][1]), w[x][1]);
id[x] = (ma[x][1] == w[x][1]) ? x : (ma[ls][1] > ma[rs][1]) ? id[ls] : id[rs];
}
void tag(int x){swap(ls,rs), lazy[x] ^= 1;}
void pushdown(int x) {
if (lazy[x]) {
if (ls) tag(ls);
if (rs) tag(rs);
lazy[x] ^= 1;
}
}
void rotate(int x) {
int y = f[x], z = f[y], k = c[y][1] == x, w = c[x][!k];
if (isroot(y)) c[z][c[z][1] == y] = x;
c[x][!k] = y, c[y][k] = w;
if (w) f[w] = y;
f[x] = z, f[y] = x;
pushup(y);
}
void splay(int x) {
int y = x, z = 0;
stak[++z] = y;
while (isroot(y)) stak[++z] = y = f[y];
while (z) pushdown(stak[z--]);
while (isroot(x)) {
y = f[x], z = f[y];
if (isroot(y)) rotate((c[y][0] == x)^(c[z][0] == y) ? x : y);
rotate(x);
}
pushup(x);
}
void access(int x) {
for (int y = 0; x;x = f[y = x])
splay(x), rs = y, pushup(x);
}
void makeroot(int x) {
access(x), splay(x);
tag(x);
}
int findroot(int x) {
access(x), splay(x);
while(ls) pushdown(x), x = ls;
return x;
}
void split(int x, int y) {
makeroot(x), access(y), splay(y);
}
void link(int x, int y) {
makeroot(x);
if (findroot(y) != x) f[x] = y;
}
void cut(int x, int y) {
makeroot(x);
if (findroot(y) == x && f[x] == y && !rs) {
f[x] = c[y][0] = 0;
pushup(y);
}
}
int main() {
int n = read(), m = read(), ans = 0x3f3f3f3f;
for (int i = 1; i <= m; ++i)
G[i].x = read(), G[i].y = read(), G[i].a = read(), G[i].b = read();
sort(G + 1, G + 1 + m);
for (int i = 1; i <= m; ++i) {
if (G[i].x == G[i].y) continue;
int x = G[i].x, y = G[i].y;
if (findroot(x) == findroot(y)) {
split(x, y);
if (ma[y][1] > G[i].b) {
int tmp = id[y];
cut(G[tmp - n].x, tmp), cut(G[tmp - n].y, tmp);
w[n + i][0] = G[i].a, w[n + i][1] = G[i].b;
link(x, n + i), link(n + i, y);
}
} else {
w[n + i][0] = G[i].a, w[n + i][1] = G[i].b;
link(x, n + i), link(n + i, y);
}
if (findroot(1) == findroot(n)) {
split(1, n);
ans = min(ans, ma[n][0] + ma[n][1]);
}
}
printf("%d\n", ans == 0x3f3f3f3f ? -1 : ans);
return 0;
}
loj2245 [NOI2014]魔法森林 LCT的更多相关文章
- 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 ...
- [NOI2014]魔法森林 LCT
题面 [NOI2014]魔法森林 题解 一条路径的代价为路径上的\(max(a[i]) + max(b[i])\),因为一条边同时有$a[i], b[i]$2种权值,直接处理不好同时兼顾到,所以我们考 ...
- bzoj3669: [Noi2014]魔法森林 lct版
先上题目 bzoj3669: [Noi2014]魔法森林 这道题首先每一条边都有一个a,b 我们按a从小到大排序 每次将一条路劲入队 当然这道题权在边上 所以我们将边化为点去连接他的两个端点 当然某两 ...
- 【BZOJ3669】[Noi2014]魔法森林 LCT
终于不是裸的LCT了...然而一开始一眼看上去这是kruskal..不对,题目要求1->n的路径上的每个点的两个最大权值和最小,这样便可以用LCT来维护一个最小生成路(瞎编的...),先以a为关 ...
- bzoj 3669: [Noi2014] 魔法森林 LCT版
Description 为了得到书法大家的真传,小E同学下定决心去拜访住在魔法森林中的隐士.魔法森林可以被看成一个包含个N节点M条边的无向图,节点标号为1..N,边标号为1..M.初始时小E同学在号节 ...
- BZOJ 3669: [Noi2014]魔法森林 [LCT Kruskal | SPFA]
题目描述 为了得到书法大家的真传,小 E 同学下定决心去拜访住在魔法森林中的隐 士.魔法森林可以被看成一个包含 n 个节点 m 条边的无向图,节点标号为 1,2,3,…,n,边标号为 1,2,3,…, ...
- P2387 [NOI2014]魔法森林 LCT维护最小生成树
\(\color{#0066ff}{ 题目描述 }\) 为了得到书法大家的真传,小 E 同学下定决心去拜访住在魔法森林中的隐 士.魔法森林可以被看成一个包含 n 个节点 m 条边的无向图,节点标号为 ...
- 洛谷P2387 [NOI2014]魔法森林(LCT)
魔法森林 题目传送门 解题思路 把每条路按照\(a\)的值从小到大排序.然后用LCT按照b的值维护最小生成树,将边按照顺序放入.如果\(1\)到\(n\)有了一条路径,就更新最小答案.这个过程就相当于 ...
随机推荐
- Centos 7.6搭建Skywalking6.5+es6.2.4
软件包版本1.elasticsearch-6.2.4.tar.gz,下载地址:https://artifacts.elastic.co/downloads/elasticsearch/elastics ...
- 一次kuberneets evicted的历险
一.概述 kubernetes 的eviction检测diskpresure,检测的是kubelet的root-dir.kubelet的默认root-dir是/var/lib/kubelet,可以使用 ...
- 使用Net Mail发送邮件
最近用到了发送邮件这个功能,简单记录一下案例.代码如下: using System; using System.Collections.Generic; using System.Linq; usin ...
- Linux 监控之 IO
简单介绍下 Linux 中与 IO 相关的内容. 简介 可以通过如下命令查看与 IO 相关的系统信息. # tune2fs -l /dev/sda7 ← 读取superblock信息 # blockd ...
- BindRequired and Required
[https://www.cnblogs.com/tdfblog/p/required-and-bindrequired-in-asp-net-core-mvc.html] Required:对值进行 ...
- Java开发环境系列:一篇能解决你99%问题的排雷日记
安装 https://archive.apache.org/dist/tomcat/ 推荐使用免安装版的Tomcat(放在没有中文和空格的目录下),前提是已经安装了JDK并配置了环境变量.Linu ...
- [转]Java游戏引擎
JME(jMonkey Engine): JME是一个高性能的3D图形API,采用LWJGL作为底层支持.它的后续版本将支持JOGL.JME和Java 3D具有类似的场景结构,开发者必须以树状方式组织 ...
- flink Reduce、GroupReduce、GroupCombine笔记
1.reduce操作,在分组的dataset上使用,也可以在不分组的dataset上使用 应用于分组DataSet的Reduce转换使用用户定义的reduce函数将每个组减少为单个元素.对于每组输入元 ...
- 遇到的一个Buffer too small问题
在ROI中输出图像时遇到 经调试后发现是driver.Create时设置的波段数大于实际写入的波段数导致的 这里xImgIn.m_nBands有204,但实际写入的数据的bands只有3,修改时忘了修 ...
- JMX——以可视化形式管理与监控正在运行中的Java程序
简单理解: MBean:管理的最小单元,一个MBean就是一个可以被监控的JavaBean. MBeanServer:一个池子,各个MBean都会注册到该池子中,并且该池子提供一系列的管理.监控API ...