【题解】[WC2006]水管局长
感觉这题好强啊……本来以为能过,结果毫无疑问的被ge了一顿……在这里记录一下做的过程,也免得以后又忘记啦。
首先,我们应看出在这张图上,要让经过的水管最长的最短,就是要维护一棵动态的最小生成树。只是删除边不是很好操作,所以我们将删边改为加边,反向处理。如果发现新加的边可以更新最小生成树,我们就应该更新图,删去原图中最大的一条边。所以在LCT上我们要多维护一个信息,即当前最大的那条边是哪一条边。至此,大部分的难点都已解决,只是LCT维护的是点上的信息,而这道题的权值都在边上。我们将边转化成点,要将要链接的两个点分别与另一点相连,将信息存在另一个点上即可。
The road is tough and there's still a long way to go. Try your best and don't regret.
#include <bits/stdc++.h>
using namespace std;
#define maxn 150005
#define maxm 1500000
int n, m, opt[maxm], cnp, x[maxm], y[maxm];
int q[maxm], id, T[maxm];
int tot, ans[maxn], mark[maxn];
map <int, int> Map[maxn];
struct node
{
int fa, sum, rev;
int id, v, ch[];
}P[maxm]; struct edge
{
int u, v, t;
friend bool operator <(edge a, edge b)
{
return a.t < b.t;
}
}E[maxn]; int read()
{
int x = , k = ;
char c;
c = getchar();
while(c < '' || c > '') { if(c == '-') k = -; c = getchar(); }
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * k;
} void push_down(int x)
{
if(!P[x].rev) return;
swap(P[x].ch[], P[x].ch[]);
P[P[x].ch[]].rev ^= , P[P[x].ch[]].rev ^= ;
P[x].rev = ;
} void update(int x)
{
if(!x) return;
P[x].id = x, P[x].sum = P[x].v;
if(P[P[x].ch[]].sum > P[x].sum) P[x].sum = P[P[x].ch[]].sum, P[x].id = P[P[x].ch[]].id;
if(P[P[x].ch[]].sum > P[x].sum) P[x].sum = P[P[x].ch[]].sum, P[x].id = P[P[x].ch[]].id;
} bool is_root(int x) { return (P[P[x].fa].ch[] != x) && (P[P[x].fa].ch[] != x); } void rotate(int x)
{
int f = P[x].fa, gf = P[f].fa, k = P[f].ch[] == x;
if(!is_root(f)) P[gf].ch[P[gf].ch[] == f] = x;
P[x].fa = gf, P[f].fa = x;
P[f].ch[k] = P[x].ch[k ^ ], P[P[x].ch[k ^ ]].fa = f;
P[x].ch[k ^ ] = f;
update(f), update(x);
} void Splay(int x)
{
int top = ; q[top] = x;
for(int i = x; !is_root(i); i = P[i].fa) q[++ top] = P[i].fa;
for(int i = top; i; i --) push_down(q[i]);
while(!is_root(x))
{
int f = P[x].fa, gf = P[f].fa;
if(!is_root(f)) (P[f].ch[] == x) ^ (P[gf].ch[] == f) ? rotate(x) : rotate(f);
rotate(x);
}
update(x);
} void Access(int x)
{
for(int ff = ; x; ff = x, x = P[x].fa)
{
Splay(x);
P[x].ch[] = ff;
update(x);
}
} void make_root(int x) { Access(x); Splay(x); P[x].rev ^= ; }
void Split(int u, int v) { make_root(u); Access(v); Splay(v); }
void Link(int u, int v) { make_root(u); P[u].fa = v; }
void Cut(int u, int v) { Split(u, v); P[v].ch[] = P[u].fa = ; } int Get_fa(int x)
{
Access(x); Splay(x);
while(P[x].ch[]) x = P[x].ch[];
return x;
} int main()
{
n = read(), m = read();
int q = read();
for(int i = ; i <= m; i ++)
{
int u = read(), v = read(), w = read(); T[i] = w;
E[++ cnp].u = u; E[cnp].v = v, E[cnp].t = w;
}
sort(E + , E + + m);
for(int i = ; i <= m; i ++) Map[E[i].u][E[i].v] = Map[E[i].v][E[i].u] = i;
for(int i = ; i <= q; i ++)
{
opt[i] = read(); x[i] = read(); y[i] = read();
if(opt[i] == ) mark[Map[x[i]][y[i]]] = ;
}
for(int i = ; i <= m; i ++)
{
int u = E[i].u, v = E[i].v, t = E[i].t;
if(!mark[Map[u][v]] && Get_fa(u) != Get_fa(v))
{
id = i + n, P[id].sum = P[id].v = t;
Link(u, id), Link(id, v);
}
}
for(int i = q; i; i --)
{
Split(x[i], y[i]);
if(opt[i] == ) // find a route
ans[++ tot] = P[y[i]].sum;
else // add edge
{
int now = n + Map[x[i]][y[i]];
int tem = E[now - n].t;
if(tem < P[y[i]].sum)
{
P[now].v = tem; tem = P[y[i]].id;
Cut(tem, E[tem - n].u); Cut(tem, E[tem - n].v);
Link(now, x[i]), Link(now, y[i]);
}
}
}
for(int i = tot; i; i --)
printf("%d\n", ans[i]);
return ;
}
【题解】[WC2006]水管局长的更多相关文章
- BZOJ2594: [Wc2006]水管局长数据加强版
题解: 裸LCT+离线+二分+MST... 代码:(几乎摘抄自hzwer) #include<cstdio> #include<cstdlib> #include<cma ...
- 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 ...
- BZOJ 2594: [Wc2006]水管局长数据加强版( LCT )
离线然后就是维护加边的动态MST, Link cut tree秒掉..不过我写+调了好久...时间复杂度O(NlogN + MlogM) ------------------------------- ...
- [bzoj2594][Wc2006]水管局长数据加强版 (lct)
论蒟蒻的自我修养T_T.. 和noi2014魔法森林基本一样...然而数据范围大得sxbk...UPD:这题如果用lct判联通的话可能会被卡到O(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
BZOJ_2594_[Wc2006]水管局长数据加强版_LCT Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供 ...
- P4172 [WC2006]水管局长(LCT)
P4172 [WC2006]水管局长 LCT维护最小生成树,边权化点权.类似 P2387 [NOI2014]魔法森林(LCT) 离线存储询问,倒序处理,删边改加边. #include<iostr ...
- [BZOJ2594][WC2006]水管局长加强版(LCT+Kruskal)
2594: [Wc2006]水管局长数据加强版 Time Limit: 25 Sec Memory Limit: 128 MBSubmit: 4452 Solved: 1385[Submit][S ...
- [BZOJ2594] [WC2006]水管局长(Kruskal+LCT)
[BZOJ2594] [WC2006]水管局长(Kruskal+LCT) 题面 SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可 ...
随机推荐
- tp5 数据库信息导出到excel(带图片)
function excel_down(){ //导入谁就去查谁 $data=Db::name('order_xueyou')->select(); // 导出Exl // import(&qu ...
- python七类之字典详解
一.字典 一.关键字:dict 1.字典是唯一的键值对数据,其表现形式: dic = {键:值},字典里的键必须保证是唯一的 2.键必须是不可变的数据类型: a.故列表是不能当键的 b.所 ...
- Mysql通过Adjacency List(邻接表)存储树形结构
转载自:https://www.jb51.net/article/130222.htm 以下内容给大家介绍了MYSQL通过Adjacency List (邻接表)来存储树形结构的过程介绍和解决办法,并 ...
- Python3.6中PyInstaller不能对文件进行打包问题
上篇文章<itchat和matplotlib的结合使用爬取微信信息>是用python爬取信息得到微信朋友的信息,并且用matplotlib统计信息进行画图,所以今天想将它打包成.exe可执 ...
- react中事件冒泡之填坑
今天在写个组件,大致代码是这样的: class Switch extends React.Component { handlerChange = (e) => { const {onChange ...
- P1991 无线通讯网
P1991 无线通讯网 题目描述 国防部计划用无线网络连接若干个边防哨所.2 种不同的通讯技术用来搭建无线网络: 每个边防哨所都要配备无线电收发器:有一些哨所还可以增配卫星电话. 任意两个配备了一条卫 ...
- RHCE7认证学习笔记17——KickStart安装系统
一.自动化安装系统工具 1.Cobbler 另一个自动化安装工具: 2.Kickstart 二.使用kickstart自动化安装系统 服务器安装的软件: 1.dhcp服务 [root@lin ...
- Eclipse 导入项目与 svn 插件关联全过程记录
文章摘自:http://www.cnblogs.com/xmmcn/archive/2013/03/01/2938365.html 感谢博友分享! Eclipse 导入项目与 svn 插件关联全过程记 ...
- php长整型完整输出
今天调用webservice时返回一个字段是int64 长整型 原始的数值应该是 190000002101056096 而php返回时转成 1.9000000210106E+17 当传入另一个接口就报 ...
- 我所认识的XPath
实例demo 测试demo所需要xml测试数据 <?xml version="1.0" encoding="iso-8859-1"?> <bo ...