感觉这题好强啊……本来以为能过,结果毫无疑问的被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]水管局长的更多相关文章

  1. BZOJ2594: [Wc2006]水管局长数据加强版

    题解: 裸LCT+离线+二分+MST... 代码:(几乎摘抄自hzwer) #include<cstdio> #include<cstdlib> #include<cma ...

  2. P4172 [WC2006]水管局长

    P4172 [WC2006]水管局长 前言 luogu数据太小 去bzoj,他的数据大一些 思路 正着删不好维护 那就倒着加,没了 LCT维护他的最小生成树MST 树上加一条边肯定会有一个环 看看环上 ...

  3. bzoj 2594: [Wc2006]水管局长数据加强版 动态树

    2594: [Wc2006]水管局长数据加强版 Time Limit: 25 Sec  Memory Limit: 128 MBSubmit: 934  Solved: 291[Submit][Sta ...

  4. BZOJ 2594: [Wc2006]水管局长数据加强版( LCT )

    离线然后就是维护加边的动态MST, Link cut tree秒掉..不过我写+调了好久...时间复杂度O(NlogN + MlogM) ------------------------------- ...

  5. [bzoj2594][Wc2006]水管局长数据加强版 (lct)

    论蒟蒻的自我修养T_T.. 和noi2014魔法森林基本一样...然而数据范围大得sxbk...UPD:这题如果用lct判联通的话可能会被卡到O(mlogm)..所以最好还是用并查集吧 一开始数组开太 ...

  6. BZOJ 2594: [Wc2006]水管局长数据加强版 [LCT kruskal]

    2594: [Wc2006]水管局长数据加强版 Time Limit: 25 Sec  Memory Limit: 128 MBSubmit: 2917  Solved: 918[Submit][St ...

  7. BZOJ_2594_[Wc2006]水管局长数据加强版_LCT

    BZOJ_2594_[Wc2006]水管局长数据加强版_LCT Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供 ...

  8. P4172 [WC2006]水管局长(LCT)

    P4172 [WC2006]水管局长 LCT维护最小生成树,边权化点权.类似 P2387 [NOI2014]魔法森林(LCT) 离线存储询问,倒序处理,删边改加边. #include<iostr ...

  9. [BZOJ2594][WC2006]水管局长加强版(LCT+Kruskal)

    2594: [Wc2006]水管局长数据加强版 Time Limit: 25 Sec  Memory Limit: 128 MBSubmit: 4452  Solved: 1385[Submit][S ...

  10. [BZOJ2594] [WC2006]水管局长(Kruskal+LCT)

    [BZOJ2594] [WC2006]水管局长(Kruskal+LCT) 题面 SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可 ...

随机推荐

  1. jquery图片滚动normalizy.css

    article,aside,details,figcaption,figure,footer,header,hgroup,main,nav,section,summary{display:block; ...

  2. MySQL Waiting for table metadata lock的解决方法

    最近需要在某一个表中新增字段,使用Sequel Pro 或者Navicat工具都会出现程序没有反应,使用 show processlist 查看,满屏都是 Waiting for table meta ...

  3. Laravel5.x 封装的上传图片类

    图片缩放需要用conposer安装 ImageManagerStatic类 可参考下面的地址安装: https://www.jb51.net/article/128159.htm 控制器里: 控制器里 ...

  4. Cent OS 下 VI 使用方法

    vi的基本概念  基本上vi可以分为三种状态,分别是命令模式(command mode).插入模式(Insert mode)和底行模式(last line mode),各模式的功能区分如下: 1) 命 ...

  5. 自定义vim配置文件vimrc,用于c/c++编程

    vim作为Linux下广受赞誉的代码编辑器,其独特的纯命令行操作模式可以很大程度上方便编程工作,通过自定义vim配置文件可以实现对vim功能的个性化设置. vim配置文件一般有两份,属于root的/e ...

  6. Excel学习路径总结

    本片涉及从入门到Excel的各个方向,包含众多资料和自己学习的心得,希望您可以仔细阅之:   入门篇: 无论是软件,还是编程,最好的入门就是通过看视频来学习,视频优点为很容易看清楚,手把手教授,不容易 ...

  7. python2.7入门---变量类型&案例

      这篇文章呢,主要是用来记录python中的变量类型学习内容的.接下来就来看一下变量类型,那么什么是变量呢.变量存储在内存中的值.这就意味着在创建变量时会在内存中开辟一个空间.基于变量的数据类型,解 ...

  8. java 单例模式(singleton)

    概念: 保证一个类仅有一个实例,并提供一个访问它的全局访问点. 要点: 1.某个类只有一个实例. 2.它必须自行创建这个示例. 3.必须自行向整个系统提供这个示例. 实现: 1.拥有一个私有的构造器. ...

  9. T分布、卡方分布、F分布

    请参考: https://www.cnblogs.com/think-and-do/p/6509239.html

  10. 纯原生仿ES6的Object.assign,实现深度合并对象

    源码: function isObj(x){ var type = typeof x; return x !== null && (type === 'object' || type ...