感觉这题好强啊……本来以为能过,结果毫无疑问的被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. JS高度融合入门笔记(二)

    <!DOCTYPE html><html><head> <meta charset="utf-8"> <title>JS ...

  2. Python栈的学习资料

    持续更新... 1. 基础 Python for Everybody的视频课程,称得上深入浅出 https://www.py4e.com/ 2. 进阶 偏重实践应用,快速上手,稀饭~ https:// ...

  3. rails中如何在a标签中添加其他标签

    最近在用rails写一个项目练练手,然后遇到了一个问题,就是用 <% link_to("首页", root_path) %> 生成一个a标签,之后就在想我怎么在这个a标 ...

  4. 003---random随机数模块

    import random # 随机数(0-1) print(random.random()) # 随机整数, 包含尾巴 print(random.randint(-1, 2)) # 不包含尾巴 pr ...

  5. R语言绘图:雷达图

    使用fmsb包绘制雷达图 library("fmsb") radarfig <- rbind(rep(90, 4), rep(60, 4), c(86.17, 73.96, ...

  6. Sqoop的安装配置及使用

    一.Sqoop基础:连接关系型数据库与Hadoop的桥梁 1.1 Sqoop的基本概念 Hadoop正成为企业用于大数据分析的最热门选择,但想将你的数据移植过去并不容易.Apache Sqoop正在加 ...

  7. Electron入门应用打包exe(windows)

    最近在学习nodejs,得知Electron是通过将Chromium和Node.js合并到同一个运行时环境中,用HTML,CSS和JavaScript来构建跨平台桌面应用程序的一门技术.对于之前一直从 ...

  8. CC3200在sl_Start函数处不断重启复位的原因解析

    1. 使用过程中,自己写的工程,发现CC3200一直重启,首先需要定位出现重启的函数?看门狗复位,还是程序跑飞复位?NWP的版本不匹配?经过测试找到出问题的函数,这个函数是启动网络的函数. lRetV ...

  9. 删除txt文件内容

    删除txt文件里的 聊天记录的时间那一行 f = open("d:\\面试.txt", "r") g = open("d:\\英雄联盟ADC技巧.tx ...

  10. qt 编译unresolved external symbol的错误解决

    题外问题:.rc文件报错,里面引用的.h文件打不开. 方法:rc文件移除,然后重新添加就可以: unresolved external symbol的原因: 1.没有添加编译生成的moc文件,添加对应 ...