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\)有了一条路径,就更新最小答案.这个过程就相当于 ...
随机推荐
- 使用semaphore写一个显示锁
/** * 这里只是将Semaphore包装了一下,注意当Semaphore的构造参数是1时,本身就是一个显示锁 */ public class SemaphoreLock { ); public v ...
- fiverr无法注册的解决办法,fiverr注册教程
转载 https://www.wok99.com/450.html
- WPF XAML Trigger中使用动画后 动画对象冻结的处理办法
在编写XAML时 在Trigger中使用动画,在动画之后,动画对象就会被冻结,无法被其他动画或者属性改变. 处理办法有: 1 使用附加属性来添加动画 public static readonly De ...
- JavaIO学习:打印流
打印流 打印流是输出信息最方便的类,注意包含字节打印流:PrintStream和字符打印流:PrintWriter. 打印流提供了非常方便的打印功能,可以打印任何类型的数据信息,例如:小数,整数,字符 ...
- golang学习笔记 --flag
概述 flag包提供了一系列解析命令行参数的功能接口 命令行语法 命令行语法主要有以下几种形式 -flag //只支持bool类型 -flag=x -flag x //只支持非bool类型 以上语法对 ...
- Linq 将两个查询结果合称为一个
var handsonitems = from a in db.DltQuestionHandson join c in db.DltBdChapter on new { a.ChapterCode ...
- dedecms5.7执行PHP代码的用法
dedecms5.7执行PHP代码的用法 {dede:php} echo 'test'; {/dede:php}
- 数据库IN查询参数化改造的方法
// 批量查询的 2019-05-14 if (!string.IsNullOrWhiteSpace(Request["userCodes"])) { string userCod ...
- C#集合中根据多个字段分组 group by linq表达式
void Main() { var empList =new List<Employee> { , FName = , Sex = 'M'}, , FName = , Sex = 'F'} ...
- [世预赛] 中国vs关岛,关岛实力有限 国足或许可以赢其10个球,比分预测 10:0,8:0,13:0
[世预赛] 中国vs关岛 开赛时间:2019-10-10 20:00 继5比0大胜马尔代夫之后,国足迎来世预赛40强赛的第二场比赛,再次向世界杯发起冲击.10月10日,国足在广州迎战神秘之旅关岛. 1 ...