BZOJ 2594: [Wc2006]水管局长数据加强版(kruskal + LCT)
Description
Input
Output
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXV = ;
const int MAXE = ;
#define foreach(iter, v) for(__typeof(v.begin()) iter = v.begin(); iter != v.end(); ++iter)
#define FOR(i, n) for(int i = 0; i < n; ++i)
inline int readint() {
char c = getchar();
while(!isdigit(c)) c = getchar();
int res = ;
while(isdigit(c)) res = res * + c - '', c = getchar();
return res;
}
struct DSU {
int fa[MAXV];
void init(int n) {
for(int i = ; i <= n; ++i)
fa[i] = i;
}
int find_set(int x) {
return fa[x] == x ? x : fa[x] = find_set(fa[x]);
}
void merge(int x, int y) {
fa[find_set(x)] = find_set(y);
}
} dsu;
struct Edge {
int u, v, cost;
bool del, use;
Edge() {}
Edge(int u, int v):
u(u), v(v) {}
int getv(int x) {
return x != u ? u : v;
}
void read() {
u = readint(), v = readint(), cost = readint();
if(u > v) swap(u, v);
del = use = false;
}
bool operator < (const Edge &rhs) const {
return cost < rhs.cost;
}
} edge[MAXE];
bool cmp_uv(const Edge &a, const Edge &b) {
if(a.u != b.u) return a.u < b.u;
return a.v < b.v;
}
struct Operator {
int k, a, b, mid;
void read() {
k = readint(), a = readint(), b = readint();
if(a > b) swap(a, b);
}
} oper[MAXV];
void kruskal(int n, int m) {
sort(edge, edge + m);
dsu.init(n);
for(int i = ; i < m; ++i) if(!edge[i].del) {
if(dsu.find_set(edge[i].u) != dsu.find_set(edge[i].v)) {
edge[i].use = true;
dsu.merge(edge[i].u, edge[i].v);
}
}
}
struct Node {
Node *ch[], *fa;
int id, ans;
bool rev, rt;
} statePool[MAXV + MAXE], *nil;
int ncnt;
vector<int> adj[MAXV];
Node *ptr[MAXV];
void init() {
ptr[] = nil = statePool;
nil->ans = -;
ncnt = ;
}
Node* new_node(int i, Node *f) {
Node *x = statePool + ncnt++;
x->ch[] = x->ch[] = nil; x->fa = f;
x->id = x->ans = i;
x->rev = false; x->rt = true;
return x;
}
void dfs(int u, int fa, Node *f) {
ptr[u] = new_node(-, f);
foreach(it, adj[u]) {
int v = edge[*it].getv(u);
if(v == fa) continue;
dfs(v, u, new_node(*it, ptr[u]));
}
}
int max_id(int a, int b) {
if(a > b) swap(a, b);
if(a == -) return b;
return edge[a].cost > edge[b].cost ? a : b;
}
void update(Node *x) {
x->ans = x->id;
FOR(k, ) x->ans = max_id(x->ans, x->ch[k]->ans);
}
void rotate(Node *x) {
Node *y = x->fa;
int t = (y->ch[] == x);
if(y->rt) y->rt = false, x->rt = true;
else y->fa->ch[y->fa->ch[] == y] = x;
x->fa = y->fa;
(y->ch[t] = x->ch[t ^ ])->fa = y;
(x->ch[t ^ ] = y)->fa = x;
update(y);
}
void modify_rev(Node *x) {
if(x == nil) return ;
x->rev = !x->rev;
swap(x->ch[], x->ch[]);
}
void pushdown(Node *x) {
if(x->rev) {
FOR(k, ) modify_rev(x->ch[k]);
x->rev = false;
}
}
void push(Node *x) {
if(!x->rt) push(x->fa);
pushdown(x);
}
void splay(Node *x) {
push(x);
while(!x->rt) {
Node *f = x->fa, *ff = f->fa;
if(!f->rt) rotate((ff->ch[] == f) == (f->ch[] == x) ? f : x);
rotate(x);
}
update(x);
}
Node *access(Node *x) {
Node *y = nil;
while(x != nil) {
splay(x);
x->ch[]->rt = true;
(x->ch[] = y)->rt = false;
update(x);
y = x; x = x->fa;
}
return y;
}
void be_root(Node *x) {
access(x);
splay(x);
modify_rev(x);
}
void link(Node *x, Node *y) {
be_root(x);
x->fa = y;
}
void cut(Node *x, Node *y) {
be_root(x);
splay(y);
y->ch[]->fa = y->fa;
y->ch[]->rt = true;
y->fa = y->ch[] = nil;
update(y);
}
int query(Node *x, Node *y) {
be_root(x);
return access(y)->ans;
}
int n, m, q;
int main() {
n = readint(), m = readint(), q = readint();
for(int i = ; i < m; ++i) edge[i].read();
for(int i = ; i < q; ++i) oper[i].read();
sort(edge, edge + m, cmp_uv);
for(int i = ; i < q; ++i) if(oper[i].k == ) {
oper[i].mid = lower_bound(edge, edge + m, Edge(oper[i].a, oper[i].b), cmp_uv) - edge;
edge[oper[i].mid].del = true;
}
kruskal(n, m);
sort(edge, edge + m, cmp_uv);
init();
for(int i = ; i < m; ++i) if(edge[i].use)
adj[edge[i].u].push_back(i), adj[edge[i].v].push_back(i);
dfs(, , nil);
vector<int> ans;
for(int i = q - ; i >= ; --i) {
int t = query(ptr[oper[i].a], ptr[oper[i].b]);
if(oper[i].k == ) {
ans.push_back(edge[t].cost);
} else if(edge[t].cost > edge[oper[i].mid].cost) {
cut(ptr[edge[t].u], ptr[edge[t].v]);
Node *x = new_node(oper[i].mid, nil);
link(x, ptr[oper[i].a]);
link(x, ptr[oper[i].b]);
}
}
for(vector<int>::reverse_iterator it = ans.rbegin(); it != ans.rend(); ++it)
printf("%d\n", *it);
}
BZOJ 2594: [Wc2006]水管局长数据加强版(kruskal + LCT)的更多相关文章
- [BZOJ 2594] [Wc2006]水管局长数据加强版 【LCT】
题目链接:BZOJ - 2594 题目分析 这道题如果没有删边的操作,那么就是 NOIP2013 货车运输,求两点之间的一条路径,使得边权最大的边的边权尽量小. 那么,这条路径就是最小生成树上这两点之 ...
- bzoj 2594 [Wc2006]水管局长数据加强版(LCT+最小生成树)
[深坑勿入] [给个链接] http://blog.csdn.net/popoqqq/article/details/41348549 #include<cstdio> #include& ...
- BZOJ 2594: [Wc2006]水管局长数据加强版 [LCT kruskal]
2594: [Wc2006]水管局长数据加强版 Time Limit: 25 Sec Memory Limit: 128 MBSubmit: 2917 Solved: 918[Submit][St ...
- bzoj 2594: [Wc2006]水管局长数据加强版 动态树
2594: [Wc2006]水管局长数据加强版 Time Limit: 25 Sec Memory Limit: 128 MBSubmit: 934 Solved: 291[Submit][Sta ...
- BZOJ 2594: [Wc2006]水管局长数据加强版( LCT )
离线然后就是维护加边的动态MST, Link cut tree秒掉..不过我写+调了好久...时间复杂度O(NlogN + MlogM) ------------------------------- ...
- bzoj 2594: [Wc2006]水管局长数据加强版
Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一 ...
- 【刷题】BZOJ 2594 [Wc2006]水管局长数据加强版
Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一 ...
- 2594. [WC2006]水管局长数据加强版【LCT+最小生成树】
Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一 ...
- BZOJ 2594: [Wc2006]水管局长数据加强版 (LCT维护最小生成树)
离线做,把删边转化为加边,那么如果加边的两个点不连通,直接连就行了.如果联通就找他们之间的瓶颈边,判断一下当前边是否更优,如果更优就cut掉瓶颈边,加上当前边. 那怎么维护瓶颈边呢?把边也看做点,向两 ...
随机推荐
- Java_java动态编译整个项目,解决jar包找不到问题
java动态编译整个项目,解决jar包找不到问题原文:http://itzyx.com/index.php/javac/ 动态将java文件编译为class文件解决方案:将temp\sdl\src目录 ...
- [CareerCup] 18.10 Word Transform 单词转换
18.10 Given two words of equal length that are in a dictionary, write a method to transform one word ...
- easy_install - pip
easy_install 下载地址 解压,安装. python ez_setup.py pip 下载地址 解压,安装. python setup.py install 注意配置环境变量. Instal ...
- ZK 使用Clients.response
参考: http://stackoverflow.com/questions/11416386/how-to-access-au-response-sent-from-server-side-at-c ...
- c# 哈希表跟函数
一.哈希表集合 先进后出,一个一个赋值,但只能一起取值. 1.哈希表的建立.赋值以及读取. 2.利用枚举类型打印出集合中的Key值和Value值. 二.函数 函数:能够独立完成某项功能的模块. 函数四 ...
- 讨论一下js获取响应中后台传回来的BigInteger类型的数字时,后几位会自动变为0的问题
后台返回的json:{"data":12345678912345678912} 在js中获取该data得到的值为:12345678912345680000 后经过实验发现,只有数字 ...
- SpringMVC下的Shiro权限框架的使用
SpringMVC+Shiro权限管理 博文目录 权限的简单描述 实例表结构及内容及POJO Shiro-pom.xml Shiro-web.xml Shiro-MyShiro-权限认证,登录认证层 ...
- HTML-embed标签详解
Embed(一).基本语法:embed src=url说明:embed可以用来插入各种多媒体,格式可以是 Midi.Wav.AIFF.AU.MP3等等, Netscape及新版的IE 都支持 ...
- BizTalk开发系列(九) MAP的连接方法
BizTalk中的Map编辑器可以在源架构和目标架构创建连接.有三种创建连接的方式: 1.普通的连接方式,将左边的记录拖到右边. 2.根据结构自动连接,点击MAP的网格,在属性中选择结构(Struct ...
- MATLAB中提供的线型属性
MATLAB中提供的线型属性有: 线型 说明 标记符 说明 颜色 说明 - 实线(默认) + 加号符 r 红色 -- 双划线 o 空心圆 g 绿色 : 虚线 * 星号 b 蓝色 :. 点划线 . 实心 ...