[题目链接]

https://www.lydsy.com/JudgeOnline/problem.php?id=2594

[算法]

首先离线 , 将删边操作转化为倒序加边

假设我们已经维护出了一棵最小生成树T , 若加入了一条边(u , v , w) , 那么形成了一个环 ,考虑kruskal算法的执行过程 :

若w < 环上的边权最大值 , 那么可以将(u , v , w)加入 , 并将环上边权最大的边删除

可以使用LCT维护

时间复杂度 : O(NlogN ^ 2) , 注意常数优化

[代码]

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + ;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull; struct edge
{
int u , v , w;
} e[MAXN << ];
struct query
{
int type;
int u , v;
} que[MAXN << ]; int n , m , q;
int fa[MAXN];
bool flg[MAXN]; struct Link_Cut_Tree
{
struct Node
{
int father , son[] , mx , value , id;
bool tag;
} a[MAXN << ];
inline void update(int x)
{
a[x].mx = a[x].value;
a[x].id = x;
if (a[x].son[])
{
if (a[a[x].son[]].mx > a[x].mx)
{
a[x].mx = a[a[x].son[]].mx;
a[x].id = a[a[x].son[]].id;
}
}
if (a[x].son[])
{
if (a[a[x].son[]].mx > a[x].mx)
{
a[x].mx = a[a[x].son[]].mx;
a[x].id = a[a[x].son[]].id;
}
}
}
inline void init()
{
for (int i = n + ; i <= n + m + ; i++)
{
a[i].mx = a[i].value = e[i - n].w;
a[i].id = i;
}
}
inline void pushdown(int x)
{
if (a[x].tag)
{
swap(a[x].son[] , a[x].son[]);
a[a[x].son[]].tag ^= ;
a[a[x].son[]].tag ^= ;
a[x].tag = false;
}
}
inline bool get(int x)
{
pushdown(a[x].father);
return a[a[x].father].son[] == x;
}
inline bool nroot(int x)
{
return a[a[x].father].son[] == x | a[a[x].father].son[] == x;
}
inline void rotate(int x)
{
int f = a[x].father , g = a[f].father;
int tmpx = get(x) , tmpf = get(f);
int w = a[x].son[tmpx ^ ];
if (nroot(f)) a[g].son[tmpf] = x;
a[x].son[tmpx ^ ] = f;
a[f].son[tmpx] = w;
if (w) a[w].father = f;
a[f].father = x;
a[x].father = g;
update(f);
}
inline int find_root(int x)
{
access(x);
splay(x);
while (a[x].son[])
{
pushdown(x);
x = a[x].son[];
}
return x;
}
inline void access(int x)
{
for (int y = ; x; x = a[y = x].father)
{
splay(x);
a[x].son[] = y;
update(x);
}
}
inline void splay(int x)
{
int y = x , z = ;
static int st[MAXN];
st[++z] = y;
while (nroot(y)) st[++z] = y = a[y].father;
while (z) pushdown(st[z--]);
while (nroot(x))
{
int y = a[x].father , z = a[y].father;
if (nroot(y))
rotate((a[y].son[] == x) ^ (a[z].son[] == y) ? x : y);
rotate(x);
}
update(x);
}
inline void split(int x , int y)
{
make_root(x);
access(y);
splay(y);
}
inline void make_root(int x)
{
access(x);
splay(x);
a[x].tag ^= true;
pushdown(x);
}
inline void link(int x , int y)
{
make_root(x);
if (find_root(y) != x) a[x].father = y;
}
inline void cut(int x , int y)
{
make_root(x);
if (find_root(y) == x && a[x].father == y && !a[x].son[])
{
a[x].father = a[y].son[] = ;
update(y);
}
}
inline int query(int x)
{
return a[x].id;
}
} LCT; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
inline bool cmp(edge a , edge b)
{
return a.w < b.w;
}
inline int get_root(int x)
{
if (fa[x] == x) return x;
else return fa[x] = get_root(fa[x]);
} int main()
{ read(n); read(m); read(q);
for (int i = ; i <= m; i++)
{
read(e[i].u);
read(e[i].v);
read(e[i].w);
if (e[i].u > e[i].v) swap(e[i].u , e[i].v);
}
sort(e + , e + m + , cmp);
map< pair<int , int> , int> mp;
for (int i = ; i <= m; i++)
mp[make_pair(e[i].u , e[i].v)] = i;
for (int i = ; i <= q; i++)
{
read(que[i].type);
read(que[i].u);
read(que[i].v);
if (que[i].u > que[i].v) swap(que[i].u , que[i].v);
if (que[i].type == )
flg[mp[make_pair(que[i].u , que[i].v)]] = true;
}
LCT.init();
for (int i = ; i <= n; i++) fa[i] = i;
for (int i = ; i <= m; i++)
{
if (flg[i]) continue;
int su = get_root(e[i].u) , sv = get_root(e[i].v);
if (su != sv)
{
fa[su] = sv;
LCT.link(e[i].u , i + n);
LCT.link(e[i].v , i + n);
}
}
vector< int > ans;
for (int i = q; i >= ; i--)
{
if (que[i].type == )
{
LCT.split(que[i].u , que[i].v);
ans.push_back(e[LCT.query(que[i].v) - n].w);
} else
{
LCT.split(que[i].u , que[i].v);
int id = LCT.query(que[i].v);
if (e[mp[make_pair(que[i].u , que[i].v)]].w < e[id - n].w)
{
LCT.cut(e[id - n].u , id);
LCT.cut(e[id - n].v , id);
LCT.link(que[i].u , mp[make_pair(que[i].u , que[i].v)] + n);
LCT.link(que[i].v , mp[make_pair(que[i].u , que[i].v)] + n);
}
}
}
reverse(ans.begin() , ans.end());
for (unsigned i = ; i < ans.size(); i++) printf("%d\n" , ans[i]); return ;
}

