【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. MySQL创建用户与授权

    一. 创建用户 命令: CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 说明: username:你将创建的用户名 host:指定该用户 ...

  2. hdu4143 A Simple Problem

    A Simple Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) ...

  3. 变态的IE

    1.IE7及更早版本, unshift()方法总是返回undefined而不是数组的新长度.2.IE8及之前版本, 在catch语句中捕获的错误对象会被添加到执行环境的变量对象, 而不是catch语句 ...

  4. EmguCv“线段” 结构类型学习

    1. 文件所在 Namespace: Emgu.CV.Structure Assembly: Emgu.CV (in Emgu.CV.dll) Version: 3.0.0.2157 (3.0.0.2 ...

  5. 1014. Waiting in Line (模拟)

    n个窗口就有n个队列,模拟这n个队列就可以了.需要注意的是,一个人在选择排队窗口的时候,他会选择排队人数最少的窗口,如果存在多个窗口排队的人数相同,那么他会选择编码最小的窗口. Note that s ...

  6. HDU - 2187 贪心

    思路:  按照单价升序排序,依次买就行. AC代码 #include <cstdio> #include <cmath> #include <algorithm> ...

  7. hdu1425 哈希技术

    常用的技巧,把每个数字分别对应数组的下标,如果存在小于零的数字,就统一加一个数使得都能映射到一个下标上去. AC代码: #include<cstdio> #include<cstri ...

  8. day2(字符串、格式化输出、运算符、流程控制)

    一.字符串 在Python中,加了引号的字符都被认为是字符串! 单引号.双引号.多引号的区别? 单引号和 双引号没有任何区别,但是某种情况下需要单双配合 如 msg = " My name ...

  9. CameraLink通信接口的一般定义

    CameraLink是从Channel link技术上发展而来的,基于视频应用发展而来的通信接口,在机器视觉系统中广泛应用. 2000年10月美国NI.摄像头供应商和其他图像采集公司联合推出了Came ...

  10. Android可以拖动位置的ListVeiw

    参考网址: 1.https://github.com/bauerca/drag-sort-listview 2.http://www.tuicool.com/articles/jyA3MrU