首先声明,这份代码空间复杂度 \(O(n^2)\),瓶颈在给边打标记

由于博主太菜,懒得再改成低复杂度的打标记了,所以\(BZOJ\)的数据过不去

Description

link

给一张图,会有删边操作,求当前图路径\((x,y)\) 的最大边权

\(n \leq 10^3 ,m \le 10^5\)

或\(BZOJ\)版:\(n\le 10^5,m\le10^6\)

Solution

\[Begin
\]

明确两点:

1.这个题要\(LCT\)(因为有删边的操作)

2.这个题把询问离线下来,然后逆序加边更可做

这个题中LCT维护的是“边权”

这里有一个点边转化:

把每一个边拆成点,对端点连两条边(边权就是\(e[i].dis\)),这样就可以维护点权了,在\(link\)和\(cut\)操作中有点点不同

如下:

cut(e[tmp - n].from, tmp);
cut(e[tmp - n].to, tmp);
link(x[i], id[x[i]][y[i]] + n);
link(y[i], id[x[i]][y[i]] + n);

先给出图的最终态,然后求一个最小生成树

每个操作先打通链,也就是 \(split\)

然后对于询问操作,直接给\(s[y]\)

对于改边的操作

每一次加边,都会在生成树上面添加一个环,然后我们找到环上最大边 \(cut\) 就可以

(\(split\)已经通链了,所以直接断就可以)

然后\(link\)就行了

\[Q.E.D
\]

Code

不是很难写吧

#include <bits/stdc++.h>
using namespace std;
#define int long long
namespace yspm {
inline int read() {
int res = 0, f = 1;
char k;
while (!isdigit(k = getchar()))
if (k == '-')
f = -1;
while (isdigit(k)) res = res * 10 + k - '0', k = getchar();
return res * f;
}
const int M = 1e6 + 10, N = 1e5 + 10;
int f[N], c[N][2], s[N], v[N], st[N], n, m, q, x[N], y[N], opt[N], fa[N], ans[N], id[2010][2010];
bool r[N];
struct node {
int from, to, dis, vis;
bool operator<(const node &a) const { return dis < a.dis; }
} e[M];
inline void push_up(int x) {
s[x] = max(s[c[x][0]], max(s[c[x][1]], v[x]));
return;
}
inline bool notroot(int x) { return c[f[x]][1] == x || c[f[x]][0] == x; }
inline void pushr(int x) {
swap(c[x][0], c[x][1]);
r[x] ^= 1;
return;
}
inline void push_down(int x) {
if (r[x]) {
if (c[x][0])
pushr(c[x][0]);
if (c[x][1])
pushr(c[x][1]);
}
return r[x] = 0, void();
}
inline void rotate(int tx) {
int ty = f[tx], z = f[ty], k = (c[ty][1] == tx), w = c[tx][!k];
if (notroot(ty))
c[z][c[z][1] == ty] = tx;
c[tx][!k] = ty;
c[ty][k] = w;
if (w)
f[w] = ty;
f[ty] = tx;
f[tx] = z;
return push_up(ty), push_up(tx);
}
inline void splay(int x) {
int y = x, z = 0;
st[++z] = y;
while (notroot(y)) st[++z] = y = f[y];
while (z) push_down(st[z--]);
while (notroot(x)) {
y = f[x], z = f[y];
if (notroot(y))
rotate((c[y][0] == x) ^ (c[z][0] == y) ? x : y);
rotate(x);
}
return push_up(y);
}
inline void access(int x) {
for (int y = 0; x; x = f[y = x]) splay(x), c[x][1] = y, push_up(x);
return;
}
inline void makeroot(int x) {
access(x);
splay(x);
pushr(x);
return;
}
inline int findroot(int x) {
access(x);
splay(x);
while (c[x][0]) push_down(x), x = c[x][0];
splay(x);
return x;
}
inline void split(int x, int y) {
makeroot(x);
access(y);
splay(y);
return;
}
inline void link(int x, int y) {
makeroot(x);
if (findroot(y) != x)
f[x] = y;
return;
}
inline void cut(int x, int y) {
makeroot(x);
if (findroot(y) == x && f[y] == x && !c[y][0])
f[y] = c[x][1] = 0, push_up(x);
return;
} inline int find(int x, int val) {
if (v[x] == val)
return x;
return s[c[x][0]] == val ? find(c[x][0], val) : find(c[x][1], val);
} inline int getf(int x) { return x == fa[x] ? x : fa[x] = getf(fa[x]); }
inline void merge(int x, int y) { fa[getf(x)] = getf(y); }
inline void Kruskal() {
for (int i = 1; i <= n; ++i) fa[i] = i;
for (int i = 1; i <= m; ++i) {
if (!e[i].vis && getf(e[i].from) != getf(e[i].to)) {
merge(e[i].from, e[i].to);
link(e[i].from, n + i);
link(e[i].to, n + i);
}
}
return;
} signed main() {
n = read();
m = read();
q = read();
for (int i = 1; i <= m; ++i) e[i].from = read(), e[i].to = read(), e[i].dis = read();
sort(e + 1, e + m + 1);
for (int i = 1; i <= m; ++i) {
id[e[i].from][e[i].to] = id[e[i].to][e[i].from] = i;
v[n + i] = e[i].dis;
}
for (int i = 1; i <= q; ++i) {
opt[i] = read();
x[i] = read();
y[i] = read();
if (opt[i] == 2)
e[id[x[i]][y[i]]].vis = 1;
}
Kruskal();
int num = 0;
for (int i = q; i >= 1; --i) {
split(x[i], y[i]);
if (opt[i] == 1)
ans[++num] = s[y[i]];
else {
int tmp = find(y[i], s[y[i]]);
if (v[id[x[i]][y[i]] + n] < s[y[i]]) {
cut(e[tmp - n].from, tmp);
cut(e[tmp - n].to, tmp);
link(x[i], id[x[i]][y[i]] + n);
link(y[i], id[x[i]][y[i]] + n);
}
}
}
for (int i = num; i >= 1; --i) printf("%lld\n", ans[i]);
return 0;
}
} // namespace yspm
signed main() { return yspm::main(); }

