先上题目 bzoj3669: [Noi2014]魔法森林

这道题首先每一条边都有一个a,b 我们按a从小到大排序 每次将一条路劲入队 当然这道题权在边上 所以我们将边化为点去连接他的两个端点

当然某两个点我用的是并查集维护 其实也可以在树上直接查询 但是这样比较方便 同时我们维护某个点极其子树的最大值所在的位置 每入一条边 如果两个端点已经联通就找出权值的边删掉之后连上新的边顺便更新答案 如果没联通就直接连上边就好 这样就解决问题了

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int M=,inf=0x3f3f3f3f;
int read(){
int ans=,f=,c=getchar();
while(c<''||c>''){if(c=='-') f=-; c=getchar();}
while(c>=''&&c<=''){ans=ans*+(c-''); c=getchar();}
return ans*f;
}
int n,m,ans=inf;
int c[M][],fa[M],p[M];
int mx[M],v[M],rev[M];
bool isrt(int x){return c[fa[x]][]!=x&&c[fa[x]][]!=x;}
struct node{int a,b,u,v;}e[M];
bool cmp(node a,node b){return a.a<b.a;}
int find(int x){return p[x]==x?x:p[x]=find(p[x]);}
void up(int x){
int l=c[x][],r=c[x][];
mx[x]=x;
if(v[mx[l]]>v[mx[x]]) mx[x]=mx[l];
if(v[mx[r]]>v[mx[x]]) mx[x]=mx[r];
}
void down(int x){
int l=c[x][],r=c[x][];
if(rev[x]){
rev[x]=; rev[l]^=; rev[r]^=;
swap(c[x][],c[x][]);
}
}
void rotate(int x){
int y=fa[x],z=fa[y],l=,r=;
if(c[y][]==x) l=,r=;
if(!isrt(y)) c[z][c[z][]==y]=x;
fa[y]=x; fa[x]=z; fa[c[x][r]]=y;
c[y][l]=c[x][r]; c[x][r]=y;
up(y); up(x);
}
int st[M],top;
void splay(int x){
st[++top]=x; for(int i=x;!isrt(i);i=fa[i]) st[++top]=fa[i];
while(top) down(st[top--]);
while(!isrt(x)){
int y=fa[x],z=fa[y];
if(!isrt(y)){
if(c[z][]==y^c[y][]==x) rotate(x);
else rotate(y);
}
rotate(x);
}
}
void acs(int x0){
for(int x=x0,y=;x;splay(x),c[x][]=y,up(x),y=x,x=fa[x]);
splay(x0);
}
void mrt(int x){acs(x); rev[x]^=;}
void link(int x,int y){mrt(x); fa[x]=y;}
void cut(int x,int y){mrt(x); acs(y); c[y][]=fa[x]=; up(y);}
int push_max(int x,int y){mrt(x); acs(y); return mx[y];}
int main()
{
n=read(); m=read();
for(int i=;i<=n;i++) p[i]=i;
for(int i=;i<=m;i++) e[i].u=read(),e[i].v=read(),e[i].a=read(),e[i].b=read();
sort(e+,e++m,cmp);
for(int i=;i<=m;i++){
int from=e[i].u,to=e[i].v,a=e[i].a,b=e[i].b;
if(find(from)==find(to)){
int now=push_max(from,to);
if(v[now]>b){cut(now,e[now-n].v); cut(now,e[now-n].u);}
else{
if(find()==find(n)) ans=min(ans,a+v[push_max(,n)]);
continue;
}
}
else p[find(from)]=find(to);
v[i+n]=b; mx[i+n]=i+n;
link(from,i+n); link(to,i+n);
if(find()==find(n)) ans=min(ans,a+v[push_max(,n)]);
}
if(ans==inf) printf("-1\n");
else printf("%d\n",ans);
return ;
}

bzoj3669: [Noi2014]魔法森林 lct版的更多相关文章

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

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

  2. bzoj3669: [Noi2014]魔法森林 lct

    记得去年模拟赛的时候好像YY出二分答案枚举a,b的暴力,过了55欸 然后看正解,为了将两维变成一维,将a排序,模拟Kruskal的加边过程,同时维护1到n的最大值,加入一条边e(u,v,a,b)时有以 ...

  3. [bzoj3669][Noi2014]魔法森林——lct

    Brief description 给定一个无向图,求从1到n的一条路径使得这条路径上最大的a和b最小. Algorithm Design 以下内容选自某HN神犇的blog 双瓶颈的最小生成树的感觉, ...

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

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

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

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

  6. [NOI2014]魔法森林 LCT

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

  7. loj2245 [NOI2014]魔法森林 LCT

    [NOI2014]魔法森林 链接 loj 思路 a排序,b做动态最小生成树. 把边拆成点就可以了. uoj98.也许lct复杂度写假了..越卡常,越慢 代码 #include <bits/std ...

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

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

  9. BZOJ3669[Noi2014]魔法森林——kruskal+LCT

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

随机推荐

  1. leetcode笔记--6 Add Digits

    question: Given a non-negative integer num, repeatedly add all its digits until the result has only ...

  2. innodb_index_stats

    mysql> select * from mysql.innodb_index_stats WHERE database_name='test' and table_name='recordsI ...

  3. Delphi实例之绘制正弦函数图像

    Delphi实例之绘制正弦函数图像 unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphic ...

  4. TortoiseGit小乌龟 git管理工具

    1.新建分支git远端新建分支: b001本地git目录:右击--TortoiseGit--获取(会获取到新建分支) 2.本地新建分支对应远端分支本地新建分支:b001 关联远端分支b001(之后工作 ...

  5. 讨伐Cucumber行为驱动

    Cucumber行为驱动,简称BDD,其核心思想是把自然语言转换成代码:但在敏捷开发的过程中,这种东西极大的束缚了测试人员的手脚,感觉它像封建时代的八股文,要遵守严格的韵律,反正我个人十分反感:就像在 ...

  6. Floatingip

    浮动IP相关功能点: 模块 功能 描述 备注 FloatingIP 创建浮动IP 指定带宽大小创建单个/多个浮动IP   指定子网.指定IP创建浮动IP   绑定浮动IP,修改带宽 绑定浮动IP到指定 ...

  7. Bitcoin-NG

    Bitcoin-NG,一个新的可扩展的区块链协议 Bitcoin-NG仅受限于网络的传输延时,它的带宽仅受限于个人节点的处理能力.通过将比特币的区块链操作分解为两部分来实现这个性能改善:首领选择(le ...

  8. 编译程序提示配置PKG_CONFIG_PATH

    http://blog.csdn.net/langeldep/article/details/6804331 在安装开源软件的过程中, 经常会碰到提示配置PKG_CONFIG_PATH路径, 或者直接 ...

  9. 【Python】Python—判断变量的基本类型

    type() >>> type(123)==type(456) True >>> type(123)==int True >>> type('ab ...

  10. Hibernate关联映射之_一对一

    数据库模型 一般对一对一的关系而言,会存在一个主从关系.如 人 与 身份证,就是一个一对一关系, 人 是主,身份证 是从 Person PK:id name age Id_Card PK.FK:id ...