• [NOI2014] 魔法森林


    题目链接:戳我
    对于LCT的操作,维护边的话,必须要把边建成一个点,把边权当成点的权值。
    这个题就是给你一个无向图,然后每条边都有两个值,现在要求你求出从点1到点n的路径上,作为边权的两个值加起来 最小中的最大值是多少。qwq
    有一个比较显然的做法是将其中一个值(比如说a)进行排序,然后按照a从小到大添加边。我们添加的时候不断地维护维护1-n上的最大值就行了。
    加边的时候有两种情况,一种是这两个点已经被连接过了,那么再连接的时候一定会形成一个环,这个是LCT无法维护的东西,所以我们要找出来这两个点之间最大权值的那个边,如果这个边权值比我们的新边大,我们就要把它替换成新边。(只找一个最大的是为了维护树的连通性),另一种是这两个点还没有连接过,那我们直接连上就可以了。
    .maxx意思是这条路径上最大权值的那个边的点编号。
    记得每次判断一下节点1和节点n之间的路径上的最大值,来更新答案。
    整个过程有点像MST??
    写cut的时候注意参数的顺序,还有因为cut之后树会被改变,而因为每次rotate都会push_up,所以如果需要什么关于某个被涉及操作的点的一些值的话,最好先开一个中间变量存一下。qwqwq
    具体参见代码。
    代码如下:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define MAXN 200010
    using namespace std;
    int n,m,tot,ans=2147483647;
    int s[MAXN];
    struct Edge{int ff,val,rev,maxx,ch[2];}t[MAXN];
    struct Edge2{int u,v,a,b;}edge[MAXN];
    inline void push_up(int x)
    {   
        t[x].maxx=x;
        if(t[t[t[x].ch[0]].maxx].val>t[t[x].maxx].val) t[x].maxx=t[t[x].ch[0]].maxx; 
        if(t[t[t[x].ch[1]].maxx].val>t[t[x].maxx].val) t[x].maxx=t[t[x].ch[1]].maxx; 
    }
    inline bool isroot(int x){return t[t[x].ff].ch[0]!=x&&t[t[x].ff].ch[1]!=x;}
    inline bool cmp(struct Edge2 x,struct Edge2 y)
    {
        if(x.a!=y.a) return x.a<y.a;
        return x.b<y.b;
    }
    inline void rotate(int x)
    {
        int y=t[x].ff;
        int z=t[y].ff;
        int k=t[y].ch[1]==x;
        if(!isroot(y)) t[z].ch[t[z].ch[1]==y]=x; t[x].ff=z;
        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;
        push_up(y),push_up(x);
    }
    inline void push_down(int x)
    {
        if(t[x].rev)
        {
            if(t[x].ch[0]) t[t[x].ch[0]].rev^=1;
            if(t[x].ch[1]) t[t[x].ch[1]].rev^=1;
            swap(t[x].ch[0],t[x].ch[1]);
            t[x].rev^=1;
        }
    }
    inline void splay(int x)
    {
        s[tot=1]=x;
        for(int i=x;!isroot(i);i=t[i].ff) s[++tot]=t[i].ff;
        while(tot) push_down(s[tot--]);
        while(!isroot(x))
        {
            int y=t[x].ff;
            int z=t[y].ff;
            if(!isroot(y))
                ((t[y].ch[0]==x)^(t[z].ch[0]==y))?rotate(x):rotate(y);
            rotate(x);
        }
    }
    inline void access(int x)
    {
        for(int y=0;x;y=x,x=t[x].ff)
            splay(x),t[x].ch[1]=y,push_up(x);
    }
    inline void makeroot(int x){access(x);splay(x);t[x].rev^=1;}
    inline void split(int x,int y){makeroot(x);access(y);splay(y);}
    inline void cut(int x,int y){split(x,y);t[y].ch[0]=t[x].ff=0;}
    inline void link(int x,int y){makeroot(x);t[x].ff=y;}
    inline int findroot(int x)
    {
        access(x);splay(x);
        while(t[x].ch[0]) push_down(x),x=t[x].ch[0];
        return x;
    }
    int main()
    {
        #ifndef ONLINE_JUDGE
        freopen("ce.in","r",stdin);
        #endif
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++)
            scanf("%d%d%d%d",&edge[i].u,&edge[i].v,&edge[i].a,&edge[i].b);
        sort(&edge[1],&edge[m+1],cmp);
        for(int i=1;i<=m;i++)
        {
            int u=edge[i].u,v=edge[i].v;
            if(findroot(u)==findroot(v))
            {
                split(u,v);
                int cur=t[v].maxx;
                if(edge[i].b>=t[cur].val) continue;
                cut(edge[cur-n].u,cur),cut(edge[cur-n].v,cur);
            }
            t[i+n].val=edge[i].b;
            link(u,i+n),link(v,i+n);
            if(findroot(1)==findroot(n))
                split(1,n),ans=min(ans,edge[i].a+t[t[n].maxx].val);
        }
        printf("%d
    ",ans==2147483647?-1:ans);
        return 0;
    }
    
  • 相关阅读:
    怎样跟踪来访用户?
    五个瓶颈影响你的Asp.Net程序(网站)性能
    if判断与比较操作符gt、lt、eq等的使用
    裸机LCD驱动配置
    汇编指令-位置无关码(BL)与绝对位置码(LDR)(2)
    汇编指令-MRS(读)和MSR(写)指令操作CPSR寄存器和SPSR寄存器使用(1)
    Nand Flash驱动(实现初始化以及读操作)
    makefile使用.lds链接脚本以及 $@ ,$^, $,< 解析
    makefile初步制作,arm-linux- (gcc/ld/objcopy/objdump)详解
    Liunx-常用命令杂烩(5)
  • 原文地址:https://www.cnblogs.com/fengxunling/p/10289518.html
Copyright © 2020-2023  润新知