传送门

如果一条边只要考虑 $a$ 的限制,那么显然最小生成树

但是现在有 $a,b$ 两个限制,所以考虑按 $a$ 从小到大枚举边,动态维护 $b$ 的最小生成树

考虑新加入的一条边 $x,y$ ,如果 $x,y$ 不在一颗树上显然直接加入,如果在一棵树上,考虑原本树上 $x$ 到 $y$ 的路径上 $b$ 最大的边

如果比当前边大,那么就把原本那条边从最小生成树上删除,把新的边加进去

答案就在每次加边时更新就好了

这个东西显然直接 $LCT$ 维护,为了维护边权所以要把边权也看成点

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
return x*f;
}
const int N=4e5+,INF=1e9+;
int n,m,ANS=INF;
struct dat{
int x,y,a,b;
inline bool operator < (const dat &tmp) const {
return a<tmp.a;
}
}d[N];
int c[N][],fa[N],t[N],val[N];
//把边化成点后,t维护点权最大的点的编号,val[x]存点x的b值
//边的编号为n+1到n+m
bool rev[N];
inline void pushdown(int x)
{
if(!rev[x]||!x) return;
int &lc=c[x][],&rc=c[x][];
rev[x]=; swap(lc,rc);
if(lc) rev[lc]^=;
if(rc) rev[rc]^=;
}
inline void rever(int x) { rev[x]^=; pushdown(x); }
inline void pushup(int x)
{
t[x]=x;
if(val[ t[c[x][]] ] > val[t[x]]) t[x]=t[c[x][]];
if(val[ t[c[x][]] ] > val[t[x]]) t[x]=t[c[x][]];
}
inline bool notroot(int x) { return (c[fa[x]][]==x)|(c[fa[x]][]==x); }
inline void rotate(int x)
{
int y=fa[x],z=fa[y],d=(c[y][]==x);
if(notroot(y)) c[z][c[z][]==y]=x;
fa[x]=z; fa[y]=x; fa[c[x][d^]]=y;
c[y][d]=c[x][d^]; c[x][d^]=y;
pushup(y); pushup(x);
}
inline void push_rev(int x)
{
if(notroot(x)) push_rev(fa[x]);
else pushdown(x);
pushdown(c[x][]); pushdown(c[x][]);
}
inline void splay(int x)
{
push_rev(x);
while(notroot(x))
{
int y=fa[x],z=fa[y];
if(notroot(y))
{
if(c[y][]==x ^ c[z][]==y) rotate(x);
else rotate(y);
}
rotate(x);
}
}
inline void access(int x)
{
for(int y=;x;y=x,x=fa[x])
splay(x),c[x][]=y,pushup(x);
}
inline void makeroot(int x) { access(x); splay(x); rever(x); }
inline int findroot(int x)
{
access(x); splay(x); pushdown(x);
while(c[x][]) pushdown(c[x][]),x=c[x][];
splay(x);
return x;
}
inline int split(int x,int y) { makeroot(x); access(y); splay(y); return t[y]; }//提取一段路径上点权最大的点的编号
inline void link(int x,int y) { makeroot(x); if(findroot(y)!=x) fa[x]=y; }
inline void cut(int x,int y)
{
makeroot(x);
if(findroot(y)!=x||fa[y]!=x||c[y][]) return;
c[x][]=fa[y]=; pushup(x);
}
inline void query(int a)//更新答案
{
if(findroot()==findroot(n))//如果在同一颗树上
{
int w=split(,n);
ANS=min(ANS,a+val[w]);
}
}
inline void insert(int i)//加入边
{
int x=d[i].x,y=d[i].y,a=d[i].a,b=d[i].b; bool flag=;
if(findroot(x)==findroot(y))//如果原本已经是一颗树
{
int w=split(x,y);
if(val[w]>b) cut(w,d[w-n].x),cut(w,d[w-n].y);//如果b更小才cut
else flag=;//否则不连边
}
if(flag) link(n+i,x),link(n+i,y),query(a);//连边并更新ANS
}
int main()
{
n=read(),m=read();
for(int i=;i<=m;i++)
d[i].x=read(),d[i].y=read(),d[i].a=read(),d[i].b=read();
sort(d+,d+m+);
for(int i=;i<=m;i++) val[n+i]=d[i].b;
for(int i=;i<=m;i++) insert(i);
printf("%d",ANS <1e9 ? ANS : -);
return ;
}

