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 ...
随机推荐
- IOS 蓝牙(GameKit、Core Bluetooth)
GameKit的蓝牙开发注意 ● 只能用于iOS设备之间的连接 ● 只能用于同一个应用程序之间的连接 ● 最好别利用蓝牙发送比较大的数据 /* 关于蓝牙的数据传输 1. 一次性传送,没有中间方法,所 ...
- libevent将信号封装为socket通知的核心代码
#include"stdafx.h" #include"iostream" #include "algorithm" #include&qu ...
- Android(java)学习笔记13:线程组的概述和使用
1. Java中使用ThreadGroup来表示线程组,它可以对一批线程进行分类管理,Java允许程序直接对线程组进行控制. (1)默认情况下,所有的线程都属于主线程组. public final T ...
- 【luogu P1262 间谍网络】 题解
题目链接:https://www.luogu.org/problemnew/show/P1262 注意: 1.缩点时计算出入度是在缩完点的图上用color计算.不要在原来的点上计算. 2.枚举出入度时 ...
- 【Openjudge 9277 Logs Stacking堆木头】 题解
题目链接:http://noi.openjudge.cn/ch0206/9277/ ... #include <algorithm> #include <iostream> # ...
- 关于css透明度的问题
先看background和background-color background:可以设置背景颜色,背景图片,还有定位.默认background:no-repeat; background-color ...
- js事件委托代码优化【感悟总结】
前两天接手了同事的一个项目,是一个网站首页,其中有段代码很累赘,要实现的功能就是, 通过给父元素添加鼠标移入移出事件,来控制子元素显示隐藏. html代码,一共有四个父元素div,每个父元素嵌套一个子 ...
- POST和GET请求区别
最新博客站点:欢迎来访 1. 请求长度的限制 在HTTP协议中,从未规定GET/POST的请求长度限制,对于GET,对url的限制来源于浏览器或web服务器,浏览器和服务器限制了url的长度.因此,在 ...
- 数论(一)LOJ1282
1.题目来源LOJ1282 You are given two integers: n and k, your task is to find the most significant three d ...
- Flask中异常捕获
HTTP 异常主动抛出 abort 方法 抛出一个给定状态代码的 HTTPException 或者 指定响应,例如想要用一个页面未找到异常来终止请求,你可以调用 abort(404). 参数: cod ...