题目链接:

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

题目大意:有$n$个点$m$次操作,每次操作分为三种:1、在$u,v$两点之间连接一条编号为$id$,长度为$l$,温度为$t$的边。2、查询从$u$到$v$的最温暖的路径长度(定义最温暖的路径为将路径上的边按温度从小到大排序后字典序尽可能大)。3、将编号为$id$的边长度修改为$l$。

仔细读题发现题目中说的字典序其实就是使路径上的边都尽可能大。

那么最优方案一定就是走最大生成树上的边咯。

用LCT动态维护最大生成树。

当新加入边两端点不连通时直接连接,否则找到两点路径上的最小边,如果当前边比最小边大,那么将最小边删除并加入当前边。

在LCT上维护边的信息不好做,我们将每个边看成一个点。

例如编号为a的边连接x,y,那么就将a+n这个新建点分别连向x,y。

splay需要维护最小点权,点权和及最小点权点编号。

注意要将所有非边点的点权置成INF。

注意在find之后要将原树根旋到它所在splay的根,否则在uoj会被卡。

#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<bitset>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pr pair<int,int>
#define ll long long
using namespace std;
char ch[10];
int n,m;
int x,y,z,u,v;
struct miku
{
int x,y;
}a[600010];
int s[800010][2];
int mx[800010];
int mn[800010];
int r[800010];
int f[800010];
int w[800010];
int st[800010];
int sum[800010];
int t[800010];
int is_root(int rt)
{
return rt!=s[f[rt]][0]&&rt!=s[f[rt]][1];
}
int get(int rt)
{
return rt==s[f[rt]][1];
}
void pushup(int rt)
{
sum[rt]=sum[s[rt][0]]+sum[s[rt][1]]+w[rt];
mx[rt]=min(t[rt],min(mx[s[rt][0]],mx[s[rt][1]]));
if(mx[rt]==t[rt])
{
mn[rt]=rt;
}
else if(mx[rt]==mx[s[rt][0]])
{
mn[rt]=mn[s[rt][0]];
}
else
{
mn[rt]=mn[s[rt][1]];
}
}
void pushdown(int rt)
{
if(r[rt])
{
swap(s[rt][0],s[rt][1]);
r[s[rt][0]]^=1;
r[s[rt][1]]^=1;
r[rt]^=1;
}
}
void rotate(int rt)
{
int fa=f[rt];
int anc=f[fa];
int k=get(rt);
if(!is_root(fa))
{
s[anc][get(fa)]=rt;
}
s[fa][k]=s[rt][k^1];
f[s[fa][k]]=fa;
s[rt][k^1]=fa;
f[fa]=rt;
f[rt]=anc;
pushup(fa);
pushup(rt);
}
void splay(int rt)
{
int top=0;
st[++top]=rt;
for(int i=rt;!is_root(i);i=f[i])
{
st[++top]=f[i];
}
for(int i=top;i>=1;i--)
{
pushdown(st[i]);
}
for(int fa;!is_root(rt);rotate(rt))
{
if(!is_root(fa=f[rt]))
{
rotate(get(fa)==get(rt)?fa:rt);
}
}
}
void access(int rt)
{
for(int x=0;rt;x=rt,rt=f[rt])
{
splay(rt);
s[rt][1]=x;
pushup(rt);
}
}
void reverse(int rt)
{
access(rt);
splay(rt);
r[rt]^=1;
}
int find(int rt)
{
access(rt);
splay(rt);
while(s[rt][0])
{
rt=s[rt][0];
}
splay(rt);
return rt;
}
void link(int x,int y)
{
reverse(x);
f[x]=y;
}
void cut(int x,int y)
{
reverse(x);
access(y);
splay(y);
if(s[x][1]||f[x]!=y)
{
return ;
}
f[x]=s[y][0]=0;
pushup(y);
}
void change(int rt,int x)
{
w[rt]=x;
access(rt);
splay(rt);
}
int query(int x,int y)
{
reverse(x);
access(y);
splay(y);
return mn[y];
}
int ask(int x,int y)
{
reverse(x);
access(y);
splay(y);
return sum[y];
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=0;i<=n;i++)
{
t[i]=1000000007;
mx[i]=1000000007;
mn[i]=i;
w[i]=0;
sum[i]=0;
}
while(m--)
{
scanf("%s",ch);
if(ch[0]=='f')
{
scanf("%d%d%d%d%d",&x,&u,&v,&y,&z);
u++;
v++;
x++;
a[x].x=u;
a[x].y=v;
t[n+x]=y;
w[n+x]=z;
sum[n+x]=z;
mn[n+x]=n+x;
if(find(u)==find(v))
{
int id=query(u,v);
if(y>mx[id])
{
cut(id,a[id-n].x);
cut(id,a[id-n].y);
link(n+x,u);
link(n+x,v);
}
}
else
{
link(n+x,u);
link(n+x,v);
}
}
else if(ch[0]=='c')
{
scanf("%d%d",&x,&y);
x++;
change(n+x,y);
}
else
{
scanf("%d%d",&u,&v);
u++;
v++;
if(find(u)==find(v))
{
printf("%d\n",ask(u,v));
}
else
{
printf("-1\n");
}
}
}
}

