Solution

更新掉路径上温暖度最小的边就可以了~

Code

 #include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
#define rd read()
using namespace std; const int N = 4e5 + ;
const int inf = ~0U >> ; int n, m, fa[N]; struct edge {
int u, v, t, w;
}e[N]; int read() {
int X = , p = ; char c = getchar();
for (; c > '' || c < ''; c = getchar())
if (c == '-') p = -;
for (; c >= '' && c <= ''; c = getchar())
X = X * + c - '';
return X * p;
} int anc(int x) {
return fa[x] == x ? x : fa[x] = anc(fa[x]);
} namespace LCT {
int f[N], ch[N][], val[N], mn[N], tun[N];
ll sum[N];
#define lc(x) ch[x][0]
#define rc(x) ch[x][1] int isroot(int x) {
return lc(f[x]) != x && rc(f[x]) != x;
} int get(int x) {
return rc(f[x]) == x;
} void up(int x) {
mn[x] = val[x];
sum[x] = e[val[x]].t;
if (e[mn[lc(x)]].w < e[mn[x]].w) mn[x] = mn[lc(x)];
if (e[mn[rc(x)]].w < e[mn[x]].w) mn[x] = mn[rc(x)];
if (lc(x)) sum[x] += sum[lc(x)];
if (rc(x)) sum[x] += sum[rc(x)];
} void rev(int x) {
swap(lc(x), rc(x));
tun[x] ^= ;
} void pushdown(int x) {
if (tun[x]) {
if (lc(x)) rev(lc(x));
if (rc(x)) rev(rc(x));
tun[x] = ;
}
} int st[N], tp; void pd(int x) {
while (!isroot(x)) {
st[++tp] = x;
x = f[x];
}
pushdown(x);
while (tp) pushdown(st[tp--]);
} void rotate(int x) {
int old = f[x], oldf = f[old], son = ch[x][get(x) ^ ];
if (!isroot(old)) ch[oldf][get(old)] = x;
ch[x][get(x) ^ ] = old;
ch[old][get(x)] = son;
f[old] = x; f[x] = oldf; f[son] = old;
up(old); up(x);
} void splay(int x) {
pd(x);
for (; !isroot(x); rotate(x))
if (!isroot(f[x]))
rotate(get(f[x]) == get(x) ? f[x] : x);
} void access(int x) {
for (int y = ; x; y = x, x = f[x])
splay(x), ch[x][] = y, up(x);
} int findr(int x) {
access(x); splay(x);
while (lc(x)) pushdown(x), x = lc(x);
return x;
} void mroot(int x) {
access(x); splay(x); rev(x);
} void split(int x, int y) {
mroot(x); access(y); splay(y);
} void link(int x, int y) {
mroot(x);
f[x] = y;
} void cut(int x, int y) {
split(x, y);
f[x] = ch[y][] = ;
}
}
using namespace LCT; int main()
{
e[].w = inf;
n = rd; m = rd;
for (int i = ; i <= n; ++i)
fa[i] = i;
for (int i = ; i <= m; ++i)
val[i + n] = i;
char op[];
for (int i = ; i <= m; ++i) {
scanf("%s", op);
if (op[] == 'f') {
int id = rd + ;
e[id].u = rd + ;
e[id].v = rd + ;
e[id].w = rd;
e[id].t = rd;
int x = e[id].u, y = e[id].v;
if (anc(x) != anc(y)) {
link(id + n, x);
link(id + n, y);
x = anc(x); y = anc(y);
fa[x] = y;
}
else {
split(x, y);
int t = mn[y];
if (e[id].w < e[t].w)
continue;
cut(t + n, e[t].u);
cut(t + n, e[t].v);
link(id + n, e[id].u);
link(id + n, e[id].v);
}
}
else if(op[] == 'm') {
int x = rd + , y = rd + ;
if (anc(x) != anc(y)) {
puts("-1"); continue;
}
if (x == y) {puts(""); continue;}
split(x, y);
printf("%lld\n", sum[y]);
}
else {
int id = rd + ;
e[id].t = rd;
mroot(id + n);
}
}
}

