这题和水管局长很像,枚举 \(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. PCI PCI-X PCI-E介绍

    1.PCI 外设互联标准(或称个人电脑接口,Personal Computer Interface),实际应用中简称PCI(Peripheral Component Interconnect),是一种 ...

  2. 郑州集训Day4 [小Cat与小鲜肉]

    考试的时候由于没有想出这道题就弃疗了 发现主要还是自己姿势不够 [问题描述] \(P\) 校某宿舍人才辈出,其舍长图书馆男神因被偷拍侧身照而在网络上一票走红. 小鲜肉 \(SJY\) 是小 \(Cat ...

  3. [18/11/20]break与continue的区别

    一.普通break 和continue 1.break: break用于强行退出循环,不执行循环中剩余的语句. 2.continue continue 语句用在循环语句体中,用于终止某次循环过程,即跳 ...

  4. 检查WIFI是否连接

    查看网络连接 查看WiFi连接状态 (连接- -断开)

  5. 显示Windows版本号

    实现效果: 知识运用: PaintDesktopVersion键 实现代码: private void button1_Click(object sender, EventArgs e) { Regi ...

  6. Reverse Polish notation

    Reverse Polish notation is a notation where every operator follows all of its operands. For example, ...

  7. 【iOS】那些年,遇到的小坑

    'NSInvalidArgumentException', reason: '-[__NSPlaceholderDictionary initWithObjectsAndKeys:]: second ...

  8. QBXT Day 2 记录

    例题1:乌龟棋 略 例题2: noip2015 子串 有两个仅包含小写英文字母的字符串 A 和 B. 现在要从字符串 A 中取出 k 个互不重叠的非空子串,然后把这 k 个子串按照其在字符串 A 中出 ...

  9. 【luogu P2936 [USACO09JAN]全流Total Flow】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2936 菜 #include <queue> #include <cstdio> #i ...

  10. 【luogu P3808 AC自动机(简单版)】 模板

    题目链接:https://www.luogu.org/problemnew/show/P3808 #include <queue> #include <cstdio> #inc ...