Uoj #274. 【清华集训2016】温暖会指引我们前行 LCT维护边权_动态最小生成树
动态维护一下最小生成树即可.
#include<bits/stdc++.h>
#define ll long long
#define maxn 1000000
#define inf 1000000000000
using namespace std; void setIO(string s)
{
string in=s+".in",out=s+".out";
freopen(in.c_str(),"r",stdin);
// freopen(out.c_str(),"w",stdout);
}
struct Union
{
int p[maxn];
void init()
{
for(int i=0;i<maxn;++i) p[i]=i;
}
int find(int x)
{
return p[x]==x?x:p[x]=find(p[x]);
}
void merge(int a,int b)
{
int x=find(a),y=find(b);
if(x==y) return ;
p[x]=y;
}
}ut;
struct LCT
{
#define lson ch[x][0]
#define rson ch[x][1]
int f[maxn],ch[maxn][2],sta[maxn],rev[maxn];
ll w[maxn],val[maxn],sumv[maxn],minv[maxn]; int isRoot(int x)
{
return !(ch[f[x]][0]==x||ch[f[x]][1]==x);
}
int get(int x)
{
return ch[f[x]][1]==x;
}
void pushdown(int x)
{
if(!x||!rev[x]) return;
mark(lson), mark(rson),rev[x]^=1;
}
void mark(int x)
{
if(!x) return;
swap(lson,rson),rev[x]^=1;
}
void pushup(int x)
{
if(!x) return;
sumv[x]=sumv[lson]+sumv[rson]+val[x];
minv[x]=min(min(minv[lson],minv[rson]),w[x]);
}
void rotate(int x)
{
int old = f[x], fold = f[old], which = get(x);
if(!isRoot(old)) ch[fold][ch[fold][1] == old] = x;
ch[old][which] = ch[x][which ^ 1], f[ch[old][which]] = old;
ch[x][which ^ 1] = old, f[old] = x, f[x] = fold;
pushup(old), pushup(x);
}
void splay(int x)
{
int u=x,v=0,fa;
sta[++v]=u;
while(!isRoot(u)) sta[++v]=f[u],u=f[u];
while(v) pushdown(sta[v--]);
for(u=f[u];(fa=f[x])!=u;rotate(x))
if(f[fa]!=u)
rotate(get(fa)==get(x)?fa:x);
}
void Access(int x)
{
int t=0;
while(x) splay(x),rson=t,pushup(x),t=x,x=f[x];
}
void MakeRoot(int x)
{
Access(x),splay(x), mark(x);
}
void link(int a,int b)
{
MakeRoot(a),f[a]=b;
}
int FindRoot(int x)
{
Access(x), splay(x);
while(lson) x=lson;
return x;
}
void split(int x,int y)
{
MakeRoot(x), Access(y), splay(y);
}
void cut(int a,int b)
{
split(a,b),ch[b][0]=f[a]=0, pushup(b);
}
int find(int x)
{
if(w[x]==minv[x]) return x;
return minv[lson]==minv[x]?find(lson):find(rson);
}
}tree;
int h1[maxn],h2[maxn];
char opt[20];
#define idx(i) (i+n) int main()
{
// setIO("input");
ll t;
int n,m,id,u,v,l,cur;
scanf("%d%d",&n,&m);
for(int i=0;i<=n;++i) tree.w[i]=inf;
tree.minv[0]=inf;
ut.init();
while(m--)
{
scanf("%s",opt);
switch(opt[0])
{
case 'f' :
{
scanf("%d%d%d%lld%d",&id,&u,&v,&t,&l);
++id,++u,++v;
h1[cur=idx(id)]=u,h2[idx(id)]=v;
if(ut.find(u)!=ut.find(v))
{
ut.merge(u,v);
tree.val[cur]=l,tree.w[cur]=t;
ut.merge(u,cur),ut.merge(v,cur);
tree.link(u,cur), tree.link(v,cur);
}
else
{
tree.split(u,v);
if(tree.minv[v] < t)
{
int k=tree.find(v);
tree.cut(k, h1[k]), tree.cut(k, h2[k]);
tree.val[cur=idx(id)]=l,tree.w[cur]=t;
ut.merge(u,cur), ut.merge(v,cur);
tree.link(u,cur),tree.link(v,cur);
}
}
break;
}
case 'm' :
{
scanf("%d%d",&u,&v);
++u,++v;
if(ut.find(u)!=ut.find(v))
{
printf("-1\n");
}
else
{
tree.split(u,v), printf("%lld\n",tree.sumv[v]);
}
break;
}
case 'c' :
{
scanf("%d%d",&id,&l);
++id;
tree.Access(cur=idx(id)),tree.val[cur]=l,tree.pushup(cur);
break;
}
}
}
return 0;
}
Uoj #274. 【清华集训2016】温暖会指引我们前行 LCT维护边权_动态最小生成树的更多相关文章
- [UOJ#274][清华集训2016]温暖会指引我们前行
[UOJ#274][清华集训2016]温暖会指引我们前行 试题描述 寒冬又一次肆虐了北国大地 无情的北风穿透了人们御寒的衣物 可怜虫们在冬夜中发出无助的哀嚎 “冻死宝宝了!” 这时 远处的天边出现了一 ...
- [清华集训2016]温暖会指引我们前行——LCT+最大生成树
题目链接: [清华集训2016]温暖会指引我们前行 题目大意:有$n$个点$m$次操作,每次操作分为三种:1.在$u,v$两点之间连接一条编号为$id$,长度为$l$,温度为$t$的边.2.查询从$u ...
- UOJ_274_[清华集训2016]温暖会指引我们前行_LCT
UOJ_274_[清华集训2016]温暖会指引我们前行_LCT 任务描述:http://uoj.ac/problem/274 本题中的字典序不同在于空串的字典序最大. 并且题中要求排序后字典序最大. ...
- 【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 ...
- UOJ #274. 【清华集训2016】温暖会指引我们前行 [lct]
#274. [清华集训2016]温暖会指引我们前行 题意比较巧妙 裸lct维护最大生成树 #include <iostream> #include <cstdio> #incl ...
- 【UOJ274】【清华集训2016】温暖会指引我们前行 LCT
[UOJ274][清华集训2016]温暖会指引我们前行 任务描述 虽然小R住的宿舍楼早已来了暖气,但是由于某些原因,宿舍楼中的某些窗户仍然开着(例如厕所的窗户),这就使得宿舍楼中有一些路上的温度还是很 ...
- bzoj 4736 /uoj274【清华集训2016】温暖会指引我们前行 lct
[清华集训2016]温暖会指引我们前行 统计 描述 提交 自定义测试 寒冬又一次肆虐了北国大地 无情的北风穿透了人们御寒的衣物 可怜虫们在冬夜中发出无助的哀嚎 “冻死宝宝了!” 这时 远处的天边出现了 ...
- BZOJ 4736 温暖会指引我们前行 LCT+最优生成树+并查集
题目链接:http://uoj.ac/problem/274 题意概述: 没什么好概述的......概述了题意就知道怎么做了......我懒嘛 分析: 就是用lct维护最大生成树. 然后如果去UOJ上 ...
随机推荐
- [poj1363]Rails_模拟_栈
Rails poj-1363 题目大意:判断一个序列是否是1~n的合法出栈序列. 注释:$1\le n\le 10^4$. 想法:开始想到一种想法. 对于一段序列来讲,显然从首元素开始的连续小于尾元素 ...
- MFS搭建
MooseFS是一个分布式存储的框架,其具有如下特性: 1.Free(GPL) 2.通用文件系统,不需要修改上层应用就可以使用 3.可以在线扩容,体系架构可伸缩性极强. ...
- 任务调度分配题两道 POJ 1973 POJ 1180(斜率优化复习)
POJ 1973 这道题以前做过的.今儿重做一次.由于每个程序员要么做A,要么做B,可以联想到0/1背包(谢谢N巨).这样,可以设状态 dp[i][j]为i个程序员做j个A项目同时,最多可做多少个B项 ...
- 浅谈 System.Decimal 结构
引言 我们知道,Microsoft .NET Framework 中的 System.Decimal 结构(在 C# 语言中等价于 decimal keyword)用来表示十进制数,范围从 -(296 ...
- PyQt5学习随笔01--计算一个目录里我们码的代码行数&&PyQt的多线程通信
今天突然想知道自学习Python以来我一共码了多少行代码了,于是写了一个简单的程序: __author__ = 'jiangzhiheng' # coding=utf-8 from PyQt5.QtC ...
- linux面试之--堆、栈、自由存储区、全局/静态存储区和常量存储区
栈,就是那些由编译器在须要的时候分配,在不须要的时候自己主动清除的变量的存储区.里面的变量一般是局部变量.函数參数等.在一个进程中.位于用户虚拟地址空间顶部的是用户栈,编译器用它来实现函数的调用.和堆 ...
- Mysql查看sql是否走事务
登陆进入server [root@gzmtest_25 ~]# su - mysql [mysql@gzmtest_25 ~]$ mysql.local Welcome to the MySQL mo ...
- LeetCode——Valid Number
Validate if a given string is numeric. Some examples: "0" => true " 0.1 " =&g ...
- [NOIP 2014] 寻找道路
[题目链接] http://uoj.ac/problem/19 [算法] 首先,在反向图上从终点广搜,求出每个点是否可以在答案路径中 然后在正向图中求出源点至终点的最短路,同样可以使用广搜 时间复杂度 ...
- 洛谷 P1032 [ NOIP 2002 ] 字串变换 —— 字符串+bfs
题目:https://www.luogu.org/problemnew/show/P1032 字符串好复杂...先写了个 dfs ,RE一个点TLE一个点,不知该怎么改了... #include< ...