传送门

洛谷
BZOJ

Solution

如果不需要动态的话,那就是一个裸的最小生成树上的最大边权对吧。
现在动态了的话,把这个过程反着来,就是加边对吧。
现在问题变成了怎么动态维护加边的最小生成树,这是一个比较常见的套路了,使用LCT(暴力)。。。
深刻理解一下Kruscal的过程,就是每一次选不在连通块内的最小边权。
那么我们如果加了一条边,显然会形成一个环,我们只要删除这个环里面最大的边权就好了。
所以LCT动态维护路径上的最大值然后根据情况\(Cut*2+Link*2\)就好了。。。

代码实现

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<iostream>
using namespace std;
#define re register
#define ll long long
inline int gi()
{
    int f=1,sum=0;char ch=getchar();
    while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
    return f*sum;
}
typedef pair<int,int> pii;
#define mp make_pair
const int N=100010,Mm=1000010;
struct edge{int u,v,w;bool operator<(const edge &b)const{return w<b.w;}}e[Mm];
struct ques{int opt,x,y;}q[Mm];
struct node{int ff,ch[2],rev,mx,val;}t[(N+Mm)<<1];
map<pii,int>M;
int n,m,Q,Cut[Mm],ans[Mm];
void init(int id,int val){t[id].ch[0]=t[id].ch[1]=0;t[id].rev=0;t[id].ff=0;t[id].val=t[id].mx=val;}
namespace LinkCutTree
{
    int sta[(N+Mm)<<1],top;
    void pushup(int x)
    {
        t[x].mx=t[x].val;
        if(e[t[x].mx].w<e[t[t[x].ch[0]].mx].w)t[x].mx=t[t[x].ch[0]].mx;
        if(e[t[x].mx].w<e[t[t[x].ch[1]].mx].w)t[x].mx=t[t[x].ch[1]].mx;
    }
    void reverse(int x){t[x].rev^=1;swap(t[x].ch[0],t[x].ch[1]);}
    void pushdown(int x){if(!t[x].rev)return;if(t[x].ch[0])reverse(t[x].ch[0]);if(t[x].ch[1])reverse(t[x].ch[1]);t[x].rev^=1;}
    bool isroot(int x){return (t[t[x].ff].ch[0]!=x)&(t[t[x].ff].ch[1]!=x);}
    void rotate(int x)
    {
        int y=t[x].ff,z=t[y].ff;
        if(!isroot(y))t[z].ch[t[z].ch[1]==y]=x;
        t[x].ff=z;int k=t[y].ch[1]==x;
        t[y].ch[k]=t[x].ch[k^1];t[t[x].ch[k^1]].ff=y;
        t[x].ch[k^1]=y;t[y].ff=x;
        pushup(y);
    }
    void splay(int x)
    {
        sta[++top]=x;
        for(int pos=x;!isroot(pos);pos=t[pos].ff)sta[++top]=t[pos].ff;
        while(top)pushdown(sta[top--]);
        while(!isroot(x))
        {
            int y=t[x].ff,z=t[y].ff;
            if(!isroot(y))
                (t[y].ch[1]==x)^(t[z].ch[1]==y)?rotate(x):rotate(y);
            rotate(x);
        }
        pushup(x);
    }
    void access(int x){for(int y=0;x;y=x,x=t[x].ff)splay(x),t[x].ch[1]=y,pushup(x);}
    void makeroot(int x){access(x);splay(x);reverse(x);}
    int findroot(int x){access(x);splay(x);while(t[x].ch[0])x=t[x].ch[0];return x;}
    void split(int x,int y){makeroot(x);access(y);splay(y);}
    void link(int x,int y){makeroot(x);t[x].ff=y;}
    void cut(int x,int y){split(x,y);t[x].ff=t[y].ch[0]=0;}
}
using namespace LinkCutTree;
int main()
{
    n=gi();m=gi();Q=gi();
    for(int i=1;i<=m;i++){int u=gi(),v=gi(),w=gi();if(u>v)swap(u,v);e[i]=(edge){u,v,w};}
    sort(e+1,e+m+1);
    for(int i=1;i<=m;i++)M[mp(e[i].u,e[i].v)]=i;
    for(int i=1;i<=n+m;i++)init(i,i<=n?0:i-n);
    int tot=0;
    for(int i=1;i<=Q;i++){int opt=gi(),x=gi(),y=gi();if(x>y)swap(x,y);q[i]=(ques){opt,x,y};if(q[i].opt==2)Cut[M[mp(q[i].x,q[i].y)]]=1;}
    for(int i=1;i<=m;i++)
        if(!Cut[i])
        {
            int u=e[i].u,v=e[i].v;
            if(tot==n-1)break;
            if(findroot(u)!=findroot(v))
            {
                link(u,i+n);
                link(v,i+n);
                tot++;
            }
        }
    for(int i=Q;i>=1;i--)
    {
        int x=q[i].x,y=q[i].y;
        split(x,y);
        if(q[i].opt&1){ans[i]=e[t[y].mx].w;}
        else
        {
            int id=M[mp(x,y)],d=t[y].mx;
            if(e[d].w<=e[id].w)continue;
            cut(e[d].u,d+n);cut(d+n,e[d].v);
            link(x,id+n);link(y,id+n);
        }
    }
    for(int i=1;i<=Q;i++)
        if(q[i].opt&1)printf("%d\n",ans[i]);
    return 0;
}

