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掉瓶颈边,加上当前边. 那怎么维护瓶颈边呢?把边也看做点,向两 ...
随机推荐
- BZOJ4154: [Ipsc2015]Generating Synergy
Description 给定一棵以1为根的有根树,初始所有节点颜色为1,每次将距离节点a不超过l的a的子节点染成c,或询问点a的颜色 Input 第一行一个数T,表示数据组数 接下来每组数据的第一 ...
- ubuntu 装机及装机以后干的事情
一.装系统 下载ubuntu镜像 ubuntu 16.04 镜像下载(linux公社) 安装unetbootin (u盘启动盘制作工具) sudo apt-get install unetbootin ...
- mysqldump备份详解
-A 备份所有-B 恢复时会自动创建库 (同时支持导出多个库 -B db01 db02) -d 导出表结构 #库中有多个表导出时导出没加 –B参数,则要先导入结构,如果表结构没有备份,那就 ...
- Xss里img标签的一些利用
<img src=x onerror=with(document)body.appendChild(document.createElement('script')).src="//x ...
- javascript平时小例子④(setInterval使用2)
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>& ...
- Oracle临时表
1概念理解 ORACLE系统的临时表常被用于存放系统操作的中间数据,由于对临时的任何操作都不产生redo(但会因为修改undo而产生redo),所以临时表的数据操作效率一般都比较高.常用的临时表主要有 ...
- jquery 多选框的问题
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Java面试题大全(二)
41.是否可以继承String类? String类是final类故不可以继承. 42.swtich是否能作用在byte上,是否能作用在long上,是否能作用在String上? switch(expr1 ...
- GridView的详细用法
l GridView无代码分页排序 l GridView选中,编辑,取消,删除 l GridView正反双向排序 l GridView和下拉菜单DropDownList结合 l GridView和Ch ...
- centos6 搭建ELK
mark一下时间:2016年2月19日10:17:09 记录使用 Logstash: Logstash服务的组件,用于处理传入的日志. Elasticsearch: 存储所有日志 Kibana 4: ...