【BZOJ4736】温暖会指引我们前行(LCT)
题意:有一张图,每条边有一个不同的编号,长度和权值,维护以下操作:
1.加边
2.修改边长
3.询问两点之间在最小权值最大的前提下的唯一路径长度
n<=100000 m<=300000
思路:RYZ作业
BZOJ上有四组数据的输入不完整,输出没问题
LCT维护最大生成树,维护子树和,和子树中权值最小的位置即可
var t:array[..,..]of longint;
sum:array[..]of int64;
f:array[..,..]of longint;
w,l1,mx,fa,q,rev:array[..]of longint;
n,m,x,y,top,tot,id,i,len,s,ll,tt,j,tmp,now:longint;
ch:string; procedure swap(var x,y:longint);
var t:longint;
begin
t:=x; x:=y; y:=t;
end; function isroot(x:longint):boolean;
begin
if (t[fa[x],]<>x)and(t[fa[x],]<>x) then exit(true);
exit(false);
end; procedure pushup(x:longint);
var l,r:longint;
begin
l:=t[x,]; r:=t[x,];
sum[x]:=sum[l]+sum[r]+l1[x];
mx[x]:=x;
if w[mx[l]]<w[mx[x]] then mx[x]:=mx[l];
if w[mx[r]]<w[mx[x]] then mx[x]:=mx[r];
end; procedure pushdown(x:longint);
var l,r:longint;
begin
l:=t[x,]; r:=t[x,];
if rev[x]> then
begin
rev[x]:=rev[x] xor ; rev[l]:=rev[l] xor ; rev[r]:=rev[r] xor ;
swap(t[x,],t[x,]);
end;
end; procedure rotate(x:longint);
var y,z,l,r:longint;
begin
y:=fa[x]; z:=fa[y];
if t[y,]=x then l:=
else l:=;
r:=l xor ;
if not isroot(y) then
begin
if t[z,]=y then t[z,]:=x
else t[z,]:=x;
end;
fa[y]:=x; fa[x]:=z; fa[t[x,r]]:=y;
t[y,l]:=t[x,r]; t[x,r]:=y;
pushup(y);
pushup(x);
end; procedure splay(x:longint);
var k,y,z:longint;
begin
inc(top); q[top]:=x;
k:=x;
while not isroot(k) do
begin
inc(top); q[top]:=fa[k];
k:=fa[k];
end;
while top> do
begin
pushdown(q[top]);
dec(top);
end; while not isroot(x) do
begin
y:=fa[x]; z:=fa[x];
if not isroot(y) then
begin
if (t[y,]=x)xor(t[z,]=y) then rotate(x)
else rotate(y);
end;
rotate(x);
end;
end; procedure access(x:longint);
var last:longint;
begin
last:=;
while x> do
begin
splay(x); t[x,]:=last; pushup(x);
last:=x; x:=fa[x];
end;
end; procedure makeroot(x:longint);
begin
access(x); splay(x); rev[x]:=rev[x] xor ;
end; procedure link(x,y:longint);
begin
makeroot(x); fa[x]:=y;
end; procedure split(x,y:longint);
begin
makeroot(x); access(y); splay(y);
end; procedure cut(x,y:longint);
begin
makeroot(x); access(y); splay(y); t[y,]:=; fa[x]:=;
end; function findroot(x:longint):longint;
var k:longint;
begin
access(x); splay(x);
k:=x;
while t[k,]<> do k:=t[k,];
exit(k);
end; begin
assign(input,'bzoj4736.in'); reset(input);
assign(output,'bzoj4736.out'); rewrite(output);
readln(n,m);
fillchar(w,sizeof(w),$7f);
//fillchar(l,sizeof(l),$1f);
for i:= to m do
begin
readln(ch); s:=; id:=; x:=; y:=; tt:=; ll:=;
len:=length(ch);
case ch[] of
'f':
begin
for j:= to len do
begin
if ch[j]=' ' then begin inc(s); continue; end;
tmp:=ord(ch[j])-ord('');
case s of
:id:=id*+tmp;
:x:=x*+tmp;
:y:=y*+tmp;
:tt:=tt*+tmp;
:ll:=ll*+tmp;
end;
end;
inc(x); inc(y); inc(id);
tot:=id+;
w[tot]:=tt; l1[tot]:=ll; f[tot,]:=x; f[tot,]:=y;
if findroot(x)<>findroot(y) then
begin
mx[tot]:=tot;
link(x,tot); link(tot,y);
end
else
begin
split(x,y);
now:=mx[y];
if w[now]<tt then
begin
cut(f[now,],now); cut(now,f[now,]);
mx[tot]:=tot;
link(x,tot); link(tot,y);
end;
end; end;
'm':
begin
for j:= to len do
begin
if ch[j]=' ' then begin inc(s); continue; end;
tmp:=ord(ch[j])-ord('');
case s of
:x:=x*+tmp;
:y:=y*+tmp;
end;
end;
inc(x); inc(y);
if findroot(x)<>findroot(y) then writeln(-)
else
begin
split(x,y); writeln(sum[y]);
end;
end;
'c':
begin
for j:= to len do
begin
if ch[j]=' ' then begin inc(s); continue; end;
tmp:=ord(ch[j])-ord('');
case s of
:id:=id*+tmp;
:ll:=ll*+tmp;
end;
end;
inc(id); tot:=id+;
splay(tot);
l1[tot]:=ll;
pushup(tot);
end;
end;
end;
close(input);
close(output);
end.
2017.3.9
突然发现前一个代码splay打错了一个字母导致退化成类似单旋的东西
神TM单旋也能过
var t:array[..,..]of longint;
sum:array[..]of int64;
f:array[..,..]of longint;
w,l1,mx,fa,q,rev:array[..]of longint;
n,m,x,y,top,tot,id,i,len,s,ll,tt,j,tmp,now:longint;
ch:string; procedure swap(var x,y:longint);
var t:longint;
begin
t:=x; x:=y; y:=t;
end; function isroot(x:longint):boolean;
begin
if (t[fa[x],]<>x)and(t[fa[x],]<>x) then exit(true);
exit(false);
end; procedure pushup(x:longint);
var l,r:longint;
begin
l:=t[x,]; r:=t[x,];
sum[x]:=sum[l]+sum[r]+l1[x];
mx[x]:=x;
if w[mx[l]]<w[mx[x]] then mx[x]:=mx[l];
if w[mx[r]]<w[mx[x]] then mx[x]:=mx[r];
end; procedure pushdown(x:longint);
var l,r:longint;
begin
l:=t[x,]; r:=t[x,];
if rev[x]> then
begin
rev[x]:=rev[x] xor ; rev[l]:=rev[l] xor ; rev[r]:=rev[r] xor ;
swap(t[x,],t[x,]);
end;
end; procedure rotate(x:longint);
var y,z,l,r:longint;
begin
y:=fa[x]; z:=fa[y];
if t[y,]=x then l:=
else l:=;
r:=l xor ;
if not isroot(y) then
begin
if t[z,]=y then t[z,]:=x
else t[z,]:=x;
end;
fa[y]:=x; fa[x]:=z; fa[t[x,r]]:=y;
t[y,l]:=t[x,r]; t[x,r]:=y;
pushup(y);
pushup(x);
end; procedure splay(x:longint);
var k,y,z:longint;
begin
inc(top); q[top]:=x;
k:=x;
while not isroot(k) do
begin
inc(top); q[top]:=fa[k];
k:=fa[k];
end;
while top> do
begin
pushdown(q[top]);
dec(top);
end; while not isroot(x) do
begin
y:=fa[x]; z:=fa[y];
if not isroot(y) then
begin
if (t[y,]=x)xor(t[z,]=y) then rotate(x)
else rotate(y);
end;
rotate(x);
end;
end; procedure access(x:longint);
var last:longint;
begin
last:=;
while x> do
begin
splay(x); t[x,]:=last; pushup(x);
last:=x; x:=fa[x];
end;
end; procedure makeroot(x:longint);
begin
access(x); splay(x); rev[x]:=rev[x] xor ;
end; procedure link(x,y:longint);
begin
makeroot(x); fa[x]:=y;
end; procedure split(x,y:longint);
begin
makeroot(x); access(y); splay(y);
end; procedure cut(x,y:longint);
begin
makeroot(x); access(y); splay(y); t[y,]:=; fa[x]:=;
end; function findroot(x:longint):longint;
var k:longint;
begin
access(x); splay(x);
k:=x;
while t[k,]<> do k:=t[k,];
exit(k);
end; begin
assign(input,'bzoj4736.in'); reset(input);
assign(output,'bzoj4736.out'); rewrite(output);
readln(n,m);
fillchar(w,sizeof(w),$7f);
//fillchar(l,sizeof(l),$1f);
for i:= to m do
begin
readln(ch); s:=; id:=; x:=; y:=; tt:=; ll:=;
len:=length(ch);
case ch[] of
'f':
begin
for j:= to len do
begin
if ch[j]=' ' then begin inc(s); continue; end;
tmp:=ord(ch[j])-ord('');
case s of
:id:=id*+tmp;
:x:=x*+tmp;
:y:=y*+tmp;
:tt:=tt*+tmp;
:ll:=ll*+tmp;
end;
end;
inc(x); inc(y); inc(id);
tot:=id+;
w[tot]:=tt; l1[tot]:=ll; f[tot,]:=x; f[tot,]:=y;
if findroot(x)<>findroot(y) then
begin
mx[tot]:=tot;
link(x,tot); link(tot,y);
end
else
begin
split(x,y);
now:=mx[y];
if w[now]<tt then
begin
cut(f[now,],now); cut(now,f[now,]);
mx[tot]:=tot;
link(x,tot); link(tot,y);
end;
end; end;
'm':
begin
for j:= to len do
begin
if ch[j]=' ' then begin inc(s); continue; end;
tmp:=ord(ch[j])-ord('');
case s of
:x:=x*+tmp;
:y:=y*+tmp;
end;
end;
inc(x); inc(y);
if findroot(x)<>findroot(y) then writeln(-)
else
begin
split(x,y); writeln(sum[y]);
end;
end;
'c':
begin
for j:= to len do
begin
if ch[j]=' ' then begin inc(s); continue; end;
tmp:=ord(ch[j])-ord('');
case s of
:id:=id*+tmp;
:ll:=ll*+tmp;
end;
end;
inc(id); tot:=id+;
splay(tot);
l1[tot]:=ll;
pushup(tot);
end;
end;
end;
close(input);
close(output);
end.
【BZOJ4736】温暖会指引我们前行(LCT)的更多相关文章
- UOJ #274. 【清华集训2016】温暖会指引我们前行 [lct]
#274. [清华集训2016]温暖会指引我们前行 题意比较巧妙 裸lct维护最大生成树 #include <iostream> #include <cstdio> #incl ...
- 【UOJ274】【清华集训2016】温暖会指引我们前行 LCT
[UOJ274][清华集训2016]温暖会指引我们前行 任务描述 虽然小R住的宿舍楼早已来了暖气,但是由于某些原因,宿舍楼中的某些窗户仍然开着(例如厕所的窗户),这就使得宿舍楼中有一些路上的温度还是很 ...
- bzoj 4736 /uoj274【清华集训2016】温暖会指引我们前行 lct
[清华集训2016]温暖会指引我们前行 统计 描述 提交 自定义测试 寒冬又一次肆虐了北国大地 无情的北风穿透了人们御寒的衣物 可怜虫们在冬夜中发出无助的哀嚎 “冻死宝宝了!” 这时 远处的天边出现了 ...
- [清华集训2016]温暖会指引我们前行——LCT+最大生成树
题目链接: [清华集训2016]温暖会指引我们前行 题目大意:有$n$个点$m$次操作,每次操作分为三种:1.在$u,v$两点之间连接一条编号为$id$,长度为$l$,温度为$t$的边.2.查询从$u ...
- BZOJ4736 温暖会指引我们前行(LCT+最大生成树)
类似于瓶颈路,满足条件的路径一定在温度的最大生成树上,那么就是一个LCT维护MST的裸题了. #include<iostream> #include<cstdio> #incl ...
- [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 本题中的字典序不同在于空串的字典序最大. 并且题中要求排序后字典序最大. ...
随机推荐
- 你干啥的?Lombok
01.Lombok 的自我介绍 Lombok 在官网是这样作自我介绍的: Project Lombok makes java a spicier language by adding 'handler ...
- 模板方法模式及php实现
模板方法模式: 定义一个操作中的算法的骨架,而将一些步骤延迟到子类中.TemplateMethod 使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤. 角色: 抽象模板角色:抽象模板类, ...
- CF983A Finite or not?
思路: 如果p,q不互质,先把q除以p,q的最大公约数.接下来只要b中包含了所有q的质因子就可以了.可以在gcd(q, b) 不等于1的时候不断地用q除以gcd(q, b).最后如果q等于1,说明Fi ...
- 激活eclipse自动提示功能
eclipse设置: Window->Preferences->Java->Editor->Content Assist
- python基础一 day5 知识点
Unicode转化为gbk和utf-8 表现形式:str转化为bytes
- Chrome安装助手踩坑
[前言] 最近用之前的方法配置hosts,想浏览下载国外网站的数据和插件,突然发现几乎所有的方法都无效了...... 本文介绍下下载谷歌助手,通过助手访问国外网站 [主体] (1)搜索谷歌助手,点击下 ...
- FastDFS和集中存储方式对比
指标 FastDFS NFS 集中存储设备如NetApp.NAS 线性扩容性 高 差 差 文件高并发访问性能 高 差 一般 文件访问方式 专有API POSIX 支持POSIX 硬件成 ...
- react笔记汇总
1.什么是React? a.React 是一个用于构建用户界面的 JAVASCRIPT 库. b.React主要用于构建UI,很多人认为 React 是 MVC 中的 V. c.React 起源于 F ...
- vue在传值的时候经常遇到的问题
在我用vue编写程序的时候,在传值的时候,经常会遇到些问题,像今天遇到了两个问题,在用父传子的方法去传值,当父组件中的要传的数据是for循环出来的或者是列表的时候,你想每次运行的事件,都去传某一行,或 ...
- dinic网络流
C - A Plug for UNIX POJ - 1087 You are in charge of setting up the press room for the inaugural meet ...