思想基本同bzoj 2594,但是多了一步

首先我们发现这时的边有两个属性了,因此我们考虑先去掉其中一者的限制

我们把所有边按$a$大小排序,然后从小到大加入维护的最小生成树

每次加边时都按照$b$的大小操作bzoj 2594,然后更新答案即可

如果始终不联通输出-1

#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
using namespace std;
struct Ques
{
int x,y,typ,num;
}q[1000005];
struct Edge
{
int l,r,v1,v2;
friend bool operator < (Edge a,Edge b)
{
return a.v1==b.v1?a.v2<b.v2:a.v1<b.v1;
}
}edge[1000005];
map <pair<int,int>,int> M;
int n,m,Q;
int ch[2000005][2];
int vis[2000005];
int maxx[2000005];
int val[2000005];
int f[2000005];
int fl[2000005];
int ret[2000005];
void update(int x)
{
maxx[x]=val[x];
if(edge[maxx[ch[x][0]]].v2>edge[maxx[x]].v2)maxx[x]=maxx[ch[x][0]];
if(edge[maxx[ch[x][1]]].v2>edge[maxx[x]].v2)maxx[x]=maxx[ch[x][1]];
}
bool be_root(int x)
{
if(ch[f[x]][0]==x||ch[f[x]][1]==x)return 0;
return 1;
}
void pushdown(int x)
{
if(fl[x])
{
swap(ch[ch[x][0]][0],ch[ch[x][0]][1]);
swap(ch[ch[x][1]][0],ch[ch[x][1]][1]);
fl[ch[x][0]]^=1,fl[ch[x][1]]^=1;
fl[x]=0;
}
}
void repush(int x)
{
if(!be_root(x))repush(f[x]);
pushdown(x);
}
void rotate(int x)
{
int y=f[x],z=f[y],k=(ch[y][1]==x);
if(!be_root(y))ch[z][ch[z][1]==y]=x;
f[x]=z;
ch[y][k]=ch[x][!k],f[ch[x][!k]]=y;
ch[x][!k]=y,f[y]=x;
update(y),update(x);
}
void splay(int x)
{
repush(x);
while(!be_root(x)&&x)
{
int y=f[x],z=f[y];
if(!be_root(y)&&y)
{
if((ch[y][1]==x)^(ch[z][1]==y))rotate(x);
else rotate(y);
}
rotate(x);
}
update(x);
}
void access(int x)
{
int y=0;
while(x)
{
splay(x);
ch[x][1]=y;
update(x);
y=x,x=f[x];
}
}
void makeroot(int x)
{
access(x),splay(x);
swap(ch[x][0],ch[x][1]),fl[x]^=1;
}
void link(int x,int y)
{
makeroot(x);
f[x]=y;
}
void split(int x,int y)
{
makeroot(x),access(y),splay(y);
}
void cut(int x,int y)
{
split(x,y),ch[y][0]=f[x]=0,update(y);
}
int findf(int x)
{
access(x),splay(x),pushdown(x);
while(ch[x][0])x=ch[x][0],pushdown(x);
return x;
}
inline int read()
{
int f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int main()
{
n=read(),m=read();
for(int i=1;i<=m;i++)
{
edge[i].l=read(),edge[i].r=read(),edge[i].v1=read(),edge[i].v2=read();
if(edge[i].l>edge[i].r)swap(edge[i].l,edge[i].r);
}
sort(edge+1,edge+m+1);
int ans=0x3f3f3f3f;
for(int i=1;i<=m;i++)val[i+n]=maxx[i+n]=i;
for(int i=1;i<=m;i++)
{
int f1=findf(edge[i].l),f2=findf(edge[i].r);
if(f1==f2)
{
split(edge[i].l,edge[i].r);
if(edge[maxx[edge[i].r]].v2>edge[i].v2)
{
int t=maxx[edge[i].r];
cut(edge[t].l,t+n),cut(edge[t].r,t+n);
link(edge[i].l,i+n),link(edge[i].r,i+n);
}
}else link(edge[i].l,i+n),link(edge[i].r,i+n);
int ff1=findf(1),ff2=findf(n);
if(ff1==ff2)
{
split(1,n);
ans=min(ans,edge[i].v1+edge[maxx[n]].v2);
}
}
if(ans==0x3f3f3f3f)printf("-1\n");
else printf("%d\n",ans);
return 0;
}

bzoj 3669的更多相关文章

  1. [BZOJ 3669] [Noi2014] 魔法森林 【LCT】

    题目链接:BZOJ - 3669 题目分析 如果确定了带 x 只精灵A,那么我们就是要找一条 1 到 n 的路径,满足只经过 Ai <= x 的边,而且要使经过的边中最大的 Bi 尽量小. 其实 ...

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

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

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

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

  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. bzoj 3669 lct维护最小生成树

    大概题意:给一个无向图,有a,b两种边权,找一条从1到n的路径,使得max(a[i])+max(b[i])最小a[i],b[i]表示该路径上的边的对应权. 如果用类似最短路的DP来做,显然每个点的状态 ...

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

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

  8. 【BZOJ 3669】 3669: [Noi2014]魔法森林 (动态spfa)

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

  9. BZOJ 3669 [Noi2014]魔法森林(贪心+LCT)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3669 [题目大意] 给出一张图,每条边上有两个值ai和bi,现在需要求出一条1到N的路 ...

  10. 洛谷 2387/BZOJ 3669 魔法森林

    3669: [Noi2014]魔法森林 Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 3765  Solved: 2402[Submit][Statu ...

随机推荐

  1. desginer启动就直接卡死

    博主经验: 请不要开有道词典    请不要开有道词典         请不要开有道词典

  2. 快速上手SpringBoot

    快速上手SpringBoot SpringBoot是用来简化Spring应用的初始化搭建以及开发过程 三个不需要,这是springboot使用mvc区别于其它框架的特点 tomcatd的端口 下一行是 ...

  3. vite实现element-plus按需配置,自定义主题和读取/修改系统主题色

    项目地址 vite.config.ts 插件和vite配置 import { defineConfig } from "vite"; import vue from "@ ...

  4. Nmon 监控分析工具使用

    一.简介 1.nmon是一种在AIX与各种Linux操作系统上广泛使用的监控与分析工具,它能在系统运行过程中实时地捕捉系统资源的使用情况,记录的信息比较全面, 并且能输出结果到文件中,然后通过nmon ...

  5. webpack的核心概念

    一.entry 指定webpack从哪个文件开始入手打包,下面是单入口 多页面开发,每个页面都有自己的js文件,多个文件,所以需要多个入口 我们看看多入口,单出口bundle.js,我们在src仅仅多 ...

  6. 6.mysql优化案例

    1.单表优化:  进行优化: 删除原来的三个字段的索引,创建二个字段的索引: 2.两表关联: 左连接,在右表创建索引   右连接,在左表创建索引: 3.三表关联:左关联全都在右表创建索引:

  7. PS 查看进行状态

    原文:https://blog.csdn.net/lyndon_li/article/details/114295654 ps 查看进行状态有如下几种: ... PROCESS STATE CODES ...

  8. idea插件Tranlation配置有道搜索引擎

    idea配置有道翻译引擎 一.更换翻译引擎原因 由于Google在2022年9月末宣布关闭GoogleTranslate在中国的服务,原本在chrome浏览器和idea上使用的google翻译引擎也不 ...

  9. sql常用记录

    sqlserver 在已有值的列上自动增加 获取列最大的值 declare @Field int select @Field = ISNULL(Max(Field),0) from SupCsBill ...

  10. docker tomcat 环境构建

    docker build -t repos_local/centos-jdk7-tomcat7:0.0.1 . -t 设置tag名称, 命名规则registry/image:tag . 表示使用当前目 ...