UOJ 274 温暖会指引我们前进 - LCT
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的更多相关文章
- UOJ #274. 【清华集训2016】温暖会指引我们前行 [lct]
#274. [清华集训2016]温暖会指引我们前行 题意比较巧妙 裸lct维护最大生成树 #include <iostream> #include <cstdio> #incl ...
- [清华集训2016]温暖会指引我们前行——LCT+最大生成树
题目链接: [清华集训2016]温暖会指引我们前行 题目大意:有$n$个点$m$次操作,每次操作分为三种:1.在$u,v$两点之间连接一条编号为$id$,长度为$l$,温度为$t$的边.2.查询从$u ...
- 【UOJ274】【清华集训2016】温暖会指引我们前行 LCT
[UOJ274][清华集训2016]温暖会指引我们前行 任务描述 虽然小R住的宿舍楼早已来了暖气,但是由于某些原因,宿舍楼中的某些窗户仍然开着(例如厕所的窗户),这就使得宿舍楼中有一些路上的温度还是很 ...
- bzoj 4736 /uoj274【清华集训2016】温暖会指引我们前行 lct
[清华集训2016]温暖会指引我们前行 统计 描述 提交 自定义测试 寒冬又一次肆虐了北国大地 无情的北风穿透了人们御寒的衣物 可怜虫们在冬夜中发出无助的哀嚎 “冻死宝宝了!” 这时 远处的天边出现了 ...
- Uoj #274. 【清华集训2016】温暖会指引我们前行 LCT维护边权_动态最小生成树
Code: 行#include<bits/stdc++.h> #define ll long long #define maxn 1000000 #define inf 100000000 ...
- BZOJ 4736 温暖会指引我们前行 LCT+最优生成树+并查集
题目链接:http://uoj.ac/problem/274 题意概述: 没什么好概述的......概述了题意就知道怎么做了......我懒嘛 分析: 就是用lct维护最大生成树. 然后如果去UOJ上 ...
- [UOJ#274][清华集训2016]温暖会指引我们前行
[UOJ#274][清华集训2016]温暖会指引我们前行 试题描述 寒冬又一次肆虐了北国大地 无情的北风穿透了人们御寒的衣物 可怜虫们在冬夜中发出无助的哀嚎 “冻死宝宝了!” 这时 远处的天边出现了一 ...
- UOJ_274_[清华集训2016]温暖会指引我们前行_LCT
UOJ_274_[清华集训2016]温暖会指引我们前行_LCT 任务描述:http://uoj.ac/problem/274 本题中的字典序不同在于空串的字典序最大. 并且题中要求排序后字典序最大. ...
- 【BZOJ4736】温暖会指引我们前行(Link-Cut Tree)
[BZOJ4736]温暖会指引我们前行(Link-Cut Tree) ##题面 神TM题面是UOJ的 题解 LCT傻逼维护最大生成树 不会的可以去做一做魔法森林 #include<iostrea ...
随机推荐
- 基于docker的wekan部署
镜像地址: https://hub.docker.com/r/wekanteam/wekan/ wiki: https://github.com/wekan/wekan/wiki#Developmen ...
- 支付宝支付Java代码
支付宝调用流程 开发前的准备工作 配置应用网关 应用网关里面填写的值就是商户后台的异步回调地址.也就是在支付宝付完款之后,由支付宝调用商户,便于商户验证订单各信息和更新订单状态 授权回调地址 授权回调 ...
- Java 动态代理 两种实现方法
AOP的拦截功能是由java中的动态代理来实现的.说白了,就是在目标类的基础上增加切面逻辑,生成增强的目标类(该切面逻辑或者在目标类函数执行之前,或者目标类函数执行之后,或者在目标类函数抛出异常时候执 ...
- C#之代码优化
1.if和swith: 区别:1.if语句会执行多次判断,增加CPU的消耗,效率较低:switch只判断一次,效率较高 2.if表示的是一个范围,switch表示一个点 2.for和foreach f ...
- 【OpenGL】纹理(Texture)
纹理是一个2D图片(也有1D和3D),它用来添加物体的细节:这就像有一张绘有砖块的图片贴到你的3D的房子上,你的房子看起来就有了一个砖墙.因为我们可以在一张图片上插入足够多的细节,这样物体就会拥有很多 ...
- 2019年华南理工大学程序设计竞赛(春季赛)-H-Parco_Love_GCD
题目链接:https://ac.nowcoder.com/acm/contest/625/H 题意:给定n个数(<=1e9)的序列,其中n<=5e5,求该序列所有子序列的对应的gcd对1e ...
- CentOS常用的文件操作命令总结
我可以说是linux操作新手,有些命令经常忘记,特别是对文件的某些操作,经常要翻阅之前的笔记,今天把之前在百度上整理的“CentOS常用的文件操作命令”转载到我的新博客上面,以供后面查阅! 博客后面还 ...
- vcenter或workstation12导入ovf出错:硬件系列vmx 14不受支持
原因是因为导出ovf的虚拟机版本太高. 两个方法,一个强制,一个推荐. 强制 1. 打开ovf后缀文件,把<vssd:VirtualSystemType>vmx-14</vssd:V ...
- jquery--blur()事件,在页面加载时自动获取焦点
jquery--blur()事件会在页面加载时自动获取焦点,应将onblur写到html标签中 <div class="inputbox"> <input typ ...
- 联想G510安装win7系统
1.插入系统盘重启(要按Fn+f12) 2默认下一步 3.选择自定义 4.选择需要将系统安装到的分区,格式化,然后下一步 5.默认下一步 安装到这里就成功了 6.问题处理: 第4步时出现 Window ...