传送门

并查集真是一个判断连通的好东西!

连通性用并查集来搞。

把每一条边按照 a 为关键字从小到大排序。

那么直接枚举,动态维护 b 的最小生成树

用 a[i] + 1 ~ n 路径上最大的 b[i] 更新答案即可

——代码

 #include <cstdio>
#include <iostream>
#include <algorithm>
#define N 200005
#define get(x) (son[f[x]][1] == (x))
#define min(x, y) ((x) < (y) ? (x) : (y))
#define swap(x, y) ((x) ^= (y) ^= (x) ^= (y))
#define isroot(x) (son[f[x]][0] ^ (x) && son[f[x]][1] ^ (x)) int n, m, ans = ~( << );
int mx[N], rev[N], fa[N], f[N], val[N], son[N][], s[N]; struct node
{
int x, y, a, b;
}e[N]; inline int read()
{
int x = , f = ;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -;
for(; isdigit(ch); ch = getchar()) x = (x << ) + (x << ) + ch - '';
return x * f;
} inline bool cmp(node x, node y)
{
return x.a < y.a;
} inline int getf(int x)
{
return x == fa[x] ? x : fa[x] = getf(fa[x]);
} inline void update(int x)
{
if(x)
{
mx[x] = x;
if(son[x][] && val[mx[son[x][]]] > val[mx[x]]) mx[x] = mx[son[x][]];
if(son[x][] && val[mx[son[x][]]] > val[mx[x]]) mx[x] = mx[son[x][]];
}
} inline void pushdown(int x)
{
if(x && rev[x])
{
swap(son[x][], son[x][]);
if(son[x][]) rev[son[x][]] ^= ;
if(son[x][]) rev[son[x][]] ^= ;
rev[x] = ;
}
} inline void rotate(int x)
{
int old = f[x], oldf = f[old], wh = get(x); if(!isroot(old))
son[oldf][get(old)] = x;
f[x] = oldf; son[old][wh] = son[x][wh ^ ];
f[son[old][wh]] = old; son[x][wh ^ ] = old;
f[old] = x; update(old);
update(x);
} inline void splay(int x)
{
int i, fat, t = ;
s[++t] = x;
for(i = x; !isroot(i); i = f[i]) s[++t] = f[i];
for(i = t; i; i--) pushdown(s[i]);
for(; !isroot(x); rotate(x))
if(!isroot(fat = f[x]))
rotate(get(x) ^ get(fat) ? x : fat);
} inline void access(int x)
{
for(int t = ; x; t = x, x = f[x]) splay(x), son[x][] = t, update(x);
} inline void reverse(int x)
{
access(x);
splay(x);
rev[x] ^= ;
} inline void cut(int x, int y)
{
reverse(x);
access(y);
splay(y);
son[y][] = f[x] = ;
update(y);
} inline void link(int x, int y)
{
reverse(x);
f[x] = y;
access(x);
} inline int find(int x)
{
access(x);
splay(x);
while(son[x][]) x = son[x][];
return x;
} inline int query(int x, int y)
{
reverse(x);
access(y);
splay(y);
return mx[y];
} int main()
{
int i, j, k, x, y, a, b, t, fx, fy;
n = read();
m = read();
for(i = ; i <= n; i++) fa[i] = i;
for(i = ; i <= m; i++)
{
e[i].x = read();
e[i].y = read();
e[i].a = read();
e[i].b = read();
x = getf(e[i].x);
y = getf(e[i].y);
if(x ^ y) fa[x] = y;
}
if(getf() ^ getf(n))
{
puts("-1");
return ;
}
std::sort(e + , e + m + , cmp);
for(i = ; i <= m; i++)
{
mx[i + n] = i + n;
val[i + n] = e[i].b;
}
for(i = ; i <= n; i++) fa[i] = i;
for(i = ; i <= m; i++)
{
x = e[i].x;
y = e[i].y;
fx = getf(x);
fy = getf(y);
if(fx ^ fy)
{
link(x, i + n);
link(y, i + n);
fa[fx] = fy;
}
else
{
t = query(x, y);
if(e[i].b < val[t])
{
cut(e[t - n].x, t);
cut(e[t - n].y, t);
link(x, i + n);
link(y, i + n);
}
}
if(getf() == getf(n)) ans = min(ans, e[i].a + val[query(, n)]);
}
printf("%d\n", ans);
return ;
}

排序很关键!

如果做不出来,先排序试试。

