[NOI2014]魔法森林

链接

loj

思路

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的更多相关文章

  1. BZOJ 3669: [Noi2014]魔法森林( LCT )

    排序搞掉一维, 然后就用LCT维护加边MST. O(NlogN) ------------------------------------------------------------------- ...

  2. bzoj 3669: [Noi2014]魔法森林 (LCT)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3669 题面: 3669: [Noi2014]魔法森林 Time Limit: 30 Sec  ...

  3. [NOI2014]魔法森林 LCT

    题面 [NOI2014]魔法森林 题解 一条路径的代价为路径上的\(max(a[i]) + max(b[i])\),因为一条边同时有$a[i], b[i]$2种权值,直接处理不好同时兼顾到,所以我们考 ...

  4. bzoj3669: [Noi2014]魔法森林 lct版

    先上题目 bzoj3669: [Noi2014]魔法森林 这道题首先每一条边都有一个a,b 我们按a从小到大排序 每次将一条路劲入队 当然这道题权在边上 所以我们将边化为点去连接他的两个端点 当然某两 ...

  5. 【BZOJ3669】[Noi2014]魔法森林 LCT

    终于不是裸的LCT了...然而一开始一眼看上去这是kruskal..不对,题目要求1->n的路径上的每个点的两个最大权值和最小,这样便可以用LCT来维护一个最小生成路(瞎编的...),先以a为关 ...

  6. bzoj 3669: [Noi2014] 魔法森林 LCT版

    Description 为了得到书法大家的真传,小E同学下定决心去拜访住在魔法森林中的隐士.魔法森林可以被看成一个包含个N节点M条边的无向图,节点标号为1..N,边标号为1..M.初始时小E同学在号节 ...

  7. BZOJ 3669: [Noi2014]魔法森林 [LCT Kruskal | SPFA]

    题目描述 为了得到书法大家的真传,小 E 同学下定决心去拜访住在魔法森林中的隐 士.魔法森林可以被看成一个包含 n 个节点 m 条边的无向图,节点标号为 1,2,3,…,n,边标号为 1,2,3,…, ...

  8. P2387 [NOI2014]魔法森林 LCT维护最小生成树

    \(\color{#0066ff}{ 题目描述 }\) 为了得到书法大家的真传,小 E 同学下定决心去拜访住在魔法森林中的隐 士.魔法森林可以被看成一个包含 n 个节点 m 条边的无向图,节点标号为 ...

  9. 洛谷P2387 [NOI2014]魔法森林(LCT)

    魔法森林 题目传送门 解题思路 把每条路按照\(a\)的值从小到大排序.然后用LCT按照b的值维护最小生成树,将边按照顺序放入.如果\(1\)到\(n\)有了一条路径,就更新最小答案.这个过程就相当于 ...

随机推荐

  1. 标签一致项(LC-KSVD)-全文解读

    Learning A Discriminative Dictionary for Sparse Coding via Label Consistent K-SVD 1,同步学习判决字典和线性分类器 2 ...

  2. SSL证书格式转换

    crt格式转pem openssl x509 -in www.x.com.crt -out www.x.com.pem openssl x509 -in mycert.crt -out mycert. ...

  3. FormData的Ajax提交注意事项

    Ajax提交表单一般有下面两种形式: var form_data = $('#form').serialize(); var form_data = new FormData($('#form')[0 ...

  4. .net core+topshelf+quartz创建windows定时任务服务

    .net core+topshelf+quartz创建windows定时任务服务 准备工作 创建.net core 控制台应用程序,这里不做过多介绍 添加TopShelf包:TopShelf: 添加Q ...

  5. Linux 常用命令 , 其他名 , 文件管理

    Linux 常用命令 , 其他名 , 文件管理 一丶Linux常用的指令 1. bsystemctl stop firewalld #关闭防火墙 2. iptables -F #清空防火墙规则 3. ...

  6. C# 构造基础返回值类型-BaseResponse

    学无止境,精益求精 十年河东,十年河西,莫欺少年穷 用于基础返回值类型,如下: using System; using System.Collections.Generic; using System ...

  7. Vue递归组件实现层层嵌套显示数据

    问题来自朋友...记录一下 需求是表格头部后端返回的数据中是不确定的 n维数据,表头存在于 listVo 字段中,如何实现层层显示呢? 温馨提示,以下内容为5张大图,请打开 WIFI 享用... 以下 ...

  8. I still have a dream!

    当聊起梦想时,哥总会说别跟我谈梦想,我已经戒了!现在的我对梦想并不感冒,总是冷眼旁观很多事情,那些经不起时间检验的事和人,总会消散在历史云烟中,若干年后,又有谁还会记得那些遗弃在历史尘埃中,琐碎的芝麻 ...

  9. ssh免密登录(公钥私钥)指令

    1.在.ssh目录中执行ssh-keygen -t rsa命令生成两个秘钥,公钥(id_rsa.pub)和私钥(id_rsa) 2.ssh-copy-id -i id_rsa.pub 对方用户名@对方 ...

  10. MySQL Backup--使用mysqldump依次备份所有数据库

    某些场景下需要将数据库分开备份,有些场景又需要将所有数据库合在一起备份,特此整理此备份脚本 #!/bin/bash ##======================================== ...