BZOJ4736 温暖会指引我们前行(LCT+最大生成树)
类似于瓶颈路,满足条件的路径一定在温度的最大生成树上,那么就是一个LCT维护MST的裸题了。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define N 100010
#define M 300010
#define inf 1000000001
#define lson tree[k].ch[0]
#define rson tree[k].ch[1]
#define lself tree[tree[k].fa].ch[0]
#define rself tree[tree[k].fa].ch[1]
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<''||c>'')) c=getchar();return c;}
int gcd(int n,int m){return m==?n:gcd(m,n%m);}
int read()
{
int x=,f=;char c=getchar();
while (c<''||c>'') {if (c=='-') f=-;c=getchar();}
while (c>=''&&c<='') x=(x<<)+(x<<)+(c^),c=getchar();
return x*f;
}
int n,m,cnt,w[N+M][],id[M],temp[N+M];
struct data{int ch[],fa,rev,len,sum,id;
}tree[N+M];
void up(int k)
{
tree[k].sum=tree[lson].sum+tree[rson].sum+tree[k].len;
tree[k].id=k;
if (temp[tree[lson].id]<temp[tree[k].id]) tree[k].id=tree[lson].id;
if (temp[tree[rson].id]<temp[tree[k].id]) tree[k].id=tree[rson].id;
}
void rev(int k){if (k) swap(lson,rson),tree[k].rev^=;}
void down(int k){if (tree[k].rev) rev(lson),rev(rson),tree[k].rev=;}
bool isroot(int k){return lself!=k&&rself!=k;}
int whichson(int k){return rself==k;}
void push(int k){if (!isroot(k)) push(tree[k].fa);down(k);}
void move(int k)
{
int fa=tree[k].fa,gf=tree[fa].fa,p=whichson(k);
if (!isroot(fa)) tree[gf].ch[whichson(fa)]=k;tree[k].fa=gf;
tree[fa].ch[p]=tree[k].ch[!p],tree[tree[k].ch[!p]].fa=fa;
tree[k].ch[!p]=fa,tree[fa].fa=k;
up(fa),up(k);
}
void splay(int k)
{
push(k);
while (!isroot(k))
{
int fa=tree[k].fa;
if (!isroot(fa))
if (whichson(k)^whichson(fa)) move(k);
else move(fa);
move(k);
}
}
void access(int k){for (int t=;k;t=k,k=tree[k].fa) splay(k),tree[k].ch[]=t,up(k);}
int findroot(int k){access(k),splay(k);for (;lson;k=lson) down(k);splay(k);return k;}
void makeroot(int k){access(k),splay(k),rev(k);}
void link(int x,int y){makeroot(x),tree[x].fa=y;}
void cut(int x,int y){makeroot(x),access(y),splay(y),tree[y].ch[]=tree[x].fa=,up(y);}
int queryid(int x,int y){makeroot(x),access(y),splay(y);return tree[y].id;}
int querylen(int x,int y){makeroot(x),access(y),splay(y);return tree[y].sum;}
int main()
{
#ifndef ONLINE_JUDGE
freopen("bzoj4736.in","r",stdin);
freopen("bzoj4736.out","w",stdout);
const char LL[]="%I64d\n";
#else
const char LL[]="%lld\n";
#endif
n=read(),m=read();
for (int i=;i<=n;i++) temp[i]=inf,tree[i].id=i;cnt=n;
for (int i=;i<=m;i++)
{
char c=getc();
if (c=='f')
{
int u=read(),x=read()+,y=read()+,t=read(),l=read();
if (findroot(x)==findroot(y))
{
int p=queryid(x,y);
if (temp[p]>=t) continue;
cut(p,w[p][]),cut(p,w[p][]);
w[p][]=w[p][]=;
}
id[u]=++cnt;temp[id[u]]=t,tree[id[u]].len=tree[id[u]].sum=l,tree[id[u]].id=id[u],w[id[u]][]=x,w[id[u]][]=y;
link(id[u],x),link(id[u],y);
}
if (c=='m')
{
int x=read()+,y=read()+;
if (findroot(x)!=findroot(y)) printf("-1\n");
else printf("%d\n",querylen(x,y));
}
if (c=='c')
{
int u=read(),l=read();
if (w[id[u]][])
{
cut(id[u],w[id[u]][]),cut(id[u],w[id[u]][]);
tree[id[u]].len=tree[id[u]].sum=l;
link(id[u],w[id[u]][]),link(id[u],w[id[u]][]);
}
}
}
return ;
}
BZOJ4736 温暖会指引我们前行(LCT+最大生成树)的更多相关文章
- [清华集训2016]温暖会指引我们前行——LCT+最大生成树
题目链接: [清华集训2016]温暖会指引我们前行 题目大意:有$n$个点$m$次操作,每次操作分为三种:1.在$u,v$两点之间连接一条编号为$id$,长度为$l$,温度为$t$的边.2.查询从$u ...
- UOJ #274. 【清华集训2016】温暖会指引我们前行 [lct]
#274. [清华集训2016]温暖会指引我们前行 题意比较巧妙 裸lct维护最大生成树 #include <iostream> #include <cstdio> #incl ...
- 【UOJ274】【清华集训2016】温暖会指引我们前行 LCT
[UOJ274][清华集训2016]温暖会指引我们前行 任务描述 虽然小R住的宿舍楼早已来了暖气,但是由于某些原因,宿舍楼中的某些窗户仍然开着(例如厕所的窗户),这就使得宿舍楼中有一些路上的温度还是很 ...
- bzoj 4736 /uoj274【清华集训2016】温暖会指引我们前行 lct
[清华集训2016]温暖会指引我们前行 统计 描述 提交 自定义测试 寒冬又一次肆虐了北国大地 无情的北风穿透了人们御寒的衣物 可怜虫们在冬夜中发出无助的哀嚎 “冻死宝宝了!” 这时 远处的天边出现了 ...
- [BZOJ4736]温暖会指引我们前行
BZOJ(BZOJ上的是什么鬼...) UOJ 任务描述 虽然小R住的宿舍楼早已来了暖气,但是由于某些原因,宿舍楼中的某些窗户仍然开着(例如厕所的窗户),这就使得宿舍楼中有一些路上的温度还是很低. 小 ...
- BZOJ 4736 温暖会指引我们前行 LCT+最优生成树+并查集
题目链接:http://uoj.ac/problem/274 题意概述: 没什么好概述的......概述了题意就知道怎么做了......我懒嘛 分析: 就是用lct维护最大生成树. 然后如果去UOJ上 ...
- Uoj #274. 【清华集训2016】温暖会指引我们前行 LCT维护边权_动态最小生成树
Code: 行#include<bits/stdc++.h> #define ll long long #define maxn 1000000 #define inf 100000000 ...
- 【BZOJ4736】温暖会指引我们前行(Link-Cut Tree)
[BZOJ4736]温暖会指引我们前行(Link-Cut Tree) ##题面 神TM题面是UOJ的 题解 LCT傻逼维护最大生成树 不会的可以去做一做魔法森林 #include<iostrea ...
- UOJ_274_[清华集训2016]温暖会指引我们前行_LCT
UOJ_274_[清华集训2016]温暖会指引我们前行_LCT 任务描述:http://uoj.ac/problem/274 本题中的字典序不同在于空串的字典序最大. 并且题中要求排序后字典序最大. ...
随机推荐
- 成都Uber优步司机奖励政策(3月24日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- Android stdio build.gradle buildscript 里面的repositories 和allprojects里面 repositories 的区别
第一段 buildscript 里面的 repositories 表示只有编译工具才会用这个仓库. 比如 buildscript 里面的 dependencies classpath 'com.and ...
- netty之粘包分包的处理
1.netty在进行字节数组传输的时候,会出现粘包和分包的情况.当个数据还好,如果数据量很大.并且不间断的发送给服务器,这个时候就会出现粘包和分包的情况. 2.简单来说:channelBuffer在接 ...
- Redis系列七 主从复制(Master/Slave)
主从复制(Master/Slave) 1.是什么 也就是我们所说的主从复制,主机数据更新后根据配置和策略,自动同步到备机的master/slaver机制,Master以写为主,Slave以读为主. 2 ...
- sql注入--高权限,load_file读写文件
select '<?php eval($_POST[123]) ?>' into outfile '/var/www/html/1.php'; 1.MYSQL新特性限制文件写入及替代方法 ...
- Linux命令应用大词典-第12章 程序编译
12.1 gcc:GNU项目的C和C++编译器 12.2 gdberver:为GNU调试的远程服务器 12.3 cmake:跨平台的Makefile生成工具 12.4 indent:更改通过插入或删除 ...
- Python零基础入门必知
Python自学知识点总结 //2018.10.09 1. Python(英国发音:/ˈpaɪθən/ 美国发音:/ˈpaɪθɑːn/), 是一种面向对象的解释型计算机程序设计语言,由荷兰人Guido ...
- 珍珠 Median Weight Bead 977
描述 There are N beads which of the same shape and size, but with different weights. N is an odd numbe ...
- CryptoZombies学习笔记——Lesson3
第三课就开始深入讲解solidity编程技巧了. chapter1: 智能合约的不变性. 合约一旦部署到以太坊后,就不可更改了,所以从一方面来说,智能合约代码的安全性是如此重要,因为一旦发现你的代码里 ...
- POJ 3608 Bridge Across Islands(计算几何の旋转卡壳)
Description Thousands of thousands years ago there was a small kingdom located in the middle of the ...