LGOJ4172 WC2006水管局长的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  8. P4172 [WC2006]水管局长

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

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

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

随机推荐

  1. phpStudy配置站点解决各种不能访问问题(本地可www.xx.com访问)

    1.配置站点:打开phpStudy->其他选项菜单->站点域名管理 2.配置站点:打开phpStudy->其他选项菜单->打开hosts(www访问重点) 3.在apache的 ...

  2. SciKit-Learn 加载数据集

    章节 SciKit-Learn 加载数据集 SciKit-Learn 数据集基本信息 SciKit-Learn 使用matplotlib可视化数据 SciKit-Learn 可视化数据:主成分分析(P ...

  3. HDU - 6043 KazaQ's Socks(找规律)

    题意:有n双袜子,编号1到n,放在衣柜里,每天早晨取衣柜中编号最小的袜子穿,晚上将这双袜子放在篮子里,当篮子里有n-1双袜子时,清洗袜子,直到第二天晚上才洗好,并将洗好的袜子重新放回衣柜. 分析:规律 ...

  4. ssm搭建,maven,javaConfig

    基于java配置SSM,eclipse 新建maven,web项目 .... 项目结构: jar包 pom.xml spring和DispatcherServlet上下文,相当于web.xml pub ...

  5. maven项目中WEB-INF的父目录必须叫webapp吗?

    这个并不是必须的,可以在pom配置文件中修改,如下所示: <webappDirectory>src/main/WebContent</webappDirectory>      ...

  6. expdp远程导出oracle库

    1.手动在本地建目录 E:\lvchengData 2.执行命令 create or replace directory data as 'E:\lvchengData\'; 3.为本地system用 ...

  7. CountDownLatch、CyclicBarrier、Semaphore的使用

    CountDownLatch(计数器) 主线程等待另外三个线程执行完成后再执行 public static void main(String[] args) { //定义一个CountDownLatc ...

  8. 关于Vue element-ui中诡异问题的解决思路

    最近在做Element-ui项目时总是会出现些异步及其一些诡异问题,关于vue 的异步原理就不多说了,感觉大部分的问题都可以用Vue.nextTick来解决,Vue.nextTick是等DOM二次加载 ...

  9. Python中的常用内置对象之map对象

    如果你了解云计算的最重要的计算框架Mapreduce,你就对Python提供的map和reduce对象有很好的理解,在大数据面前,单机计算愈加力不从心,分布式计算也就是后来的云计算的框架担当大任,它提 ...

  10. Python Learning Day3

    爬虫练习 说是练习,实际是尝试了一些还没有具体了解的方式吧hhhhh' 基于urllib实现 import urllib.request import re url="https://www ...