bzoj3669[Noi2014]魔法森林
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define maxn 150005
#define maxm 100005
#define pi pair<int,int>
#define mp(a,b) make_pair(a,b)
using namespace std; struct note{
int u,v,a,b;
}wi[maxm];
int n,m,ans,fa[maxn],son[maxn][],val[maxn],sm[maxn],sm_id[maxn];
bool rev[maxn]; bool comp(note x,note y){
if (x.a==y.a) return x.b<y.b;
return x.a<y.a;
} struct date{
int which(int x){
return son[fa[x]][]==x;
}
int isroot(int x){
return son[fa[x]][]!=x&&son[fa[x]][]!=x;
}
void update(int x){
sm_id[x]=x,sm[x]=val[x];
if (son[x][]&&sm[son[x][]]>sm[x]) sm[x]=sm[son[x][]],sm_id[x]=sm_id[son[x][]];
if (son[x][]&&sm[son[x][]]>sm[x]) sm[x]=sm[son[x][]],sm_id[x]=sm_id[son[x][]];
}
void pushdown(int x){
if (rev[x]){
rev[x]^=,swap(son[x][],son[x][]);
if (son[x][]) rev[son[x][]]^=;
if (son[x][]) rev[son[x][]]^=;
}
}
void relax(int x){
if (!isroot(x)) relax(fa[x]);
pushdown(x);
}
void rotata(int x){
int y=fa[x],d=which(x),dd=which(y);
if (!isroot(y)) son[fa[y]][dd]=x; fa[x]=fa[y];
fa[son[x][d^]]=y,son[y][d]=son[x][d^];
fa[y]=x,son[x][d^]=y;
update(y);
}
void splay(int x){
relax(x);
while (!isroot(x)){
if (isroot(fa[x])) rotata(x);
else if (which(x)==which(fa[x])) rotata(fa[x]),rotata(x);
else rotata(x),rotata(x);
}
update(x);
}
void access(int x){
for (int p=;x;x=fa[x]){
splay(x);
son[x][]=p;
update(x);
p=x;
}
}
void make_root(int x){
access(x);
splay(x);
rev[x]^=;
}
void link(int x,int y){
make_root(x);
fa[x]=y;
}
void cut(int x,int y){
make_root(x);
access(y);
splay(y);
son[y][]=fa[x]=;
update(y);
}
void split(int x,int y){
make_root(x);
access(y);
splay(y);
}
int query(int x,int y){
split(x,y);
return sm[y];
}
pi find(int x,int y){
split(x,y);
return mp(sm_id[y],sm[y]);
}
int find_root(int x){
access(x);
splay(x);
while (son[x][]) x=son[x][];
return x;
}
}lct; int main(){
// freopen("forest.in","r",stdin);
// freopen("forest.out","w",stdout);
memset(rev,,sizeof(rev));
memset(fa,,sizeof(fa));
memset(son,,sizeof(son));
memset(val,,sizeof(val));
memset(sm,,sizeof(sm));
scanf("%d%d",&n,&m);
for (int i=;i<=m;i++) scanf("%d%d%d%d",&wi[i].u,&wi[i].v,&wi[i].a,&wi[i].b);
sort(wi+,wi+m+,comp);
for (int i=;i<=n;i++) val[i]=,lct.update(i);
ans=maxn;
pi temp;
for (int i=;i<=m;i++){
int u=wi[i].u,v=wi[i].v;
if (lct.find_root(u)!=lct.find_root(v)){
val[n+i]=wi[i].b,lct.update(n+i);
lct.link(n+i,u),lct.link(n+i,v);
}else{
temp=lct.find(u,v);
if (temp.second<=wi[i].b) continue;
else{
int t=temp.first;
lct.cut(wi[t-n].u,t),lct.cut(wi[t-n].v,t);
val[n+i]=wi[i].b,lct.update(n+i);
lct.link(n+i,u),lct.link(n+i,v);
}
}
if (lct.find_root()!=lct.find_root(n)) continue;
int t=lct.query(,n);
ans=min(ans,t+wi[i].a);
}
if (ans>maxm) printf("-1\n");
else printf("%d\n",ans);
return ;
}
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3669
题目大意:给定一个无向图,每条边有两个权值va,vb,要求选一条从1到n的路径,满足这条路径上点的max(va)+max(vb)最小,若没有从A到B的路径,则输出-1。
做法:初看这题,暴力写法:将边按va升序排序,枚举i,此时max(va)=vi,保证max(vb)最小即可,我们可以想到kruscal,并查集集维护即可,但是瓶颈在于每次都要将1~i的边按vb升序排序,在O(n)的加入,这种做法复杂度过高,不宜使用。
仔细想想:我们可以考虑用lct维护这个过程,考虑先将边按va升序排序,然后依次加入每一条边,此时max(va)=vi,保证max(vb)最小即可,加入该边时会有两种情况:
1.不形成环,则加入这条边,若节点1与节点n联通,则用1到n链上vb最大值+vi更新答案,否则不更新答案。
2.形成环,与lct模拟kruscal的过程一样,删掉原本那条链上vb权值最大的边,并加入这条边,若节点1与节点n联通,则用1到n链上vb最大值+vi更新答案,否则不更新答案。
lct+离线处理
bzoj3669[Noi2014]魔法森林的更多相关文章
- bzoj3669: [Noi2014]魔法森林 lct版
先上题目 bzoj3669: [Noi2014]魔法森林 这道题首先每一条边都有一个a,b 我们按a从小到大排序 每次将一条路劲入队 当然这道题权在边上 所以我们将边化为点去连接他的两个端点 当然某两 ...
- [bzoj3669][Noi2014]魔法森林_LCT_并查集
魔法森林 bzoj-3669 Noi-2014 题目大意:说不明白题意系列++……题目链接 注释:略. 想法:如果只有1个参量的话spfa.dij什么的都上来了. 两个参量的话我们考虑,想将所有的边按 ...
- BZOJ3669[Noi2014]魔法森林——kruskal+LCT
题目描述 为了得到书法大家的真传,小E同学下定决心去拜访住在魔法森林中的隐士.魔法森林可以被看成一个包含个N节点M条边的无向图,节点标号为1..N,边标号为1..M.初始时小E同学在号节点1,隐士则住 ...
- BZOJ3669 [Noi2014]魔法森林(SPFA+动态加边)
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- BZOJ3669: [Noi2014]魔法森林(瓶颈生成树 LCT)
Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 3558 Solved: 2283[Submit][Status][Discuss] Descript ...
- [bzoj3669][Noi2014]魔法森林——lct
Brief description 给定一个无向图,求从1到n的一条路径使得这条路径上最大的a和b最小. Algorithm Design 以下内容选自某HN神犇的blog 双瓶颈的最小生成树的感觉, ...
- bzoj3669: [Noi2014]魔法森林 lct
记得去年模拟赛的时候好像YY出二分答案枚举a,b的暴力,过了55欸 然后看正解,为了将两维变成一维,将a排序,模拟Kruskal的加边过程,同时维护1到n的最大值,加入一条边e(u,v,a,b)时有以 ...
- 沉迷Link-Cut tree无法自拔之:[BZOJ3669][Noi2014] 魔法森林
来自蒟蒻 \(Hero \_of \_Someone\) 的 \(LCT\) 学习笔记 $ $ 有一个很好的做法是 \(spfa\) ,但是我们不聊 \(spfa\) , 来聊 \(LCT\) \(L ...
- BZOJ3669 NOI2014魔法森林
按a从小到大排序,然后按b建图. 每次只需要找1~n中最大的b加当前的a计算答案即可. 这里还有一个小操作就是化边为点,把一条边的边权看做一个点的点权然后多连两条边. By:大奕哥 #include& ...
随机推荐
- STL标准库面试常考知识点
C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vector, string, list等方便的容器,更重要的是STL封装了许多复杂的数据结构算法和大量常用数据结构操作.vecto ...
- 带参数的CLR存储过程
昨天有学习<简单创建与布署CLR存储过程>http://www.cnblogs.com/insus/p/4371762.html,知道怎样创建以及布署至SQL中去. 下面这个范例是实现CL ...
- codevs2693 上学路线(施工)
难度等级:黄金 2693 上学路线(施工) 题目描述 Description 问题描述 你所在的城市街道好像一个棋盘,有a条南北方向的街道和b条东西方向的街道. 南北方向a条街道从西到东依次编号为1到 ...
- 学习C++.Primer.Plus 4 复合类型
本章介绍的有复合类型有: 数组. 字符串. 结构. 共用体. 指针 数组: 声明数组时数组长度必须为常量(或const). 只有初始化时可以用“=”,其它时候均不可以给数组直接赋值,除了赋值的元素以外 ...
- LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree
LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...
- servlet乱码问题总结
在学习时servlet乱码问题还是挺严重的,总结一下有三种情况 1.新建HTML页面后浏览出现乱码 2.以post形式请求时出现乱码 3.以get形式请求时出现乱码 让我们一个一个来解决吧 1.新建H ...
- Theano2.1.4-基础知识之图结构
来自:http://deeplearning.net/software/theano/tutorial/symbolic_graphs.html Graph Structures Theano是将符号 ...
- Scala集合操作
大数据技术是数据的集合以及对数据集合的操作技术的统称,具体来说: 1.数据集合:会涉及数据的搜集.存储等,搜集会有很多技术,存储技术现在比较经典方案是使用Hadoop,不过也很多方案采用Kafka. ...
- C#:异步编程和线程的使用(.NET 4.5 )
摘自:http://www.codeproject.com/Articles/996857/Asynchronous-programming-and-Threading-in-Csharp-N(葡萄城 ...
- 【Alpha版本】冲刺阶段——Day 6
我说的都队 031402304 陈燊 031402342 许玲玲 031402337 胡心颖 03140241 王婷婷 031402203 陈齐民 031402209 黄伟炜 031402233 郑扬 ...