UOJ 274 温暖会指引我们前进 - LCT的更多相关文章

  1. UOJ #274. 【清华集训2016】温暖会指引我们前行 [lct]

    #274. [清华集训2016]温暖会指引我们前行 题意比较巧妙 裸lct维护最大生成树 #include <iostream> #include <cstdio> #incl ...

  2. [清华集训2016]温暖会指引我们前行——LCT+最大生成树

    题目链接: [清华集训2016]温暖会指引我们前行 题目大意:有$n$个点$m$次操作,每次操作分为三种:1.在$u,v$两点之间连接一条编号为$id$,长度为$l$,温度为$t$的边.2.查询从$u ...

  3. 【UOJ274】【清华集训2016】温暖会指引我们前行 LCT

    [UOJ274][清华集训2016]温暖会指引我们前行 任务描述 虽然小R住的宿舍楼早已来了暖气,但是由于某些原因,宿舍楼中的某些窗户仍然开着(例如厕所的窗户),这就使得宿舍楼中有一些路上的温度还是很 ...

  4. bzoj 4736 /uoj274【清华集训2016】温暖会指引我们前行 lct

    [清华集训2016]温暖会指引我们前行 统计 描述 提交 自定义测试 寒冬又一次肆虐了北国大地 无情的北风穿透了人们御寒的衣物 可怜虫们在冬夜中发出无助的哀嚎 “冻死宝宝了!” 这时 远处的天边出现了 ...

  5. Uoj #274. 【清华集训2016】温暖会指引我们前行 LCT维护边权_动态最小生成树

    Code: 行#include<bits/stdc++.h> #define ll long long #define maxn 1000000 #define inf 100000000 ...

  6. BZOJ 4736 温暖会指引我们前行 LCT+最优生成树+并查集

    题目链接:http://uoj.ac/problem/274 题意概述: 没什么好概述的......概述了题意就知道怎么做了......我懒嘛 分析: 就是用lct维护最大生成树. 然后如果去UOJ上 ...

  7. [UOJ#274][清华集训2016]温暖会指引我们前行

    [UOJ#274][清华集训2016]温暖会指引我们前行 试题描述 寒冬又一次肆虐了北国大地 无情的北风穿透了人们御寒的衣物 可怜虫们在冬夜中发出无助的哀嚎 “冻死宝宝了!” 这时 远处的天边出现了一 ...

  8. UOJ_274_[清华集训2016]温暖会指引我们前行_LCT

    UOJ_274_[清华集训2016]温暖会指引我们前行_LCT 任务描述:http://uoj.ac/problem/274 本题中的字典序不同在于空串的字典序最大. 并且题中要求排序后字典序最大. ...

  9. 【BZOJ4736】温暖会指引我们前行(Link-Cut Tree)

    [BZOJ4736]温暖会指引我们前行(Link-Cut Tree) ##题面 神TM题面是UOJ的 题解 LCT傻逼维护最大生成树 不会的可以去做一做魔法森林 #include<iostrea ...

随机推荐

  1. JAVA 实验报告

        石家庄铁道大学信息科学与技术学院       实验报告 2018年----2019年  第一学期               题目:   四则运算.生成验证码 课程名称:  JAVA语言程序设 ...

  2. go语言指针判等

    https://blog.csdn.net/qq_26981997/article/details/52608081

  3. rectangle,boundingRect和Rect

    rectangle( rook_image, Point( , *w/8.0 ), Point( w, w), Scalar( , , ), , ); 矩形将被画到图像 rook_image 上 矩形 ...

  4. 04_web基础(八)之车票实现增删改查初级版本

    43.web页面显示车票列表简略完成 代码: 控制层代码 package com.day03.station.controller; import com.day03.station.model.Ti ...

  5. flex-direction

    [flex-direction] The flex-direction CSS property specifies how flex items are placed in the flex con ...

  6. package.json bin

    [package.json bin] 1.bin field in your package.json which is a map of command name to local file nam ...

  7. WeakHashMap<K,V> 中的弱引用

    相信很多人对WeakHashMap并没有完全理解. WeakHashMap 持有的弱引用的 Key. 1. 弱引用的概念: 弱引用是用来描述非必需对象的,被弱引用关联的对象只能生存到下一次垃圾收集发生 ...

  8. Django 的认识,题型

    Django 的认识,面试题 链接:https://www.cnblogs.com/chongdongxiaoyu/p/9403399.html 1. 对Django的认识? #1.Django是走大 ...

  9. redismyadmin安装(支持redis4 集群模式)

    yum install php-pecl-redis https://github.com/daivem/RedisMyAdmin下载最新的安装包,解压yum install nginx php ph ...

  10. KNN算法应用

    import numpy as np# 运算符模块,这里主要用来排序 import operator import matplotlib.pylab as plt def create_dataset ...