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 ...
随机推荐
- max_element(C++)求数组最大元素
#include<iostream> #include<vector> #include<algorithm> using namespace std; int m ...
- table tr 加入背景色之后 去掉td之间的空隙
给table加样式 border-collapse:collapse;
- Emulating private methods with closures
[Emulating private methods with closures] JavaScript does not provide a native way of doing this, bu ...
- day25 面向对象之多态和鸭子类型
1.封装方法 如何封装:给方法名称前面加上双下划线 # ATM 的取款功能 # 1.插入银行卡 2.输入密码 3.选择取款金额 4.取款 class ATM: def __insert_card(se ...
- Python基础之列表及元组的增删改查
定义一个列表 晚上的状态不太适合写博客,容易犯困~ 列表的增加有三种方式 第一种主流的增加 append函数 list1 = ['noevil', 'fordearme', 'city', 'cust ...
- uWSGI和WSGI区别
uWSGI是一个Web服务器,它实现了WSGI协议.uwsgi.http等协议.Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换.WSGI是一种Web服务器网关接口.它是一 ...
- CentOS 下搭建Gitlab
centos7安装部署gitlab服务器 我这里使用的是centos 7 64bit,我试过centos 6也是可以的! 1. 安装依赖软件 yum -y install policycoreut ...
- Codeforces Beta Round #69 (Div. 2 Only)
Codeforces Beta Round #69 (Div. 2 Only) http://codeforces.com/contest/80 A #include<bits/stdc++.h ...
- SOA与微服务的区别
乍一看: 1.SOA更抽象. 2. SOA是拆分服务后,用ECS等手段,将服务组合调度. 微服务则是拆分服务后组合成各种业务. https://blog.csdn.net/HeatDeath/arti ...
- 用django实现一个资产管理的系统
整个页面的效果如下图所示 1.登陆注册页面 2.注册的页面 3.登陆页面 4.所有设备 5.正常设备页面 6.退库设备页面 7.丢失设备页面 8.导出设备的页面,仅仅在所有设备页面才支持导出按钮 9. ...