「luogu2387」[NOI2014] 魔法森林

题目大意

\(n\) 个点 \(m\) 条边的无向图,每条边上有两个权值 \(a,b\),求从 \(1\) 节点到 \(n\) 节点 \(max\{a\}+max\{b\}\) 的最小值。图中可能有重边和自环。\((n \leq 5 \times 10^4 , m \leq 10^5)\)

一句话题解

考虑生成树 ( 过程类似 \(kruskal​\) );

把边按照 \(a\) 从小到大排序,\(1-m\) 枚举边,设边连接的两点为 \(u , v\);

若 \(u , v\) 已经在一个联通块中,则在 \(u\) 到 \(v\) 的路径中找一个 \(b\) 的最大值 \(b_{max}\),如果 \(b_{max} > b当前枚举到的边\),就把 \(当前枚举到的边\) 连接,\(max_b\) 所在的边断开;

若 \(u , v\) 不在一个联通块中,则直接连接;

边用 \(lct\) 维护即可;

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
inline int in() {
int x=0;char c=getchar();bool f=false;
while(c<'0'||c>'9') f|=c=='-', c=getchar();
while(c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48), c=getchar();
return f?-x:x;
} const int N = 5e4+5, M = 1e5+5;
struct edge {
int u, v, a, b;
friend inline bool operator < (const edge &x, const edge &y) {
return x.a < y.a;
}
}e[M];
int n, m; template<typename T>inline void chk_min(T &_, T __) { _=_<__?_:__; } struct link_cut_tree {
#define lson c[p][0]
#define rson c[p][1]
int c[N+M][2], fa[N+M], val[N+M], max[N+M];
bool rev[N+M]; inline bool nroot(int p) {
return c[fa[p]][0]==p||c[fa[p]][1]==p;
}
inline void push_up(int p) {
max[p]=p;
if(lson&&val[max[lson]]>val[max[p]]) max[p]=max[lson];
if(rson&&val[max[rson]]>val[max[p]]) max[p]=max[rson];
}
inline void rever(int p) {
std::swap(lson, rson);
rev[p]^=1;
}
inline void push_down(int p) {
if(rev[p]) {
if(lson) rever(lson);
if(rson) rever(rson);
rev[p]=false;
}
}
void push_all(int p) {
if(nroot(p)) push_all(fa[p]);
push_down(p);
}
inline void rotate(int x) {
int y=fa[x], z=fa[y], l=c[y][1]==x, r=l^1;
if(nroot(y)) c[z][c[z][1]==y]=x;
fa[x]=z, fa[c[x][r]]=y, fa[y]=x;
c[y][l]=c[x][r], c[x][r]=y;
push_up(y), push_up(x);
}
inline void splay(int x) {
int y, z;
push_all(x);
while(nroot(x)) {
y=fa[x], z=fa[y];
if(nroot(y)) rotate(c[y][0]==x^c[z][0]==y?x:y);
rotate(x);
}
}
inline void access(int p) { for(int s=0;p;p=fa[s=p]) splay(p), rson=s, push_up(p); }
inline void make_root(int p) { access(p), splay(p), rever(p); }
inline void split(int p, int s) { make_root(p), access(s), splay(s); }
inline void link(int p, int s) { make_root(p), fa[p]=s; }
inline void cut(int p, int s) { split(p, s), c[s][0]=fa[p]=0, push_up(s); }
inline int select(int p, int s) { split(p, s); return max[s]; }
#undef lson
#undef rson
}lct; struct disjoint_set_union {
int fa[N];
int get_fa(int u) { return u==fa[u]?u:fa[u]=get_fa(fa[u]); }
inline int & operator [] (int u) { return fa[u]; }
}dsu; inline int kruskal() {
int ret=-1;
std::sort(e+1, e+1+m);
for(int i=1;i<=n;++i) dsu[i]=i;
for(int i=n+1;i<=n+m;++i) lct.val[i]=e[i-n].b;
for(int i=1;i<=n+m;++i) lct.max[i]=i;
for(int i=1;i<=m;++i) {
if(e[i].u==e[i].v) continue;
int u=dsu.get_fa(e[i].u), v=dsu.get_fa(e[i].v);
if(u==v) {
int k=lct.select(e[i].u, e[i].v);
if(lct.val[k]>e[i].b) {
lct.cut(e[k-n].u, k), lct.cut(k, e[k-n].v);
lct.link(e[i].u, i+n), lct.link(i+n, e[i].v);
}
}
else {
dsu[u]=v;
lct.link(e[i].u, i+n), lct.link(i+n, e[i].v);
}
if(dsu.get_fa(1)==dsu.get_fa(n)) {
lct.split(1, n);
if(ret==-1) ret=lct.val[lct.max[n]]+e[i].a;
else chk_min(ret, lct.val[lct.max[n]]+e[i].a);
}
}
return ret;
} int main() {
n=in(), m=in();
for(int i=1;i<=m;++i)
e[i]=(edge){in(), in(), in(), in()};
printf("%d\n", kruskal());
return 0;
}

