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

##题面

神TM题面是UOJ的

题解

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)的更多相关文章

  1. [BZOJ4736]温暖会指引我们前行

    BZOJ(BZOJ上的是什么鬼...) UOJ 任务描述 虽然小R住的宿舍楼早已来了暖气,但是由于某些原因,宿舍楼中的某些窗户仍然开着(例如厕所的窗户),这就使得宿舍楼中有一些路上的温度还是很低. 小 ...

  2. BZOJ4736 温暖会指引我们前行(LCT+最大生成树)

    类似于瓶颈路,满足条件的路径一定在温度的最大生成树上,那么就是一个LCT维护MST的裸题了. #include<iostream> #include<cstdio> #incl ...

  3. Link Cut Tree 总结

    Link-Cut-Tree Tags:数据结构 ##更好阅读体验:https://www.zybuluo.com/xzyxzy/note/1027479 一.概述 \(LCT\),动态树的一种,又可以 ...

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. markdown语法探究

    \[\sum_{i=1}^n a_i=0\] \[f(x_1,x_x,\ldots,x_n) = x_1^2 + x_2^2 + \cdots + x_n^2 \] \[\sum^{j-1}_{k=0 ...

  2. JS工厂模式开发实践

    JS工厂模式开发实践 基于JS工厂模式的H5应用,实现了轮播图功能与滑屏功能,并且实现了文字大小的自适应功能,基于SASS样式开发. 核心的JS代码如下: index.js define(functi ...

  3. linux 中 svn 服务器搭建 重启

    鉴于在搭建时,参考网上很多资料,网上资料在有用的同时,也坑了很多人 本文的目的,也就是想让后继之人在搭建svn服务器时不再犯错,不再被网上漫天的坑爹作品所坑害,故此总结 /******开始****** ...

  4. thinkPHP替换SQL变量

    使用tp里M()->where(pb_id=%d and course=%d and DATE_FORMAT(pub_time, \"%H:%i:%s\") < &qu ...

  5. 用VSCode开发一个基于asp.net core 2.0/sql server linux(docker)/ng5/bs4的项目(2)

    第一部分: http://www.cnblogs.com/cgzl/p/8478993.html 为Domain Model添加约束 前一部分, 我们已经把数据库创建出来了. 那么我们先看看这个数据库 ...

  6. Mac下使用SSH(密钥)访问Github

    1,终端中输入:cd ~/.ssh 如果出现 -bash: cd: /Users/glamor/.ssh: No such file or directory,说明你之前没有用过.直接执行第二步. 如 ...

  7. 老男孩Python全栈开发(92天全)视频教程 自学笔记18

    day18课程内容: os模块 import osprint(os.getcwd())#D:\untitled\练习题 获取当前工作目录os.chdir(r'D:\untitled\练习题\16.1切 ...

  8. 如何学习 MFC ?

    //std::string => CString std::string srcString = "Hello World!"; CString dstString = CS ...

  9. Android虚拟机安装

    由于虫师那边的源估计到期了,我又找了一波. 打开SDK Manager.exe, 就在安卓目录下. 点击Tools--Options进入配置页面 mirrors.neusoft.edu.cn 配置如下 ...

  10. tcp/ip 卷一 读书笔记(3)为什么既要有IP地址又要有MAC地址

    网络层 首先明确一点,并不是所有的网络之间传输数据都需要mac地址和ip地址,比如说点对点线路之间的通信就没有MAC地址,网络层使用ipx协议时就没有ip地址,但是在当前的主流网络中,我们都使用ip地 ...