[WC 2006] 水管局长的更多相关文章

  1. 数据结构(动态树):COGS 27. [WC 2006] 水管局长

    27. [WC 2006] 水管局长 ★★★☆   输入文件:tube.in   输出文件:tube.out   简单对比时间限制:3 s   内存限制:128 MB [问题描述 ] SC 省 MY ...

  2. [WC 2006]水管局长数据加强版

    Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一 ...

  3. 解题:WC 2006 水管局长

    题面 初见LCT,动态最小生成树+链上查询max,具体做法是把边转换成点(LCT只能维护点) 时光倒流,先把最后剩的连起来.然后查询就看链上最大值,修改看看链上最大值是否大于当前边,如果是就断开原来的 ...

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

    Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一 ...

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 【OpenGL】OpenGL帧缓存对象(FBO:Frame Buffer Object) 【转】

    http://blog.csdn.net/xiajun07061225/article/details/7283929/ OpenGL Frame BufferObject(FBO) Overview ...

  2. python 工具 图片批量合并

    注:代码两处设置 region = (4,3,x-3,y-5) 目的是crop剪去图片的白边,这个可以视情况改变 图片需要命名为   x_1.png   .....这样的格式 #encoding=ut ...

  3. 转: 多版本并发控制(MVCC)在分布式系统中的应用 (from coolshell)

    from:  http://coolshell.cn/articles/6790.html 问题 最近项目中遇到了一个分布式系统的并发控制问题.该问题可以抽象为:某分布式系统由一个数据中心D和若干业务 ...

  4. 我对Lamport Logical Clock的理解

    建议先看论文原文再来看这篇文章,我不会对论文中的各个点都具体说明.仅仅是写一些我自己的想法,帮助理解. 大家都知道.分布式环境下.确定各个事件发生的顺序非常重要,不然就会发生一些麻烦的问题. 考虑一下 ...

  5. Java序列化算法

    Serialization(序列化)是一种将对象以一连串的字节描述的过程:反序列化deserialization是一种将这些字节重建成一个对象的过程.java序列化API提供一种处理对象序列化的标准机 ...

  6. Lightoj 1088 - Points in Segments 【二分】

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1088 题意: 有一维的n个点和q条线段.询问每条线段上的点有多少个. 思路:寻 ...

  7. ssm 网页

    http://stackoverflow.com/questions/14545872/bean-named-xxx-must-be-of-typexxx-but-was-actually-of-ty ...

  8. Intel平台map

  9. 网站web.cofig配置用户的权限

    访问被拒绝. 说明: 访问服务此请求所需的资源时出错.服务器可能未配置为访问所请求的 URL. 错误消息 401.2.: 未经授权: 服务器配置导致登录失败.请验证您是否有权基于您提供的凭据和 Web ...

  10. Unable to run Kiwi tests on iOS8 device

    本文转载至 http://stackoverflow.com/questions/25871601/unable-to-run-kiwi-tests-on-ios8-device 5down vote ...