【BZOJ4736】温暖会指引我们前行(Link-Cut Tree)
【BZOJ4736】温暖会指引我们前行(Link-Cut Tree)
##题面
题解
LCT傻逼维护最大生成树
不会的可以去做一做魔法森林
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define MAX 700000
#define lson (t[x].ch[0])
#define rson (t[x].ch[1])
#define rg register
inline int read()
{
rg int x=0,t=1;rg char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return x*t;
}
struct Node
{
int ch[2],ff;
int rev;
int sum,ma,v,r;
}t[MAX];
int S[MAX],top,n,m;
void pushup(int x)
{
t[x].sum=t[lson].sum+t[rson].sum+t[x].v;
t[x].ma=(t[t[lson].ma].r>t[t[rson].ma].r)?t[rson].ma:t[lson].ma;
if(t[x].r<t[t[x].ma].r)t[x].ma=x;
}
inline bool isroot(int x){return t[t[x].ff].ch[0]!=x&&t[t[x].ff].ch[1]!=x;}
inline void rotate(int x)
{
rg int y=t[x].ff,z=t[y].ff;
rg int k=t[y].ch[1]==x;
if(!isroot(y))t[z].ch[t[z].ch[1]==y]=x;t[x].ff=z;
t[y].ch[k]=t[x].ch[k^1];t[t[x].ch[k^1]].ff=y;
t[x].ch[k^1]=y;t[y].ff=x;
pushup(y);pushup(x);
}
inline void pushdown(int x)
{
if(!t[x].rev)return;
t[lson].rev^=1;t[rson].rev^=1;
t[x].rev^=1;
swap(lson,rson);
}
void Splay(int x)
{
S[top=1]=x;
for(int i=x;!isroot(i);i=t[i].ff)S[++top]=t[i].ff;
while(top)pushdown(S[top--]);
while(!isroot(x))
{
int y=t[x].ff,z=t[y].ff;
if(!isroot(y))
(t[y].ch[1]==x)^(t[z].ch[1]==y)?rotate(x):rotate(y);
rotate(x);
}
}
void access(int x){for(int y=0;x;y=x,x=t[x].ff)Splay(x),t[x].ch[1]=y,pushup(x);}
void makeroot(int x){access(x);Splay(x);t[x].rev^=1;}
void split(int x,int y){makeroot(x);access(y);Splay(y);}
void link(int x,int y){makeroot(x);t[x].ff=y;}
void cut(int x,int y){split(x,y);t[x].ff=t[y].ch[0]=0;pushup(y);}
int findroot(int x){access(x);Splay(x);while(lson)x=lson;return x;}
int main()
{
n=read();m=read();
for(rg int i=0;i<=n;++i)t[i].r=1e9;
rg char ch[10];
while(m--)
{
scanf("%s",ch);
if(ch[0]=='f')
{
rg int id=read()+1,u=read()+1,v=read()+1,tmp=read(),len=read();
t[id+n].r=tmp;t[id+n].v=len;
if(findroot(u)!=findroot(v))link(u,id+n),link(v,id+n);//未联通
else
{
split(u,v);
if(t[t[v].ma].r<tmp)
{
rg int ss=t[v].ma;
cut(u,ss);cut(v,ss);
link(u,id+n);link(v,id+n);
}
}
}
else if(ch[0]=='m')
{
rg int u=read()+1,v=read()+1;
if(findroot(u)!=findroot(v))puts("-1");
else split(u,v),printf("%d\n",t[v].sum);
}
else
{
rg int id=read()+1,len=read();
makeroot(id+n);t[id+n].v=len;pushup(id+n);
}
}
return 0;
}
【BZOJ4736】温暖会指引我们前行(Link-Cut Tree)的更多相关文章
- [BZOJ4736]温暖会指引我们前行
BZOJ(BZOJ上的是什么鬼...) UOJ 任务描述 虽然小R住的宿舍楼早已来了暖气,但是由于某些原因,宿舍楼中的某些窗户仍然开着(例如厕所的窗户),这就使得宿舍楼中有一些路上的温度还是很低. 小 ...
- BZOJ4736 温暖会指引我们前行(LCT+最大生成树)
类似于瓶颈路,满足条件的路径一定在温度的最大生成树上,那么就是一个LCT维护MST的裸题了. #include<iostream> #include<cstdio> #incl ...
- Link Cut Tree 总结
Link-Cut-Tree Tags:数据结构 ##更好阅读体验:https://www.zybuluo.com/xzyxzy/note/1027479 一.概述 \(LCT\),动态树的一种,又可以 ...
- UOJ #274. 【清华集训2016】温暖会指引我们前行 [lct]
#274. [清华集训2016]温暖会指引我们前行 题意比较巧妙 裸lct维护最大生成树 #include <iostream> #include <cstdio> #incl ...
- UOJ_274_[清华集训2016]温暖会指引我们前行_LCT
UOJ_274_[清华集训2016]温暖会指引我们前行_LCT 任务描述:http://uoj.ac/problem/274 本题中的字典序不同在于空串的字典序最大. 并且题中要求排序后字典序最大. ...
- 【UOJ274】【清华集训2016】温暖会指引我们前行 LCT
[UOJ274][清华集训2016]温暖会指引我们前行 任务描述 虽然小R住的宿舍楼早已来了暖气,但是由于某些原因,宿舍楼中的某些窗户仍然开着(例如厕所的窗户),这就使得宿舍楼中有一些路上的温度还是很 ...
- [UOJ#274][清华集训2016]温暖会指引我们前行
[UOJ#274][清华集训2016]温暖会指引我们前行 试题描述 寒冬又一次肆虐了北国大地 无情的北风穿透了人们御寒的衣物 可怜虫们在冬夜中发出无助的哀嚎 “冻死宝宝了!” 这时 远处的天边出现了一 ...
- bzoj 4736 /uoj274【清华集训2016】温暖会指引我们前行 lct
[清华集训2016]温暖会指引我们前行 统计 描述 提交 自定义测试 寒冬又一次肆虐了北国大地 无情的北风穿透了人们御寒的衣物 可怜虫们在冬夜中发出无助的哀嚎 “冻死宝宝了!” 这时 远处的天边出现了 ...
- [清华集训2016]温暖会指引我们前行——LCT+最大生成树
题目链接: [清华集训2016]温暖会指引我们前行 题目大意:有$n$个点$m$次操作,每次操作分为三种:1.在$u,v$两点之间连接一条编号为$id$,长度为$l$,温度为$t$的边.2.查询从$u ...
随机推荐
- Atom Mac安装 有快捷方式
https://jeffjade.com/2016/03/03/2016-03-02-how-to-use-atom/ 如何在 PyCharm 中使用 MacDown 作为外部编辑器 新编码神器Ato ...
- IDEA的导包优化问题
一.现象 文件初始导包状态 package co.x.dw.function; import java.text.SimpleDateFormat; import java.util.ArrayLis ...
- Vi/Vim的快捷方式
1 vi/ vim键盘图 2 文字解说 进入编辑模式的6种方式: i在光标前插入 I在行首插入 a在光标后插入 A在行末插入 o在下一行插入 O在上一行插入 删除字符 x 删除当前字符 X 删除前一个 ...
- 用Python发送邮件
文件:send.py # -*- coding:utf-8 -*- # ## 任兴测试用Python发送邮件 import os import sys import getopt import tim ...
- nginx+tomcat单个域名及多个域名配置
同步首发:http://www.yuanrengu.com/index.php/20171130.html 项目开发接近尾声,开始着手在生产环境部署项目,开发阶段部署项目都没用nginx.项目是采用S ...
- centos6.9 开机进入grub界面问题解决
安装系统时没把之前的磁盘分区格式化,导致安装新系统grub冲突 解决办法:删除当前磁盘分区,重新安装系统
- web自动化一(selenium+python+pycharm环境搭建)
年前公司刚刚搭起了web自动化测试框架的环境,趁着过完年还没全部忘掉,准备把如何搭建环境的方法和大家分享下,有哪里不对的地方,请批评指正,共同进步,共勉! 为此我把搭建环境所需的软件打包上传到百度云, ...
- Jenkins系列——定时构建
1.环境说明 操作系统:win7旗舰版64bit jdk:sun JDK1.7.0_80 64bit tomcat:apache-tomcat-8.0.41 jenkins:2.32.3LST 本系列 ...
- 3.2 while 循环
Python 编程中 while 语句用于循环执行程序,即在条件满足的情况下,循环执行某段代码.所以就需要在循环的代码块中设计一种使代码块循环执行一定次数后是while语句的条件不满足,从而中止whi ...
- poj2635 同余定理 + 素数筛法
题意:给定一个数,这个数是两个素数的乘积,并给定一个限制L,问是否两个素数中存在小于L的数,若存在输出较小质数,否则打印'GOOD'. 思路: 1 . x = a * b, a和b都是素数,那么x只能 ...