luogu4172 [WC2006]水管局长
就是用 lct 维护最小生成树
ref
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <map>
using namespace std;
typedef pair<int,int> par;
int n, m, q, val[200005], zdz[200005], ans[200005], ch[200005][2];
int fa[200005];
bool vis[100005], rev[200005];
map<par,int> mp;
struct Edge{
int fro, too, val;
}edge[100005];
struct Ques{
int opt, x, y;
}qu[100005];
bool cmp(Edge x, Edge y){
return x.val<y.val;
}
bool isRoot(int x){
return ch[fa[x]][0]!=x && ch[fa[x]][1]!=x;
}
int getW(int x){
return ch[fa[x]][1]==x;
}
void upd(int x){
zdz[x] = val[x];
if(edge[zdz[ch[x][0]]].val>edge[zdz[x]].val)
zdz[x] = zdz[ch[x][0]];
if(edge[zdz[ch[x][1]]].val>edge[zdz[x]].val)
zdz[x] = zdz[ch[x][1]];
}
void pushDown(int x){
if(rev[x]){
swap(ch[x][0], ch[x][1]);
rev[ch[x][0]] ^= 1;
rev[ch[x][1]] ^= 1;
rev[x] = false;
}
}
void xf(int x){
if(fa[x]) xf(fa[x]);
pushDown(x);
}
void rotate(int x){
int old=fa[x], oldf=fa[old], w=getW(x);
if(!isRoot(old)) ch[oldf][ch[oldf][1]==old] = x;
ch[old][w] = ch[x][w^1]; ch[x][w^1] = old;
fa[ch[old][w]] = old; fa[ch[x][w^1]] = x; fa[x] = oldf;
upd(old); upd(x);
}
void splay(int x){
xf(x);
while(!isRoot(x)){
int f=fa[x];
if(!isRoot(f))
rotate(getW(x)==getW(f)?f:x);
rotate(x);
}
upd(x);
}
void access(int x){
int y=0;
while(x){
splay(x);
ch[x][1] = y;
upd(x);
y = x;
x = fa[x];
}
}
int findroot(int x){
access(x);
splay(x);
while(ch[x][0])
x = ch[x][0];
splay(x);
return x;
}
void makeroot(int x){
access(x);
splay(x);
rev[x] ^= 1;
}
void split(int x, int y){
makeroot(x);
access(y);
splay(y);
}
void link(int x, int y){
makeroot(x);
fa[x] = y;
}
void cut(int x, int y){
split(x, y);
fa[x] = ch[y][0] = 0;
}
int main(){//anonymous Pro Regular
cin>>n>>m>>q;
for(int i=1; i<=m; i++){
scanf("%d %d %d", &edge[i].fro, &edge[i].too, &edge[i].val);
if(edge[i].fro>edge[i].too) swap(edge[i].fro, edge[i].too);
}
sort(edge+1, edge+1+m, cmp);
for(int i=1; i<=m; i++)
mp[make_pair(edge[i].fro, edge[i].too)] = i;
for(int i=1; i<=q; i++){
scanf("%d %d %d", &qu[i].opt, &qu[i].x, &qu[i].y);
if(qu[i].x>qu[i].y) swap(qu[i].x, qu[i].y);
if(qu[i].opt==2)
vis[mp[make_pair(qu[i].x, qu[i].y)]] = true;
}
for(int i=n+1; i<=n+m; i++)
val[i] = zdz[i] = i - n;
int tmpcnt=0;
for(int i=1; i<=m; i++){
if(tmpcnt==n-1) break;
if(vis[i] || findroot(edge[i].fro)==findroot(edge[i].too)) continue;
link(edge[i].fro, i+n); link(edge[i].too, i+n);
tmpcnt++;
}
for(int i=q; i; i--){
if(qu[i].opt==1){
split(qu[i].x, qu[i].y);
ans[i] = edge[zdz[qu[i].y]].val;
}
else{
split(qu[i].x, qu[i].y);
int idx=mp[make_pair(qu[i].x, qu[i].y)];
int tmp=zdz[qu[i].y];
if(edge[idx].val<edge[tmp].val){
cut(edge[tmp].fro, tmp+n);
cut(edge[tmp].too, tmp+n);
link(edge[idx].fro, idx+n);
link(edge[idx].too, idx+n);
}
}
}
for(int i=1; i<=q; i++)
if(qu[i].opt==1)
printf("%d\n", ans[i]);
return 0;
}
luogu4172 [WC2006]水管局长的更多相关文章
- BZOJ2594: [Wc2006]水管局长数据加强版
题解: 裸LCT+离线+二分+MST... 代码:(几乎摘抄自hzwer) #include<cstdio> #include<cstdlib> #include<cma ...
- 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) ------------------------------- ...
- [bzoj2594][Wc2006]水管局长数据加强版 (lct)
论蒟蒻的自我修养T_T.. 和noi2014魔法森林基本一样...然而数据范围大得sxbk...UPD:这题如果用lct判联通的话可能会被卡到O(mlogm)..所以最好还是用并查集吧 一开始数组开太 ...
- BZOJ 2594: [Wc2006]水管局长数据加强版 [LCT kruskal]
2594: [Wc2006]水管局长数据加强版 Time Limit: 25 Sec Memory Limit: 128 MBSubmit: 2917 Solved: 918[Submit][St ...
- BZOJ_2594_[Wc2006]水管局长数据加强版_LCT
BZOJ_2594_[Wc2006]水管局长数据加强版_LCT Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供 ...
- P4172 [WC2006]水管局长(LCT)
P4172 [WC2006]水管局长 LCT维护最小生成树,边权化点权.类似 P2387 [NOI2014]魔法森林(LCT) 离线存储询问,倒序处理,删边改加边. #include<iostr ...
- P4172 [WC2006]水管局长
P4172 [WC2006]水管局长 前言 luogu数据太小 去bzoj,他的数据大一些 思路 正着删不好维护 那就倒着加,没了 LCT维护他的最小生成树MST 树上加一条边肯定会有一个环 看看环上 ...
- [BZOJ2594][WC2006]水管局长加强版(LCT+Kruskal)
2594: [Wc2006]水管局长数据加强版 Time Limit: 25 Sec Memory Limit: 128 MBSubmit: 4452 Solved: 1385[Submit][S ...
随机推荐
- 大整数乘法(POJ2389)
题目链接:http://poj.org/problem?id=2389 #include <stdio.h> #include <string.h> #define Max 1 ...
- [POI2008]STA-Station
嘟嘟嘟 一道树形dp题. 令dp[u]表示以u为根时所有点的深度之和.考虑u到他的一个子节点v时答案的变化,v子树以外的点的深度都加1,v子树以内的点的深度都减1,所以dp[v] = dp[u] + ...
- 【转】Js中Prototype、__proto__、Constructor、Object、Function关系介绍
一 Prototype.__proto__与Object.Function关系介绍 Function.Object:Js自带的函数对象. prototype,每一个 ...
- python-文件基本操作(二)
在上一篇文章中,简单介绍了打开文件的方法以及关于读.写.追加的操作,点击此处查看. 在此篇文章中,继续介绍另外一种打开文件的方法和几种同时读写的模式. 一.打开文件方法:with 使用file()或o ...
- UVA - 136 Ugly Numbers(丑数,STL优先队列+set)
Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9 ...
- SQL模糊查询,sum,AVG,MAX,min函数
cmd mysql -hlocalhost -uroot -p select * from emp where ename like '___' -- 三个横线, - 代表字符,可以查询 三个enam ...
- 替换html里面的\r\n及解决记事本中的每个段落只有一行的情形
1. 在用python爬取小说的时候, 发现在内容里每次换行都有\r\n(即回车, 换行)出现. 此时可以采用 s.replace('\\r\\n','') , 其中s为字符串类型. 2. 在爬取完 ...
- 最长公共子序列Lcs (51Nod - 1006)
20180604 11:28 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). 比如两个串为: abcicba abdkscab ab是两个串的子序列,ab ...
- 在Windows系统上使用压缩归档文件安装MySQL流程
最近需要做个小小的验证实验,需要安装MySQL,网上一搜发现教程繁多,bug也多,所以直接把官网的流程翻译过来,注意是压缩文件,不是安装版的,解压直接能用的,下面直接把流程贴过来: 使用压缩文档安装在 ...
- python元组操作
元组:(tuple)元素不可被修改,不能被增加或者删除 一般写元组的时候,建议在最后加上一个逗号 可以索引取值 可以切片取值 元组一级元素不可被修改,但是二级及以后可以被修改 count() 获 ...