[BZOJ]2594 水管局长数据加强版(Wc2006)
失踪人口回归。
LCT一直是小C的弱项,特别是这种维护链的信息的,写挂了就会调代码调到心态爆炸。
不过还好这一次的模板练习没有出现太多的意外。
Description
Input
Output
按顺序对应输入文件中每一项k=1的任务,你需要输出一个数字和一个回车/换行符。该数字表示:你寻找到的水管路径中所有管道全都完成准备工作所需要的时间(当然要求最短)。
Sample Input
4 4 3
1 2 2
2 3 3
3 4 2
1 4 2
1 1 4
2 1 4
1 1 4
Sample Output
2
3
HINT
N ≤ 100000
M ≤ 1000000
Q ≤ 100000
任何时候我们考虑的水管网络都是连通的,即从任一结点A必有至少一条水管路径通往任一结点B。
Solution
很容易看出题目要求我们维护无向图的最小生成树,查询两点树上路径最大权值,并支持删边操作。
删边不好处理,而且题目没有要求我们在线,我们很容易想到倒过来做,把删边改为加边。
于是用hash判断删去了哪些边(注意hash可能会撞),对删去所有边之后的图做最小生成树。
这样就是LCT维护最小生成树的裸题了。
对于每条新加入的边,查找两端点之间路径最大权值,如果小于那个权值,就删去那条边,并加入这条边。
#include <cstdio>
#include <cstring>
#include <algorithm>
#define l(a) son[a][0]
#define r(a) son[a][1]
#define nrt(a) (l(fa[a])==a || r(fa[a])==a)
#define MM 1000005
#define MN 100005
#define mod 10000007
using namespace std;
struct meg1{int x,y,z;}a[MM];
struct meg2{int g,x,y,z,pos;}b[MN];
struct edge{int nex,to,wt,pos;}e[MN<<];
struct dmp{dmp *nex; int x,y,val;}*mp[mod];
int son[MN][],fa[MN],tfa[MN],tson[MN],tg[MN],mx[MN];
int hr[MN],hua[MN],pin;
int n,m,p; inline int read()
{
int n=,f=; char c=getchar();
while (c<'' || c>'') {if(c=='-')f=-; c=getchar();}
while (c>='' && c<='') {n=n*+c-''; c=getchar();}
return n*f;
} void revs(int x) {tg[x]^=; swap(l(x),r(x)); swap(tfa[x],tson[x]);}
void down(int x) {if (tg[x]) {revs(l(x)); revs(r(x)); tg[x]=;}}
int L(int x) {down(x); return l(x)?L(l(x)):x;}
void aldown(int x) {if (nrt(x)) aldown(fa[x]); down(x);}
void upd(int &x,int y) {if (a[y].z>a[x].z) x=y;}
void update(int x) {mx[x]=; upd(mx[x],tfa[x]); upd(mx[x],tson[x]); upd(mx[x],mx[l(x)]); upd(mx[x],mx[r(x)]);}
void rotate(int x)
{
register int y,z,l,r;
y=fa[x]; z=fa[y]; l=r(y)==x; r=l^;
if (nrt(y)) son[z][r(z)==y]=x;
fa[y]=x; fa[x]=z; fa[son[x][r]]=y;
son[y][l]=son[x][r]; son[x][r]=y;
update(y);
}
void splay(int x)
{
aldown(x);
register int y,z;
for (;nrt(x);rotate(x))
{y=fa[x]; if (nrt(y)) z=fa[y],rotate(r(z)==y^r(y)==x?x:y);}
update(x);
}
void access(int x)
{
for (int i=;x;x=fa[i=x])
{
splay(x); tson[x]=tfa[L(i)];
r(x)=i; update(x);
}
} int getmx(int x,int y)
{
access(x); splay(x); revs(x);
access(y); splay(y); return mx[y];
}
void link(int x,int y,int z)
{
access(x); splay(x);
access(y); splay(y); revs(y);
fa[y]=x; r(x)=y; tfa[y]=tson[x]=z;
update(y); update(x);
}
void cut(int x,int y)
{
access(x); splay(x); revs(x);
access(y); splay(y);
l(y)=fa[l(y)]=; tson[x]=tfa[y]=;
update(x); update(y);
} inline void ins(int x,int y,int z,int w)
{
e[++pin]=(edge){hr[x],y,z,w}; hr[x]=pin;
e[++pin]=(edge){hr[y],x,z,w}; hr[y]=pin;
}
void dfs(int x,int fat)
{
fa[x]=fat;
for (register int i=hr[x];i;i=e[i].nex)
if (e[i].to!=fat) mx[e[i].to]=tfa[e[i].to]=e[i].pos,dfs(e[i].to,x);
}
inline bool gather(int x,int y)
{
if (x==y) return false;
hua[x]=y; return true;
}
int getf(int x) {return hua[x]?hua[x]=getf(hua[x]):x;}
bool cmp(const meg1& a,const meg1& b) {return a.z<b.z;} int seamp(int h,int x,int y)
{
for (dmp* q=mp[h];q;q=q->nex) if (q->x==x && q->y==y) return q->val;
return ;
}
void insmp(int h,int x,int y,int z)
{
dmp* q=new(dmp);
q->nex=mp[h]; q->x=x; q->y=y; q->val=z; mp[h]=q;
}
int geth(int x,int y) {return (1LL*x*+y)%mod;} int main()
{
register int i,lt;
n=read(); m=read(); p=read();
for (i=;i<=m;++i)
{
a[i].x=read(); a[i].y=read(); a[i].z=read();
if (a[i].x>a[i].y) swap(a[i].x,a[i].y);
}
for (i=;i<=p;++i)
{
b[i].g=read(); b[i].x=read(); b[i].y=read();
if (b[i].x>b[i].y) swap(b[i].x,b[i].y);
if (b[i].g==) insmp(geth(b[i].x,b[i].y),b[i].x,b[i].y,i);
}
sort(a+,a+m+,cmp);
for (i=;i<=m;++i)
{
if (lt=seamp(geth(a[i].x,a[i].y),a[i].x,a[i].y)) {b[lt].pos=i; continue;}
if (gather(getf(a[i].x),getf(a[i].y))) ins(a[i].x,a[i].y,a[i].z,i);
}
dfs(,);
for (i=p;i;--i)
{
if (b[i].g==) b[i].z=a[getmx(b[i].x,b[i].y)].z;
else if (b[i].g==) {if (a[b[i].pos].z<a[lt=getmx(b[i].x,b[i].y)].z) cut(a[lt].x,a[lt].y),link(b[i].x,b[i].y,b[i].pos);}
}
for (i=;i<=p;++i) if (b[i].g==) printf("%d\n",b[i].z);
}
Last Word
由于link和cut部分可能有点难以理解,LCT一直是令小C头疼的算法之一,但是小C还是成功把模板默出来了。
然后迷之RE,并且发现树上部分节点的关系违背了伦理道德。调完hash之后,结果发现是在找最左边的节点的节点时忘记down操作了。
果然LCT在维护信息时,考虑哪里需要update,哪里需要down,顺序是什么是最烦的。
不得不说,D-map真的好用。
[BZOJ]2594 水管局长数据加强版(Wc2006)的更多相关文章
- bzoj 2594: 水管局长数据加强版 Link-Cut-Tree
题目: Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公 ...
- BZOJ 2594 水管局长数据加强版(动态树)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2594 题意:给出一个无向图,边有权值.定义一条路径的长度为该路径所有边的最大值.两种操作 ...
- BZOJ 2594 水管局长数据加强版
LCT维护最小生成树 要求两点路径最大的最小,首先想到的肯定是最小生成树,再加上有删边操作,那就得用LCT维护了. 可是对于cut一条边,我们要时刻维护图中的最小生成树,需要把之前被我们淘汰的边找回, ...
- bzoj 2594: [Wc2006]水管局长数据加强版 动态树
2594: [Wc2006]水管局长数据加强版 Time Limit: 25 Sec Memory Limit: 128 MBSubmit: 934 Solved: 291[Submit][Sta ...
- BZOJ 2594: [Wc2006]水管局长数据加强版( LCT )
离线然后就是维护加边的动态MST, Link cut tree秒掉..不过我写+调了好久...时间复杂度O(NlogN + MlogM) ------------------------------- ...
- BZOJ 2594: [Wc2006]水管局长数据加强版 [LCT kruskal]
2594: [Wc2006]水管局长数据加强版 Time Limit: 25 Sec Memory Limit: 128 MBSubmit: 2917 Solved: 918[Submit][St ...
- BZOJ 2594 【WC2006】 水管局长数据加强版
题目链接:水管局长数据加强版 好久没写博客了…… 上次考试的时候写了一发LCT,但是写挂了……突然意识到我已经很久没有写过LCT了,于是今天找了道题来练练手. 首先,LCT这里不讲.这道题要求支持动态 ...
- BZOJ2594: [Wc2006]水管局长数据加强版
题解: 裸LCT+离线+二分+MST... 代码:(几乎摘抄自hzwer) #include<cstdio> #include<cstdlib> #include<cma ...
- [bzoj2594][Wc2006]水管局长数据加强版 (lct)
论蒟蒻的自我修养T_T.. 和noi2014魔法森林基本一样...然而数据范围大得sxbk...UPD:这题如果用lct判联通的话可能会被卡到O(mlogm)..所以最好还是用并查集吧 一开始数组开太 ...
随机推荐
- HTTP协议中PUT和POST使用区别
有的观点认为,应该用POST来创建一个资源,用PUT来更新一个资源:有的观点认为,应该用PUT来创建一个资源,用POST来更新一个资源:还有的观点认为可以用PUT和POST中任何一个来做创 ...
- Java 持久化之 --io流与序列化操作
1)File类操作文件的属性 1.File类的常用方法 1. 文件的绝对完整路径:getAbsolutePath() 文件名:getName() 文件相对路径:getPath() 文件的上一级目录:g ...
- Python内置函数(49)——isinstance
英文文档: isinstance(object, classinfo) Return true if the object argument is an instance of the classin ...
- spring MVC框架入门(外加SSM整合)
spring MVC框架 一.什么是sping MVC Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面.Spring 框架提供了构建 W ...
- OAuth2.0学习(1-10)新浪开放平台微博认证-手机应用授权和refresh_token刷新access_token
1.当你是使用微博官方移动SDK的移动应用时,授权返回access_token的同时,还会多返回一个refresh_token: JSON 1 2 3 4 5 6 { "access ...
- 基于python的统计公报关键数据爬取
# -*- coding: utf-8 -*- """ Created on Wed Nov 8 14:23:14 2017 @author: 123 "&qu ...
- Python基础-用户验证
一.项目需求 1.根据用户名和密码,验证用户是否可登陆 2.允许一次执行可验证三次 3.当用户名输错三次后,该用户名锁定,永久不可登陆 二.代码如下 #!/usr/bin/env python #-* ...
- React-Native(四):React Native之View学习
React Native实现以下布局效果:携html5(http://m.ctrip.com/html5/) 基于HelloWord修改项目代码: /** * Sample React Native ...
- [论文阅读] MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications (MobileNet)
论文地址:MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications 本文提出的模型叫Mobi ...
- 复习HTML+CSS(6)
n 表格和表单的嵌套顺序 n 单行文本域 语法格式:<input type="text" 属性="值"> 常用属性 l Name:文本框的名字 ...