UOJ 274 【清华集训2016】温暖会指引我们前行 ——Link-Cut Tree
魔法森林高清重置,
只需要维护关于t的最大生成树,然后链上边权求和即可。
直接上LCT
调了将近2h
吃枣药丸
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define F(i,j,k) for (int i=j;i<=k;++i)
#define D(i,j,k) for (int i=j;i>=k;--i)
#define maxn 1000005
#define inf 0x3f3f3f3f
struct Link_Cut_Tree{
int ch[maxn][2],fa[maxn],rev[maxn],mn[maxn],val[maxn],sum[maxn],len[maxn];
int sta[maxn],top;
bool isroot(int x){return ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;}
void update(int x)
{
mn[x]=x;
if (val[mn[ch[x][0]]]<val[mn[x]]) mn[x]=mn[ch[x][0]];
if (val[mn[ch[x][1]]]<val[mn[x]]) mn[x]=mn[ch[x][1]];
sum[x]=len[x]+sum[ch[x][0]]+sum[ch[x][1]];
}
void pushdown (int x)
{
if (rev[x])
{
rev[x]^=1;
rev[ch[x][0]]^=1;
rev[ch[x][1]]^=1;
swap(ch[x][0],ch[x][1]);
}
}
void rot(int x)
{
int y=fa[x],z=fa[y],l,r;
if (ch[y][0]==x) l=0; else l=1; r=l^1;
if (!isroot(y))
{
if (ch[z][0]==y) ch[z][0]=x;
else ch[z][1]=x;
}
fa[x]=z; fa[y]=x; fa[ch[x][r]]=y;
ch[y][l]=ch[x][r]; ch[x][r]=y;
update(y);update(x);
}
void splay(int x)
{
top=0;sta[++top]=x;
for (int i=x;!isroot(i);i=fa[i]) sta[++top]=fa[i];
while (top) pushdown(sta[top--]); while (!isroot(x))
{
int y=fa[x],z=fa[y];
if (!isroot(y))
{
if (ch[z][0]==y^ch[y][0]==x) rot(x);
else rot(y);
}
rot(x);
}
}
void access(int x)
{
for (int t=0;x;t=x,x=fa[x])
splay(x),ch[x][1]=t,update(x);
}
void makeroot(int x)
{
access(x); splay(x); rev[x]^=1;
}
int find(int x)
{
access(x); splay(x); while (ch[x][0]) x=ch[x][0];
return x;
}
void link(int x,int y)
{
makeroot(x);
fa[x]=y;
}
void cut(int x,int y)
{
makeroot(x);
access(y);
ch[y][0]=0;
fa[x]=0;
}
int query(int x,int y)
{
if (find(x)!=find(y)) return -1;
makeroot(x);access(y);splay(y);
return sum[y];
}
int qmn(int x,int y)
{
makeroot(x);access(y);splay(y);
return mn[y];
}
void modify(int x,int l)
{
access(x);splay(x);
len[x]=l;update(x);
}
}LCT; int n,m,u[maxn],v[maxn],id,t,l,x,y,in[maxn];
char opt[11]; int main()
{
scanf("%d%d",&n,&m);
F(i,0,n) LCT.val[i]=inf;
F(i,1,m)
{
scanf("%s",opt);
switch(opt[0])
{
case 'f':
scanf("%d",&id); id++;
scanf("%d%d%d%d",&u[id],&v[id],&t,&l);u[id]++;v[id]++;
LCT.len[id+n]=l;LCT.val[id+n]=t;LCT.mn[id+n]=id+n;
if (LCT.find(u[id])!=LCT.find(v[id]))
{
LCT.link(id+n,u[id]);
LCT.link(id+n,v[id]);
in[id]=1;
}
else
{
int tmp=LCT.qmn(u[id],v[id]);
if (LCT.val[tmp]<t)
{
LCT.cut(u[tmp-n],tmp);
LCT.cut(v[tmp-n],tmp);
in[tmp-n]=0;
LCT.link(id+n,u[id]);
LCT.link(id+n,v[id]);
in[id]=1;
}
}
break;
case 'm':
scanf("%d%d",&x,&y);x++;y++;
printf("%d\n",LCT.query(x,y));
break;
case 'c':
scanf("%d%d",&id,&l); id++;
if (in[id]) LCT.modify(id+n,l);
break;
}
}
}
UOJ 274 【清华集训2016】温暖会指引我们前行 ——Link-Cut Tree的更多相关文章
- [UOJ#274][清华集训2016]温暖会指引我们前行
[UOJ#274][清华集训2016]温暖会指引我们前行 试题描述 寒冬又一次肆虐了北国大地 无情的北风穿透了人们御寒的衣物 可怜虫们在冬夜中发出无助的哀嚎 “冻死宝宝了!” 这时 远处的天边出现了一 ...
- UOJ_274_[清华集训2016]温暖会指引我们前行_LCT
UOJ_274_[清华集训2016]温暖会指引我们前行_LCT 任务描述:http://uoj.ac/problem/274 本题中的字典序不同在于空串的字典序最大. 并且题中要求排序后字典序最大. ...
- [清华集训2016]温暖会指引我们前行——LCT+最大生成树
题目链接: [清华集训2016]温暖会指引我们前行 题目大意:有$n$个点$m$次操作,每次操作分为三种:1.在$u,v$两点之间连接一条编号为$id$,长度为$l$,温度为$t$的边.2.查询从$u ...
- 【bzoj4736/uoj#274】[清华集训2016]温暖会指引我们前行 语文题+LCT
题目描述 http://uoj.ac/problem/274 题解 语文题+LCT 对于这种语文题建议还是自己读题好一些... 读懂题后发现:由于温度互不相同,最大生成树上的路径必须走(不走的话温度大 ...
- UOJ274 [清华集训2016] 温暖会指引我们前行 【LCT】【最大生成树】
题目分析: 差评,最大生成树裸题.hack数据还卡常. 代码: #include<bits/stdc++.h> using namespace std; ; struct LCT{ ],d ...
- 【BZOJ4736】温暖会指引我们前行(Link-Cut Tree)
[BZOJ4736]温暖会指引我们前行(Link-Cut Tree) ##题面 神TM题面是UOJ的 题解 LCT傻逼维护最大生成树 不会的可以去做一做魔法森林 #include<iostrea ...
- BZOJ 4732 UOJ #268 [清华集训2016]数据交互 (树链剖分、线段树)
题目链接 (BZOJ) https://www.lydsy.com/JudgeOnline/problem.php?id=4732 (UOJ) http://uoj.ac/problem/268 题解 ...
- [UOJ#276][清华集训2016]汽水[分数规划+点分治]
题意 给定一棵 \(n\) 个点的树,给定 \(k\) ,求 \(|\frac{\sum w(路径长度)}{t(路径边数)}-k|\)的最小值. \(n\leq 5\times 10^5,k\leq ...
- UOJ #274. 【清华集训2016】温暖会指引我们前行 [lct]
#274. [清华集训2016]温暖会指引我们前行 题意比较巧妙 裸lct维护最大生成树 #include <iostream> #include <cstdio> #incl ...
- 【UOJ274】【清华集训2016】温暖会指引我们前行 LCT
[UOJ274][清华集训2016]温暖会指引我们前行 任务描述 虽然小R住的宿舍楼早已来了暖气,但是由于某些原因,宿舍楼中的某些窗户仍然开着(例如厕所的窗户),这就使得宿舍楼中有一些路上的温度还是很 ...
随机推荐
- Objective-C language
Objective-C is the primary language used to write Mac software. If you're comfortable with basic obj ...
- UITabBarController、导航控制器、控制器关系
UITabBarController与UINavigationController类似,UITabBarController也可以用来控制多个页面导航,用户可以在多个视图控制器之间移动,并可以定制屏幕 ...
- 如何修改tomcat的启动方式为 run
tomcat根目录\bin\startup.bat,记事本打开,找到: call "%EXECUTABLE%" start %CMD_LINE_ARGS% :end 把start ...
- Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.Reason: Failed to determine a suitable driver class
解决方案: @SpringBootApplication(exclude = DataSourceAutoConfiguration.class) 作用://取消数据库配置 但是 在用到数据库的时候记 ...
- for..in...时,注意hasOwnProperty验证
for..in...时,注意hasOwnProperty验证 var obj = { a: 10, b: 20 }; // 注意词句代码 Object.prototype.c = 30; var it ...
- crop层
A是要进行剪裁的blob,B是参考,C是由A剪裁出来的输出. 模式1和模式2不同在于offset,模式1中每个dimension可以不同,模式2中用一个值表示了所有dimension的值. axis表 ...
- 获得stixel的gt数据
这是论文中的作者博客地址https://sites.google.com/site/danmlevi/ 这是作者现在的博客地址https://sites.google.com/view/danlevi ...
- StringMVCWeb接受前台值的几种方式
这些决定与request header 的Content-Type属性 1.通过@RequestParam @RequestParam Map<String, Object> pa ...
- java常用流处理工具StreamTool 常见的InputStream流转字符串, 转字节数组等等
ava 常用流处理工具 StreamTool ,常见的InputStream 流转字符串, 转字节数组等等 **应用场景: ** 1. 文件上传 2. js / css / img 等文件读取输出. ...
- C++系统学习之六:函数
1.函数基础 典型的函数定义包括:返回类型.函数名.由0个或多个形参组成的列表以及函数体. 2.参数传递 形参初始化的机理和变量初始化一样. 有两种方式:引用传递和值传递 2.1 传值参数 当形参是非 ...