这题和水管局长很像,枚举 \(a\) 的边然后维护关于 \(b\) 的最小生成树就可以了。

1A呐>_<

#include <algorithm>
#include <iostream>
#include <cstdio>
using namespace std;
int n, m, val[150005], zdz[150005], ans=0x3f3f3f3f, fa[150005];
int ch[150005][2], rev[150005];
struct Edge{
int fro, too, vaa, vab;
}edge[100005];
bool cmp(Edge x, Edge y){
return x.vaa<y.vaa;
}
void upd(int x){
zdz[x] = val[x];
if(edge[zdz[ch[x][0]]].vab>edge[zdz[x]].vab)
zdz[x] = zdz[ch[x][0]];
if(edge[zdz[ch[x][1]]].vab>edge[zdz[x]].vab)
zdz[x] = zdz[ch[x][1]];
}
int getw(int x){
return ch[fa[x]][1]==x;
}
bool isroot(int x){
return ch[fa[x]][0]!=x && ch[fa[x]][1]!=x;
}
void pushdown(int x){
if(rev[x]){
swap(ch[x][0], ch[x][1]);
rev[ch[x][0]] ^= 1;
rev[ch[x][1]] ^= 1;
rev[x] = false;
}
}
void xf(int x){
if(fa[x]) xf(fa[x]);
pushdown(x);
}
void rotate(int x){
int old=fa[x], oldf=fa[old], w=getw(x);
if(!isroot(old)) ch[oldf][ch[oldf][1]==old] = x;
ch[old][w] = ch[x][w^1]; ch[x][w^1] = old;
fa[ch[old][w]] = old; fa[ch[x][w^1]] = x; fa[x] = oldf;
upd(old); upd(x);
}
void splay(int x){
xf(x);
while(!isroot(x)){
int f=fa[x];
if(!isroot(f)) rotate(getw(f)==getw(x)?f:x);
rotate(x);
}
upd(x);
}
void access(int x){
int y=0;
while(x){
splay(x);
ch[x][1] = y;
upd(x);
y = x;
x = fa[x];
}
}
void makeroot(int x){
access(x);
splay(x);
rev[x] ^= 1;
}
int findroot(int x){
access(x);
splay(x);
while(ch[x][0])
x = ch[x][0];
splay(x);
return x;
}
void split(int x, int y){
makeroot(x);
access(y);
splay(y);
}
void link(int x, int y){
makeroot(x);
fa[x] = y;
}
void cut(int x, int y){
split(x, y);
fa[x] = ch[y][0] = 0;
}
int main(){
cin>>n>>m;
for(int i=1; i<=m; i++){
scanf("%d %d %d %d", &edge[i].fro, &edge[i].too, &edge[i].vaa, &edge[i].vab);
if(edge[i].fro>edge[i].too) swap(edge[i].fro, edge[i].too);
}
for(int i=n+1; i<=n+m; i++)
val[i] = zdz[i] = i - n;
sort(edge+1, edge+1+m, cmp);
int j=1;
for(int i=1; i<=50000; i++){
for(; j<=m && edge[j].vaa<=i; j++){
int x=edge[j].fro, y=edge[j].too;
if(findroot(x)!=findroot(y)){
link(x, j+n);
link(y, j+n);
}
else{
split(x, y);
int idx=zdz[y];
if(edge[j].vab<edge[idx].vab){
cut(edge[idx].fro, idx+n);
cut(edge[idx].too, idx+n);
link(edge[j].fro, j+n);
link(edge[j].too, j+n);
}
}
}
if(findroot(1)==findroot(n)){
split(1, n);
ans = min(ans, i+edge[zdz[n]].vab);
}
}
if(ans!=0x3f3f3f3f) printf("%d\n", ans);
else printf("%d\n", -1);
return 0;
}

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

  1. 「luogu2387」[NOI2014] 魔法森林

    「luogu2387」[NOI2014] 魔法森林 题目大意 \(n\) 个点 \(m\) 条边的无向图,每条边上有两个权值 \(a,b\),求从 \(1\) 节点到 \(n\) 节点 \(max\{ ...

  2. NOI2014 魔法森林

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

  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]魔法森林

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

  6. BZOJ_3669_[Noi2014]魔法森林_LCT

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

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

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

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

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

  9. [NOI2014]魔法森林 LCT

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

随机推荐

  1. 【9.29 模拟】T3 小清新最优化(easy)

    [题目描述] 给出一个长度为 n 的序列,序列的每个元素为一个二元组,代表一种单目运算: • \((0,x)\): 对于一个数\(a\),将其变为 \(a\&x\).\((\&=x)\ ...

  2. Manacher算法模板

    题目描述 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度. 字符串长度为n 输入输出格式 输入格式: 一行小写英文字符a,b,c...y,z组成的字符串S 输出格 ...

  3. 安卓extends和implements

    extends是继承类 implements是实现接口

  4. 【luogu P4114 Qtree1】 题解

    题目链接:https://www.luogu.org/problemnew/show/P4114 1.把边权转化到点权:选取连接这条边的两个点中较深的一个. 2.查询点到点之间的边权时,要从seg[x ...

  5. c++ 单继承派生类的构造函数

    1.派生类的构造函数: #include <iostream> #include<string> using namespace std; class Student//声明基 ...

  6. visual assist x vs2012不智能提示

    今天装了visual assist x,但是在vs2012里不智能提示,在哪里看了看.找到了开关. 我英文不好.... 默认的话是不选中的.

  7. Python常用模块之time和datetime

    1.时间的格式化 结构化时间 ##把字符串时间转换成结构化时间 time.strptime("2017-06-21","%Y-%m-%d") ##把结构化时间转 ...

  8. ios应用数据存储方式(偏好设置)-转

    一.简单介绍 1.很多ios应用都支持偏好设置,比如保存用户名,密码,字体大小等设置,ios提供了一套标准的解决方案来为应用加入偏好设置功能. 2.每个应用都有个NSUserDefaults实例,通过 ...

  9. Windows获取物理内存的2种方式 - 随笔记录

    typedef enum _SYSTEM_INFORMATION_CLASS { SystemBasicInformation, SystemProcessorInformation, // obso ...

  10. linux系统基础之---RPM管理(基于centos7.4)