P2387 [NOI2014]魔法森林的更多相关文章

  1. P2387 [NOI2014]魔法森林(LCT)

    P2387 [NOI2014]魔法森林 LCT边权维护经典题 咋维护呢?边化为点,边权变点权. 本题中我们把边对关键字A进行排序,动态维护关键字B的最小生成树 加边后出现环咋办? splay维护最大边 ...

  2. 洛谷 P2387 [NOI2014]魔法森林 解题报告

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

  3. 洛谷P2387 [NOI2014]魔法森林(lct维护最小生成树)

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

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

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

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

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

  6. [Luogu P2387] [NOI2014]魔法森林 (LCT维护边权)

    题面 传送门:https://www.luogu.org/problemnew/show/P2387 Solution 这题的思想挺好的. 对于这种最大值最小类的问题,很自然的可以想到二分答案.很不幸 ...

  7. 洛谷P2387 [NOI2014]魔法森林(LCT,Splay)

    在XZY&XZZ巨佬的引领下,一枚蒟蒻终于啃动了这道题...... 这次还是第一次写LCT维护边权,还要化边为点,思路乱七八糟的,写起来也不顺手,还好调了许久终于AC啦. 贪心排序按一个关键字 ...

  8. luogu P2387 [NOI2014]魔法森林

    传送门 这题似乎不好直接做,可以考虑按照\(a_i\)升序排序,然后依次加边更新答案 具体实现方法是用lct维护当前的树,这里需要维护链上最大的\(b_i\).每次加一条边,如果加完以后没有环直接加, ...

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

    在XZY&XZZ巨佬的引领下,一枚蒟蒻终于啃动了这道题...... 这次还是第一次写LCT维护边权,还要化边为点,思路乱七八糟的,写起来也不顺手,还好调了许久终于AC啦. 贪心排序按一个关键字 ...

随机推荐

  1. css-三边框,外边距和内边距

    <div style="width:100px;height:50px;border: solid black 1px;position: absolute;right: 500px; ...

  2. wampserver无法打开http://127.0.0.1/

    如果你更换了端口号那么你就应该把相应的端口号也带上,比如localhost:8080(或127.0.0.1:8080)这样应该就可以了,因为只有80端口是默认才不用输入的.

  3. boost库中sleep方法详解

    博客转载自:https://blog.csdn.net/huang_xw/article/details/8453506 boost库中sleep有两个方法: 1. 这个方法只能在线程中用, 在主线程 ...

  4. 数字图像处理实验(13):PROJECT 05-04,Parametric Wiener Filter 标签: 图像处理MATLAB 2017-05-27 10:59

    实验要求: Objective: To understand the high performance of the parametric Wiener Filter in image restora ...

  5. 编写高质量代码改善C#程序的157个建议——建议12: 重写Equals时也要重写GetHashCode

    建议12: 重写Equals时也要重写GetHashCode 除非考虑到自定义类型会被用作基于散列的集合的键值:否则,不建议重写Equals方法,因为这会带来一系列的问题. 如果编译上一个建议中的Pe ...

  6. leetcode N-Queens I && N-Queens II

    第一个的代码: #include<iostream> #include<vector> using namespace std; bool isLegal(int i, int ...

  7. Redis 占用Windows系统盘空间23G

    Redis常出现问题总结: 1.当出现修改--maxheap and --heapdir 在启动这两个版本时都会创建一个 RedisQFork.dat文件,我不确定 RedisQFork 文件是否变小 ...

  8. Ubuntu16.04切换python3和python2

    切换Python3为默认版本: sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100 sudo ...

  9. C# winform使用cefsharp

    注意事项写在开头: 一)cef要求.Net FrameWork框架最少为4.5.2,所以咱们在创建工程的时候注意 二)cef不支持anycpu平台 第一步: 新建工程  第二步 添加cef nuget ...

  10. git Problem with the SSL CA cert (path? access rights?)

    问题: [root@localhost opt]# git clone https://github.com/docker/docker.git 正克隆到 'docker'...fatal: unab ...