[题目链接]

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. 【Caffe代码解析】compute_image_mean

    功能: 计算训练数据库的平均图像. 由于平均归一化训练图像会对结果有提升,所以Caffe里面,提供了一个可选项. 用法: compute_image_mean [FLAGS] INPUT_DB [OU ...

  2. 关于Oracle中sysoper这个系统权限的问题

    我们都知道Oracle数据库安装完之后.默认的会有这样几个系统角色或权限.nomal,sysdba,sysoper等等,之前每次登录Oracle的时候.都是直接以conn / as sysdba 的身 ...

  3. mysql 查询表索引的命令详解

    http://hi.baidu.com/wylinux/item/cbc458c2984381300831c651查看索引命令mysql> show index from tblname;mys ...

  4. [Node.js] 關於 console.log 的格式化輸出

    Node.js 當中的 console.log,除了基本字串的輸出之外,還可以利用 %s.%d.%j 格式化的輸出,就讓我們來看些例子吧! 一.範例1 (字串輸出):console.js consol ...

  5. PCIE、UART、HDA、I2C、SMBUS、SPI、eSPI、USB、PS2、CAN、SDIO等数据传输协议简介

    M.2 wife一般支持USB.SDIO.PCIE三种传输 1.摄像头 (1)MIPI CSI (2)USB mipi摄像头模组IC简单便宜(小),应为一般把ADC解码在CPU端. MIPI摄像头简介 ...

  6. Android中Environment与StatFs获取系统/SDCard存储空间大小

    近期想起Android开发的知识.好久没有使用了,都忘得几乎相同了,今天查看了一会资料往回捡捡,顺便写下来帮助一下须要的同学. 首先讲述一下Environment与StatFs这两个类,然后介绍它们的 ...

  7. python发送邮件相关问题总结

    一.发送邮件报错:554:DT:SPM 1.报错信息 2.通过查找163报错信息页面,554 DT:SPM的问题如下: 3.将邮件主题中的“test”去除,经过测试,实际上邮件主题包含“test”也能 ...

  8. 目标检测之hough forest---霍夫森林(Hough Forest)目标检测算法

     Hough Forest目标检测一种比较时兴的目标检测算法,Juergen Gall在2009的CVPR上提出. Hough Forest听上去像hough变换+Random Forest的结合体, ...

  9. Android-可随意拖动的View

    http://blog.csdn.net/w8320273/article/details/8101687

  10. LeetCode(155)题解--Min Stack

    https://leetcode.com/problems/min-stack/ 题目: Design a stack that supports push, pop, top, and retrie ...