LCT维护生成树

先按照a的权值把边排序,离线维护b的最小生成树。

将a排序后,依次动态加边,我们只需要关注b的值。要保证1-n花费最少,两点间的b值肯定是越小越好,所以我们可以考虑以b为关键字维护最小生成树。

对于新加的边b,如果1-n已经联通,需要更新答案

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
int X = 0, w = 0; char ch = 0;
while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
return w ? -X : X;
}
inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
A ans = 1;
for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
return ans;
}
const int N = 400005;
int n, m, tot, ans, mx[N], fa[N], w[N], ch[N][2], rev[N], id[N], st[N];
struct Edge {
int v, u, a, b;
bool operator < (const Edge &rhs) const {
return a < rhs.a;
}
} e[N]; int newNode(int v){
++tot;
w[tot] = mx[tot] = v, id[tot] = tot;
fa[tot] = ch[tot][0] = ch[tot][1] = 0;
return tot;
} bool isRoot(int x){
return ch[fa[x]][0] != x && ch[fa[x]][1] != x;
} void reverse(int x){
rev[x] ^= 1, swap(ch[x][0], ch[x][1]);
} void push_up(int x){
int l = ch[x][0], r = ch[x][1];
mx[x] = w[x], id[x] = x;
if(mx[l] > mx[x]) mx[x] = mx[l], id[x] = id[l];
if(mx[r] > mx[x]) mx[x] = mx[r], id[x] = id[r];
} void push_down(int x){
if(rev[x]){
rev[x] ^= 1;
if(ch[x][0]) reverse(ch[x][0]);
if(ch[x][1]) reverse(ch[x][1]);
}
} void rotate(int x){
int y = fa[x], z = fa[y], p = (ch[y][1] == x) ^ 1;
ch[y][p^1] = ch[x][p], fa[ch[x][p]] = y;
if(!isRoot(y)) ch[z][ch[z][1] == y] = x;
fa[x] = z, fa[y] = x, ch[x][p] = y;
push_up(y), push_up(x);
} void splay(int x){
int pos = 0; st[++pos] = x;
for(int i = x; !isRoot(i); i = fa[i]) st[++pos] = fa[i];
while(pos) push_down(st[pos--]);
while(!isRoot(x)){
int y = fa[x], z = fa[y];
if(!isRoot(y)) (ch[y][0] == x) ^ (ch[z][0] == y) ? rotate(x) : rotate(y);
rotate(x);
}
push_up(x);
} void access(int x){
for(int p = 0; x; p = x, x = fa[x])
splay(x), ch[x][1] = p, push_up(x);
} void makeRoot(int x){
access(x), splay(x), reverse(x);
} void link(int x, int y){
makeRoot(x);
fa[x] = y;
} int findRoot(int x){
access(x), splay(x);
while(ch[x][0]) push_down(x), x = ch[x][0];
splay(x);
return x;
} void split(int x, int y){
makeRoot(x), access(y), splay(y);
} bool isConnect(int x, int y){
makeRoot(x);
return findRoot(y) == x;
} int main(){ ans = INF;
n = read(), m = read();
for(int i = 1; i <= n; i ++) newNode(0);
for(int i = 0; i < m; i ++){
e[i].u = read(), e[i].v = read(), e[i].a = read(), e[i].b = read();
}
sort(e, e + m);
for(int i = 0; i < m; i ++){
int u = e[i].u, v = e[i].v, t = newNode(e[i].b);
if(!isConnect(u, v)) link(u, t), link(t, v);
else{
split(u, v);
if(e[i].b > mx[v]) continue;
int tmp = id[v]; splay(tmp);
fa[ch[tmp][0]] = fa[ch[tmp][1]] = 0;
ch[tmp][0] = ch[tmp][1] = 0;
link(u, t), link(t, v);
}
if(isConnect(1, n)){
split(1, n);
ans = min(ans, mx[n] + e[i].a);
}
}
printf(ans == INF ? "-1\n" : "%d\n", ans);
return 0;
}

最新文章

  1. eclipse调试
  2. 【转】《从入门到精通云服务器》第七讲—负载均衡和CDN技术
  3. 使用MapReduce实现join操作
  4. MyBatis学习--逆向工程
  5. leetcode 102. Binary Tree Level Order Traversal
  6. iOS OAuth2.0认证和SSO授权
  7. DOM浏览器文档模型
  8. 实战Django:官方实例Part1
  9. [转载]数据库存储图片(MSSQL/ORACLE/ACCESS
  10. 【暑假】[实用数据结构]UVAlive 3942 Remember the Word
  11. delphi xe5 android 使用样式(风格)
  12. linux,Centos,bash: service: command not found
  13. 2016/9/7 jdbc.properties配置数据库相关
  14. PHP 异常处理
  15. Javascript设计模式之装饰者模式详解篇
  16. html标记语言 --格式标记
  17. Python———pandas数据处理
  18. CLion之C++框架篇-优化开源框架,引入curl,实现get方式获取资源(四)
  19. non-ZenoAndAcceptingLocation
  20. IDA反汇编学习

热门文章

  1. 从头认识一下docker-附带asp.net core程序的docker化部署
  2. Linux 环境下 Git 安装与基本配置
  3. PJSUA2开发文档--第八章 好友(Buddy)类
  4. PHAR系列之导言
  5. Linux中删除特殊名称文件的多种方式
  6. Editplus5.0 注册码
  7. Navicat Premium 12.0.18安装与激活
  8. Linux:Day12(上) 进程
  9. 【js】this问题
  10. 手把手教你发布一个Python包