luogu P4172 [WC2006]水管局长 LCT维护动态MST + 离线
Description
Input
Output
#include<bits/stdc++.h>
#define maxn 1200000
#define N 120000
using namespace std;
char *p1,*p2,buf[100000];
#define nc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
int rd() {int x=0,f=1; char c=nc(); while(c<48) {if(c=='-') f=-1; c=nc();} while(c>47) x=(((x<<2)+x)<<1)+(c^48),c=nc(); return x*f;}
void setIO(string s)
{
string in=s+".in";
string out=s+".out";
freopen(in.c_str(),"r",stdin);
freopen(out.c_str(),"w",stdout);
}
struct Union
{
int p[maxn];
inline void init() { for(int i=0;i<maxn;++i) p[i]=i; }
inline int find(int x) { return p[x]==x?x:p[x]=find(p[x]); }
inline int merge(int x,int y)
{
int a=find(x),b=find(y);
if(a==b) return 0;
p[a]=b;
return 1;
}
}con;
struct LCT
{
#define lson ch[x][0]
#define rson ch[x][1]
#define isrt(x) (!(ch[f[x]][1]==x || ch[f[x]][0]==x))
#define get(x) (ch[f[x]][1]==x)
int ch[maxn][2], maxv[maxn], rev[maxn], sumv[maxn], val[maxn], sta[maxn], idx[maxn], f[maxn];
inline void mark(int x)
{
if(!x)return;
swap(lson,rson), rev[x]^=1;
}
inline void pushup(int x)
{
if(!x)return;
idx[x]=x, maxv[x]=val[x];
if(maxv[lson] > maxv[x]) idx[x] = idx[lson], maxv[x]=maxv[lson];
if(maxv[rson] > maxv[x]) idx[x] = idx[rson], maxv[x]=maxv[rson];
sumv[x]=sumv[lson]+sumv[rson]+val[x];
}
inline void pushdown(int x)
{
if(!x)return;
if(rev[x]) mark(lson), mark(rson), rev[x]^=1;
}
inline void rotate(int x)
{
int old=f[x],fold=f[old], which=get(x);
if(!isrt(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);
}
inline void splay(int x)
{
int u=x,v=0,fa;
for(sta[++v]=u;!isrt(u);u=f[u]) sta[++v]=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);
}
inline void Access(int x)
{
int t=0;
while(x)
{
splay(x), rson=t, pushup(x), t=x,x=f[x];
}
}
inline void MakeRoot(int x)
{
Access(x), splay(x), mark(x);
}
inline void split(int x,int y)
{
MakeRoot(x), Access(y), splay(y);
}
inline void cut(int x,int y)
{
MakeRoot(x), Access(y), splay(y);
ch[y][0]=f[x]=0;
pushup(y);
}
inline void link(int x,int y)
{
MakeRoot(x), f[x]=y;
}
}tr;
int n,m,Q,eds;
int from[maxn],to[maxn], cap[maxn], answer[maxn];
map<int,int>ck[maxn];
struct OPT
{
int o, x,y,eds, id, ans;
}opt[maxn];
int main()
{
int i,j;
setIO("input");
con.init();
n=rd(),m=rd(),Q=rd();
for(i=1;i<=m;++i)
{
++eds;
from[eds]=rd(), to[eds]=rd(), cap[eds]=rd();
ck[from[eds]][to[eds]]=ck[to[eds]][from[eds]]=eds;
}
for(i=1;i<=Q;++i)
{
opt[i].o=rd();
if(opt[i].o==1) opt[i].x=rd(), opt[i].y=rd();
if(opt[i].o==2)
{
int a,b;
a=rd(),b=rd();
opt[i].eds = ck[a][b];
ck[a][b]=ck[b][a]=0;
}
}
for(i=1;i<=eds;++i)
{
if(ck[from[i]][to[i]])
{
++Q;
opt[Q].o=2;
opt[Q].eds=ck[from[i]][to[i]];
}
}
for(i=Q;i>=1;--i)
{ if(opt[i].o==1)
{
tr.split(opt[i].x,opt[i].y);
opt[i].ans=tr.maxv[opt[i].y];
}
if(opt[i].o==2)
{
int cur=opt[i].eds,c,_new;
int x=from[cur], y=to[cur], cc=cap[cur],old;
if(con.merge(x, y))
{
_new=cur + n;
tr.val[_new]=tr.maxv[_new]=tr.sumv[_new]=cap[cur];
tr.link(_new, x), tr.link(_new, y);
}
else
{
tr.split(x,y);
c=tr.idx[y];
old=tr.val[c];
if(cc < old)
{
int a=from[c-n], b=to[c-n];
tr.cut(a, c), tr.cut(b, c);
_new=cur+n;
tr.val[_new]=tr.maxv[_new]=tr.sumv[_new]=cap[cur];
tr.link(_new, x), tr.link(_new, y);
}
}
}
}
for(i=1;i<=Q;++i) if(opt[i].o==1) printf("%d\n",opt[i].ans);
return 0;
}
luogu P4172 [WC2006]水管局长 LCT维护动态MST + 离线的更多相关文章
- P4172 [WC2006]水管局长 LCT维护最小生成树
\(\color{#0066ff}{ 题目描述 }\) SC 省 MY 市有着庞大的地下水管网络,嘟嘟是 MY 市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的 ...
- Luogu P4172 [WC2006]水管局长
题意 给定一个 \(n\) 个点 \(m\) 条边的图和 \(q\) 次操作,每次操作分为以下两种: 1 u v:查询 \(u\) 到 \(v\) 的一条路径使得边权最大的边的权值最小. 2 u v: ...
- 洛谷4172 WC2006水管局长(LCT维护最小生成树)
这个题和魔法森林感觉有很相近的地方啊 同样也是维护一个类似最大边权最小的生成树 但是不同的是,这个题是有\(cut\)和询问,两种操作.... 这可如何是好啊? 我们不妨倒着来考虑,假设所有要\(cu ...
- P4172 [WC2006]水管局长(LCT)
P4172 [WC2006]水管局长 LCT维护最小生成树,边权化点权.类似 P2387 [NOI2014]魔法森林(LCT) 离线存储询问,倒序处理,删边改加边. #include<iostr ...
- P4172 [WC2006]水管局长
P4172 [WC2006]水管局长 前言 luogu数据太小 去bzoj,他的数据大一些 思路 正着删不好维护 那就倒着加,没了 LCT维护他的最小生成树MST 树上加一条边肯定会有一个环 看看环上 ...
- bzoj 2594: [Wc2006]水管局长数据加强版 动态树
2594: [Wc2006]水管局长数据加强版 Time Limit: 25 Sec Memory Limit: 128 MBSubmit: 934 Solved: 291[Submit][Sta ...
- 洛谷P4172 [WC2006]水管局长(lct求动态最小生成树)
SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一条从A至B的水管的路径, ...
- 洛谷P4172 [WC2006]水管局长 (LCT,最小生成树)
洛谷题目传送门 思路分析 在一个图中,要求路径上最大边边权最小,就不难想到最小生成树.而题目中有删边的操作,那肯定是要动态维护啦.直接上LCT维护边权最小值(可以参考一下蒟蒻的Blog) 这时候令人头 ...
- [洛谷P4172] WC2006 水管局长
问题描述 SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一条从A至B的水 ...
随机推荐
- easyUI 对话框的关闭事件
有一个easyUI的dialog: <div id="dlg_Add" class="easyui-dialog" style=" width: ...
- js 判断手机横竖屏的实现方法(不依赖任何其他库)
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- JeePlus:目录
ylbtech-JeePlus:目录 1.返回顶部 0. http://www.jeeplus.org/ 0.2.文档 http://wiki.jeeplus.org/docs/show/75 0.3 ...
- 从缓冲上看阻塞与非阻塞socket在发送接收上的区别(转载)
转自:http://blog.chinaunix.net/uid-24517549-id-4044877.html 首先socket在默认情况下是阻塞状态的,这就使得发送以及接收操作处于阻塞的状态 ...
- bzoj 1641: [Usaco2007 Nov]Cow Hurdles 奶牛跨栏【Floyd】
先跑一遍取max版的Floyd,直接用数组回答询问即可 #include<iostream> #include<cstdio> using namespace std; con ...
- mydatepicker97 日历控件
官方教程: http://www.my97.net/
- [算法] 常见排序算法总结(C语言版)
常见排序算法总结 本文对比较常用且比较高效的排序算法进行了总结和解析,并贴出了比较精简的实现代码,包括选择排序.插入排序.归并排序.希尔排序.快速排序等.算法性能比较如下图所示: 1 冒泡排序 基本原 ...
- 数据结构之动态顺序表(C实现)
线性表有2种,分为顺序表和链表. 顺序表: 采用顺序存储方式,在一组地址连续的存储空间上存储数据元素的线性表(长度固定) 链表: 有3种,单链表.双向链表.循环链表(长度不固定) seqList.h ...
- Node“getTextContent() is undefined for the type Node”处理办法
最近一个项目在MyEclipse导入后总是报getTextContent() is undefined for the type Node错误. 经过查找原来是因为Node类为JDK中自带的(org. ...
- SQL数据库--数据访问
数据访问: 对应命名空间:System.Data.SqlClient; SqlConnection:连接对象 SqlCommand:命令对象 SqlDataReader:读取器对象 //造连接字符串 ...