「luogu2387」[NOI2014] 魔法森林的更多相关文章

  1. NOI2014 魔法森林

    3669: [Noi2014]魔法森林 Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 106  Solved: 62[Submit][Status] ...

  2. bzoj 3669: [Noi2014]魔法森林 动态树

    3669: [Noi2014]魔法森林 Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 363  Solved: 202[Submit][Status] ...

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

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

  4. bzoj 3669: [Noi2014]魔法森林

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

  5. BZOJ_3669_[Noi2014]魔法森林_LCT

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

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

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

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

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

  8. [NOI2014]魔法森林 LCT

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

  9. bzoj 3669: [Noi2014]魔法森林 -- 动点spfa

    3669: [Noi2014]魔法森林 Time Limit: 30 Sec  Memory Limit: 512 MB 动点spfa Description 为了得到书法大家的真传,小E同学下定决心 ...

随机推荐

  1. django.db.utils.ProgrammingError: (1146, "Table 'db_gold.user_ip_info' doesn't exist") RuntimeError: Model class scanhosts.models.HostLoginInfo doesn't declare an explicit app_label and isn't in an a

    Error Msg 创建了一个apps的目录将所有app放入apps文件中, 将apps路径加入sys.path中:sys.insert(0, os.path.join(BASE_DIR, " ...

  2. [百度百科]dir命令指定显示的排序方式

    https://jingyan.baidu.com/article/7c6fb428dcf39880642c9095.html 今天工作中遇到了这个需求 感觉很好用 dir /o:d >name ...

  3. DELPHI中build和compile有什么区别?

    Build编译全部与工程相关联的文件,可包括版本信息及工程中的预编译变量等:Compile只重新编译更改过的相关单元及文件,调试是Compile就可以了,若是发布,则Build为好 BUILD  =C ...

  4. mysql原生语句基础知识

    要操作数据库,首先要登录mysql: *mysql -u root -p 密码 创建数据库: *create database Runoob(数据库名); 删除数据库: *drop database ...

  5. Word技巧设置

    1.如何在Word中快速输入上下标 下标~~同时按住:Ctrl 和 =键: 上标~~同时按住:Ctrl 和 Shift 和 +键: 2.如何删除Word中产生的空白页 设置 段落 中的   固定值  ...

  6. 《AutoCAD Civil 3D .NET二次开发》勘误1

    第十三章atc文件中Displayname应为DisplayName,注意Name的N为大写,否则参数名称无法正常显示. 给您带来的不便深表歉意!

  7. Mint-UI Picker 三级联动

    Mint-UI Picker组件的三级联动 HTML: <mt-picker :slots="slots" value-key="name" @chang ...

  8. Want To Say Something

    2019.3.3 明天要演讲了,在这里为自己打一下气! 加油!     2019.3.31 停课三周的第一次写日志 怎么说这三周结交了很多八班的朋友 在竞赛上一直在学数论快学吐了,但没办法呀还是要为出 ...

  9. Auto Layout Masonry

    1. Auto layout 1.1 NSLayoutConstraint 1.1.1 约束类 ios6.0可用 为了更好的适配各个尺寸 1.1.2 constraintWithItem:attrib ...

  10. [CQOI2009] 中位数

    不错的思维题 传送门:$>here<$ 题意:给出一个N的排列,求出其中有多少个连续子段的中位数是b 数据范围:$N \leq 100000$ $Solution$ 先考虑中位数的意义:一 ...