[清华集训2016]温暖会指引我们前行——LCT+最大生成树的更多相关文章

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

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

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

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

  3. UOJ274 [清华集训2016] 温暖会指引我们前行 【LCT】【最大生成树】

    题目分析: 差评,最大生成树裸题.hack数据还卡常. 代码: #include<bits/stdc++.h> using namespace std; ; struct LCT{ ],d ...

  4. 【bzoj4736/uoj#274】[清华集训2016]温暖会指引我们前行 语文题+LCT

    题目描述 http://uoj.ac/problem/274 题解 语文题+LCT 对于这种语文题建议还是自己读题好一些... 读懂题后发现:由于温度互不相同,最大生成树上的路径必须走(不走的话温度大 ...

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

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

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

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

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

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

  8. Uoj #274. 【清华集训2016】温暖会指引我们前行 LCT维护边权_动态最小生成树

    Code: 行#include<bits/stdc++.h> #define ll long long #define maxn 1000000 #define inf 100000000 ...

  9. BZOJ 4736 温暖会指引我们前行 LCT+最优生成树+并查集

    题目链接:http://uoj.ac/problem/274 题意概述: 没什么好概述的......概述了题意就知道怎么做了......我懒嘛 分析: 就是用lct维护最大生成树. 然后如果去UOJ上 ...

随机推荐

  1. 第一篇 jQuery

    1-1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3. ...

  2. luogu题解 UVA534 【Frogger--最小瓶颈边

    题目链接: https://www.luogu.org/problemnew/show/UVA534 Update 6.18 多点对最短瓶颈路算法:https://www.cnblogs.com/Ry ...

  3. luogu P2154 [SDOI2009]虔诚的墓主人

    luogu 下面记一个点上下左右点数分别为\(u_i,d_i,l_i,r_i\) 枚举每个中间点太慢了,考虑枚举两个点之间横的一条线段,这里面的点左边点数目都相同,右边点数目都相同,然后只要查一下区间 ...

  4. springboot的一些注解

    springboot注解以及一些晦涩难理解的点介绍 @Validated 用于注入数值校验的注解(JSR303数据校验) @PropertySource 用于加载指定的配置文件,例如@Property ...

  5. CentOS7部署kettle

    去官网下载kettle, 或者百度网盘下载(nnnk),解压到目录/opt/service/, 解压后的目录是data-integration kettle需要java环境才能运行,因此要安装Java ...

  6. countUp.js-数字滚动效果(简单基础使用)

    最近写了个移动端宣传页,里面有数字的效果,所以有使用到countUp.js. 以下内容有包括:h5页面countUp.js的引入和实例.参数说明.事件简单使用和描述.countUp.js源代码. 附上 ...

  7. LVS Director端服务启动脚本

    #!/bin/bash # 手动安装lpvs前端管理工具 # chkconfig: - # # lvs启动脚本:director # lvs模式类型:nat.dr.ipip # lvs代理协议:tcp ...

  8. LInux安装MySQL5.7.24详情

    安装包下载 MySQL 的官网下载地址:http://www.mysql.com/downloads/ 我安装的是5.7版本 第二步: 选择:TAR (mysql-5.7.24-el7-x86_64. ...

  9. MobileNet系列

    最近一段时间,重新研读了谷歌的mobilenet系列,对该系列有新的认识. 1.MobileNet V1 这篇论文是谷歌在2017年提出了,专注于移动端或者嵌入式设备中的轻量级CNN网络.该论文最大的 ...

  10. CenOS7秘钥双向验证的配置

    配置密钥对的双向配置 HOST1配置: root下编辑/etc/ssh/sshd_config  RSAAuthentication  yes              //启用RSA算法 Pubke ...