P4172 [WC2006]水管局长
P4172 [WC2006]水管局长
前言
luogu数据太小
去bzoj,他的数据大一些
思路
正着删不好维护
那就倒着加,没了
LCT维护他的最小生成树MST
树上加一条边肯定会有一个环
看看环上最大值和加边的大小
然后选择加不加,改不改
错误
哇,恶心撒
怼着题解都写不出来
最后乱改了一下就A了???
代码
// luogu-judger-enable-o2
#include <bits/stdc++.h>
#define ls ch[x][0]
#define rs ch[x][1]
#define FOR(i,a,b) for(int i=a;i<=b;++i)
using namespace std;
const int N=1e6+7;
const int inf=0x3f3f3f3f;
int read() {
int x=0,f=1;char s=getchar();
for(;s>'9'||s<'0';s=getchar()) if(s=='-') f=-1;
for(;s>='0'&&s<='9';s=getchar()) x=x*10+s-'0';
return x*f;
}
int n,m,qwq,ans[N];
map<int,int> MVP[N];
//MVP[1200][1200]; luogu
struct node {
int u,v,q,id;
bool operator < (const node &b) const {
return q<b.q;
}
}G[N];
map<pair<int,int>,int> mdzz;
struct query {
int opt,x,y,id;
}Q[N];
int f[N],ch[N][2],s[N],lazy[N];
int val[N];
void pushup(int x) {
s[x]=x;
if(val[s[ls]] > val[s[x]]) s[x]=s[ls];
if(val[s[rs]] > val[s[x]]) s[x]=s[rs];
}
void tag(int x) {
swap(ls,rs);
lazy[x]^=1;
}
bool isroot(int x) {
return ch[f[x]][0]==x || ch[f[x]][1]==x;
}
void pushdown(int x) {
if(lazy[x]) {
if(ls) tag(ls);
if(rs) tag(rs);
lazy[x]=0;
}
}
void rotate(int x) {
int y=f[x],z=f[y],k=ch[y][1]==x,m=ch[x][k^1];
if(isroot(y)) ch[z][ch[z][1]==y]=x;
ch[x][k^1]=y;
ch[y][k]=m;
if(m) f[m]=y;
f[x]=z;
f[y]=x;
pushup(y);
}
int q[N];
void splay(int x) {
int y=x,z=0;
q[++z]=y;
while(isroot(y)) q[++z]=y=f[y];
while(z) pushdown(q[z--]);
while(isroot(x)) {
y=f[x],z=f[y];
if(isroot(y)) (ch[y][1]==x)^(ch[z][1]==y) ? rotate(x) : rotate(y);
rotate(x);
}
pushup(x);
}
void access(int x) {
for(int y=0;x;y=x,x=f[x])
splay(x),rs=y,pushup(x);
}
void makeroot(int x) {
access(x);
splay(x);
tag(x);
}
int findroot(int x) {
access(x);
splay(x);
while(ls) pushdown(x),x=ls;
return x;
}
int split(int x,int y) {
makeroot(x);
access(y);
splay(y);
return s[y];
}
void link(int x,int y) {
makeroot(x);
if(findroot(y)!=x) f[x]=y;
}
void cut(int x,int y) {
makeroot(x);
if(findroot(y)==x&&f[x]==y&&!ch[x][1]) {
f[x]=ch[y][0]=0;
pushup(y);
}
}
int suanle_f[N],suanle_siz[N];
int suanle_find(int x) {
return suanle_f[x]==x ? x : suanle_f[x]=suanle_find(suanle_f[x]);
}
void suanle_uu(int x,int y) {
int fx=suanle_find(x),fy=suanle_find(y);
if(suanle_siz[fx] <= suanle_siz[fy]) {
suanle_f[fx]=fy;
if(suanle_siz[fx]==suanle_siz[fy]) suanle_siz[fy]++;
} else {
suanle_f[fy]=fx;
}
}
int main() {
// freopen("a.in","r",stdin);
// freopen("a.out","w",stdout);
n=read(),m=read(),qwq=read();
FOR(i,1,m) suanle_f[i]=i;
FOR(i,1,m) {
G[i].u=read(),G[i].v=read(),G[i].q=read();
if(G[i].u>G[i].v)swap(G[i].u,G[i].v);
}
sort(G+1,G+1+m);
FOR(i,1,m) val[i+n]=G[i].q,mdzz[make_pair(G[i].u,G[i].v)]=i,s[n+i]=n+i;
FOR(i,1,qwq) {
Q[i].opt=read(),Q[i].x=read(),Q[i].y=read();
if(Q[i].x>Q[i].y)swap(Q[i].x,Q[i].y);
if(Q[i].opt==2) {
Q[i].id=mdzz[make_pair(Q[i].x,Q[i].y)];
MVP[Q[i].x][Q[i].y]=1;
}
}
FOR(i,1,m) val[i+n]=G[i].q;
int js=0;
FOR(i,1,m) {
if(MVP[G[i].u][G[i].v]==1) continue;
if(suanle_find(G[i].u)==suanle_find(G[i].v)) continue;
link(G[i].u,i+n);
link(G[i].v,i+n);
suanle_uu(G[i].u,G[i].v);
js++;
if(js==n-1) break;
}
for(int i=qwq;i>=1;--i) {
int x=Q[i].x,y=Q[i].y;
if(Q[i].opt==1) {
int tmp=split(x,y);
ans[i]=val[tmp];
} else {
int tmp=split(x,y);
if(val[tmp] > G[Q[i].id].q) {
cut(G[tmp-n].u,tmp);
cut(G[tmp-n].v,tmp);
link(x,Q[i].id+n);
link(y,Q[i].id+n);
}
}
}
FOR(i,1,qwq) if(Q[i].opt==1) cout<<ans[i]<<"\n";
return 0;
}
P4172 [WC2006]水管局长的更多相关文章
- P4172 [WC2006]水管局长(LCT)
P4172 [WC2006]水管局长 LCT维护最小生成树,边权化点权.类似 P2387 [NOI2014]魔法森林(LCT) 离线存储询问,倒序处理,删边改加边. #include<iostr ...
- 洛谷P4172 [WC2006]水管局长(lct求动态最小生成树)
SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一条从A至B的水管的路径, ...
- P4172 [WC2006]水管局长 LCT维护最小生成树
\(\color{#0066ff}{ 题目描述 }\) SC 省 MY 市有着庞大的地下水管网络,嘟嘟是 MY 市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的 ...
- luogu P4172 [WC2006]水管局长 LCT维护动态MST + 离线
Code: #include<bits/stdc++.h> #define maxn 1200000 #define N 120000 using namespace std; char ...
- [洛谷P4172] WC2006 水管局长
问题描述 SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一条从A至B的水 ...
- 洛谷P4172 [WC2006]水管局长 (LCT,最小生成树)
洛谷题目传送门 思路分析 在一个图中,要求路径上最大边边权最小,就不难想到最小生成树.而题目中有删边的操作,那肯定是要动态维护啦.直接上LCT维护边权最小值(可以参考一下蒟蒻的Blog) 这时候令人头 ...
- Luogu P4172 [WC2006]水管局长
题意 给定一个 \(n\) 个点 \(m\) 条边的图和 \(q\) 次操作,每次操作分为以下两种: 1 u v:查询 \(u\) 到 \(v\) 的一条路径使得边权最大的边的权值最小. 2 u v: ...
- 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 ...
随机推荐
- 9.if/else/elif
简单的条件是通过使用 if/else/elif 语法创建的.条件的括号是允许的,但不是必需的.考虑到基于表的缩进的性质,可以使用 elif 而不是 else/if 来维持缩进的级别. if [expr ...
- Qt & VS2013 报错:There's no Qt version assigned to this project for platform Win32
如果你想了解关于Qt与VS2013开发环境搭建,可以至此翻页. 这里主要分享环境已搭建成功,在构建项目时遇到的报错解决方案. [1]Qt 与 VS2013开发环境构建时报错 报错界面如下: 注意:对话 ...
- CentOS下Yum的$releasever和$basearch的取值
CentOS下Yum源配置文件中如CentOS-Base.repo的$releasever和$basearch的取值 $releasever的值,这个表示当前系统的发行版本,可以通过如下命令查看: r ...
- 批量下载网站图片的Python实用小工具(下)
引子 在 批量下载网站图片的Python实用小工具 一文中,讲解了开发一个Python小工具来实现网站图片的并发批量拉取.不过那个工具仅限于特定网站的特定规则,本文将基于其代码实现,开发一个更加通用的 ...
- Spring源码阅读(六)
这一讲分析spring bean属性注入代码populateBean,源码分析如下 /** * Populate the bean instance in the given BeanWrapper ...
- TCP协议的三次握手
TCP协议是面向连接的通信协议,即在传输数据前先在发送端和接收端建立逻辑连接,然后再传输数据,它提供了两台计算机之间可靠无差错的数据传输. l IP地址:用来唯一表示我们自己的电脑的,是一个网络标示 ...
- Maven依赖中的scope详解,在eclipse里面用maven install可以编程成功,到服务器上用命令执行报VM crash错误
Maven依赖中的scope详解 项目中用了<scope>test</scope>在eclipse里面用maven install可以编译成功,到服务器上用命令执行报VM cr ...
- webStorm 2018 激活
原文地址 https://blog.csdn.net/jiangxinyu50/article/details/79104016 webStorm 2018 激活 今天早上一更新webStorm,之前 ...
- Codeforce 296A - Yaroslav and Permutations
Yaroslav has an array that consists of n integers. In one second Yaroslav can swap two neighboring a ...
- The Little Prince-12/16
The Little Prince-12/16 今天四六级考完了呢,布吉岛大家考的怎么样,会有好多好多奇葩翻译吧,哈哈哈! 突然放出一条16年的笑笑汪~~~今年的也应该会很快出炉了,段子手们准备好!! ...