【洛谷4172】 [WC2006]水管局长(LCT)的更多相关文章

  1. 洛谷.4172.[WC2006]水管局长(LCT Kruskal)

    题目链接 洛谷(COGS上也有) 不想去做加强版了..(其实处理一下矩阵就好了) 题意: 有一张图,求一条x->y的路径,使得路径上最长边尽量短并输出它的长度.会有<=5000次删边. 这 ...

  2. 洛谷4172 WC2006水管局长(LCT维护最小生成树)

    这个题和魔法森林感觉有很相近的地方啊 同样也是维护一个类似最大边权最小的生成树 但是不同的是,这个题是有\(cut\)和询问,两种操作.... 这可如何是好啊? 我们不妨倒着来考虑,假设所有要\(cu ...

  3. 洛谷 4172 [WC2006]水管局长

    [题解] 我们把操作倒过来做,就变成了加边而不是删边.于是用LCT维护动态加边的最小生成树就好了.同样要注意把边权变为点权. #include<cstdio> #include<al ...

  4. 洛谷P4172 [WC2006]水管局长 (LCT,最小生成树)

    洛谷题目传送门 思路分析 在一个图中,要求路径上最大边边权最小,就不难想到最小生成树.而题目中有删边的操作,那肯定是要动态维护啦.直接上LCT维护边权最小值(可以参考一下蒟蒻的Blog) 这时候令人头 ...

  5. 洛谷P4172 [WC2006]水管局长(lct求动态最小生成树)

    SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一条从A至B的水管的路径, ...

  6. [洛谷P4172] WC2006 水管局长

    问题描述 SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一条从A至B的水 ...

  7. P4172 [WC2006]水管局长 LCT维护最小生成树

    \(\color{#0066ff}{ 题目描述 }\) SC 省 MY 市有着庞大的地下水管网络,嘟嘟是 MY 市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的 ...

  8. luogu P4172 [WC2006]水管局长 LCT维护动态MST + 离线

    Code: #include<bits/stdc++.h> #define maxn 1200000 #define N 120000 using namespace std; char ...

  9. 【洛谷P4172】水管局长

    题目大意:给定 N 个点,M 条边的无向图,支持两种操作:动态删边和查询任意两点之间路径上边权的最大值最小是多少. 题解: 引理:对原图求最小生成树,可以保证任意两点之间的路径上边权的最大值取得最小值 ...

  10. P4172 [WC2006]水管局长(LCT)

    P4172 [WC2006]水管局长 LCT维护最小生成树,边权化点权.类似 P2387 [NOI2014]魔法森林(LCT) 离线存储询问,倒序处理,删边改加边. #include<iostr ...

随机推荐

  1. 在MyEclipse中搭建spring-boot+mybatis+freemarker框架

    一.创建项目 1.右键-->New-->Project... 2.选中Maven Project,点击next 3.选中第一个 4.添写Group Id,Artifact Id,选择Com ...

  2. Jenkins自定义变量共享

    https://www.cnblogs.com/junneyang/p/5239480.html https://www.cnblogs.com/Rocky_/p/8317156.html https ...

  3. Springboot异常:org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController'

    今天本菜鸟编写程序时,遇到了一个异常. org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating ...

  4. mac电脑Git提交代码到Github提示git-credential-osxkeychain 验证解决方案

    ## 啊哈哈 这个简单,直接给出当前mac电脑登录账号密码即可,^_*,拿走不谢!!

  5. Chapter5_初始化与清理_数组初始化与可变参数列表

    一.数组初始化 数组是相同类型的,用一个标识符名称封装到一起的一个对象序列或基本类型数据序列.编译器是不允许指定数组的长度的,当使用语句int[] a时,拥有的只是一个符号名,即一个数组的引用,并不拥 ...

  6. 开发快平台(M302I小e开发板系列教程)

    开发快平台(M302I小e开发板系列教程) 开发块平台ESP8266模块相关理解 一. M302I小e开发板源码注释,源码基于:v1.4.0.8-u34.zip 1. user_main.c /*** ...

  7. 微信小程序开发之搞懂flex布局3——Flex Item

    Flex Item flex容器的子元素就是这个容器的flex item. The direct children of a Flex Container (elements with display ...

  8. strchr和strstr 函数

    函数原型:extern char *strchr(char *str,char character) 参数说明:str为一个字符串的指针,character为一个待查找字符.        所在库名: ...

  9. python编程之变量和简单的数据结构

    一.变量 前面我们用python输出了“hello world!” 这次我们在前面加入一行,定义一个变量,然后修改第二行. 添加变量导致Python解释器需要做更多工作.处理第1行代码时,它将文本“H ...

  10. svn的基本使用方法

    一,svn的介绍 Subversion(SVN) 是一个开源的版本控制系統, 也就是说 Subversion 管理着随时间改变的数据. 这些数据放置在一个中央资料档案库(repository) 中. ...