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 ... 
随机推荐
- Javascript概念
			什么是JavaScript? 1 以前的目的:验证表单输入的正确性. 2 现在的目的:多做一些页面的交互效果. 3 javascript是一个跨平台的脚本语言. 4 网景公司开发的,由布兰登·艾奇最先 ... 
- java工程师基础笔试题(一)
			一.选择和填空 (不定项哦!) 1,如下是一份文件名为Test2.java的源文件,请问,编译该文件之后会生成几份字节码文件 class Test{ class Inner{} static cla ... 
- metasploit framework(十):SSH扫描、爆破
			SSH版本扫描 SSH密码爆破 设置爆破字典 run开始 
- 三、Template 模板模式
			需求:有规格的尺子,不管何种笔写,写出的字大小.形状都是一样的?抽象为处理流程一致,仅仅是不同的实现 代码清单: 抽象类: public abstract class AbstractDisplay{ ... 
- js实现简单拖拽效果
			方法如下: var params = { left: 0, top: 0, currentX: 0, currentY: 0, flag: false }; var getCss = function ... 
- pta7-19打印学生选课清单(模拟)
			题目链接:https://pintia.cn/problem-sets/1101307589335527424/problems/1101314114875633664 题意:输入n个学生,k門课程, ... 
- c#  键值对的方式post提交
			DataContractJsonSerializer jsQcData = new DataContractJsonSerializer(typeof(DATA<data>));//DAT ... 
- oracle数据库查询出多条数据,合并,之后列转行
			select B.enterprise_code, B.enterprise_name, sum(B.h0_overnum) AS over00, sum(B.h1_overnum) AS over0 ... 
- [剑指offer]51-数组中的逆序对(归并排序)
			题目链接 https://www.nowcoder.com/questionTerminal/96bd6684e04a44eb80e6a68efc0ec6c5 题意 在数组中的两个数字,如果前面一个数 ... 
- [LeetCode_98]Validate Binary Search Tree
			题目链接 https://leetcode.com/problems/validate-binary-search-tree/ 题意 判断给定树是否是BST 思路 根据定义判断.递归. 代码 clas ... 