[luoguP2387] 魔法森林(LCT + 并查集)的更多相关文章

  1. bzoj 3669: [Noi2014]魔法森林(并查集+LCT)

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

  2. [bzoj3669][Noi2014]魔法森林_LCT_并查集

    魔法森林 bzoj-3669 Noi-2014 题目大意:说不明白题意系列++……题目链接 注释:略. 想法:如果只有1个参量的话spfa.dij什么的都上来了. 两个参量的话我们考虑,想将所有的边按 ...

  3. 【bzoj2959】长跑 LCT+并查集

    题目描述 某校开展了同学们喜闻乐见的阳光长跑活动.为了能“为祖国健康工作五十年”,同学们纷纷离开寝室,离开教室,离开实验室,到操场参加3000米长跑运动.一时间操场上熙熙攘攘,摩肩接踵,盛况空前.为了 ...

  4. 【bzoj4998】星球联盟 LCT+并查集

    题目描述 在遥远的S星系中一共有N个星球,编号为1…N.其中的一些星球决定组成联盟,以方便相互间的交流.但是,组成联盟的首要条件就是交通条件.初始时,在这N个星球间有M条太空隧道.每条太空隧道连接两个 ...

  5. BZOJ 3669: [Noi2014]魔法森林 [LCT Kruskal | SPFA]

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

  6. BZOJ_2049_[Sdoi_2008]_Cave_洞穴勘测_(LCT/并查集)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=2049 给出一个森林,起始互不相连,现在有link和cut两种操作,问x,y是否在一棵树里. 分 ...

  7. [loj6038]「雅礼集训 2017 Day5」远行 lct+并查集

    给你 n 个点,支持 m 次操作,每次为以下两种:连一条边,保证连完后是一棵树/森林:询问一个点能到达的最远的点与该点的距离.强制在线. n≤3×10^5 n≤3×10^5 ,m≤5×10^5 m≤5 ...

  8. bzoj3669: [Noi2014]魔法森林 lct版

    先上题目 bzoj3669: [Noi2014]魔法森林 这道题首先每一条边都有一个a,b 我们按a从小到大排序 每次将一条路劲入队 当然这道题权在边上 所以我们将边化为点去连接他的两个端点 当然某两 ...

  9. 【bzoj4399】魔法少女LJJ 并查集+权值线段树合并

    题目描述 在森林中见过会动的树,在沙漠中见过会动的仙人掌过后,魔法少女LJJ已经觉得自己见过世界上的所有稀奇古怪的事情了LJJ感叹道“这里真是个迷人的绿色世界,空气清新.淡雅,到处散发着醉人的奶浆味: ...

随机推荐

  1. spring Cache /Redis 缓存 + Spring 的集成示例

    spring Cache https://www.ibm.com/developerworks/cn/opensource/os-cn-spring-cache/ spring+redis 缓存 ht ...

  2. 计算几何值平面扫面poj2932 Coneology

    Coneology Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4097   Accepted: 859 Descript ...

  3. 279 Perfect Squares 完美平方数

    给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...) 使得他们的和等于 n.你需要让平方数的个数最少.比如 n = 12,返回 3 ,因为 12 = 4 + 4 + 4 : ...

  4. Windows下重置MySQL密码【MYSQL】

    使用环境:win10 x64企业版 关闭正在运行的MySQL服务. 打开DOS窗口,转到mysqlbin目录. 输入mysqld --skip-grant-tables回车.如果没有出现提示信息,那就 ...

  5. IDEA 提示Cannot resolve symbol

    将Springboot项目,构建为多个模块的时候,idea的有些类中报错Cannot resolve symbol 解决: 1.File->Invalidate Caches/Restart 清 ...

  6. 第一次创建svn的项目的使用方法

    1.第一步.在服务器上创建svn项目,将开发人人员你的账号密码添加上去. 2.第二步.开始在本地创建一个文件夹,点文件夹,右键->tortoisSVN->repo-brower 填写svn ...

  7. 【译】x86程序员手册29-第8章 输入输出

    Chapter 8 Input/Output 输入/输出 This chapter presents the I/O features of the 80386 from the following ...

  8. jQuery 遍历 - children() 方法

    jQuery 遍历参考手册 实例 找到类名为 "selected" 的所有 div 的子元素,并将其设置为蓝色: $("div").children(" ...

  9. SpringBoot+Mybatis 自动创建数据表(适用mysql)

    Mybatis用了快两年了,在我手上的发展史大概是这样的 第一个阶段 利用Mybatis-Generator自动生成实体类.DAO接口和Mapping映射文件.那时候觉得这个特别好用,大概的过程是这样 ...

  10. WindowsForms获取服务名称

    StringBuilder sb = new StringBuilder(); ServiceController[] services = ServiceController.GetServices ...