基本思路:

首先按照weightA升序排序,然后依次在图中加边,并维护起点到终点路径上weightB的最大值

如果加边过程中生成了环,则删除环中weightB最大的边

由于是无向图,点之间没有拓扑序,所以在建立LCT模型时,可以将原图的边也视为点,这样就转化成了维护路径上的点权最大值(Orz Hzwer)

点的连通性可以用并查集维护

AC code:(其实Splay双旋一次时只需要进行3次update,而代码中舍弃了这个优化)

 #include <cstdio>
 #include <cstring>
 #include <algorithm>

 ;
 ;
 const int inf=0x3f3f3f3f;

 struct Edge
 {
     int u,v;
     int weightA,weightB;

     void assign(int _u,int _v,int _wa,int _wb)
     {
         u=_u; v=_v; weightA=_wa; weightB=_wb;
     }
     bool operator < (const Edge& other) const
     {
         return this->weightA < other.weightA;
     }
 };

 Edge elist[maxM];

 inline int getMaxIdx(int u,int v)
 {
     return elist[u].weightB > elist[v].weightB ? u : v;
 } //Let elist[0].weightB = -inf

 struct Splay
 {
     int idx;
     int maxIdx;
     bool rev;
     Splay* child[];
     Splay* parent;

     void init(int id)
     {
         idx=maxIdx=id;
         rev=false;
         child[]=child[]=parent=;
     }
     bool isRoot()
     {
         if(!parent) return true;
         ] && ];
     }
     void update()
     {
         maxIdx=idx;
         ]) maxIdx=getMaxIdx(]->maxIdx);
         ]) maxIdx=getMaxIdx(]->maxIdx);
     }
     void reverse()
     {
         if(!isRoot()) parent->reverse();
         if(rev) {
             std::swap(child[],child[]);
             ]) child[]->rev ^= ;
             ]) child[]->rev ^= ;
             ;
         }
     }
     ; }
     void rotate(int dir)
     {
         Splay* temp=parent;
         this->parent=temp->parent;
         if(parent)
         {
             ]) parent->child[]=this;
             ]) parent->child[]=this;
         }
         temp->child[dir^]=this->child[dir];
         if(child[dir]) child[dir]->parent=temp;
         child[dir]=temp;
         temp->parent=this;
         temp->update();
         this->update();
     }
     void splay()
     {
         reverse();
         while(!isRoot())
         {
             );
             ]) st|=;
             ;
             if(!parent->isRoot())
             {
                 ]) st|=;
                 ;
             }
             switch(st)
             {
             : rotate(); break;
             : rotate(); break;
             : parent->rotate(); ); break;
             : rotate(); rotate(); break;
             : rotate(); rotate(); break;
             :parent->rotate(); ); break;
             }
         }
     }
 };

 Splay node[maxN+maxM];

 int access(int idx)
 {
     Splay *cur,*last;
     ;cur;last=cur,cur=cur->parent)
     {
         cur->splay();
         cur->child[]=last;
         cur->update();
     }
     return last->maxIdx;
 }
 inline void setAsRoot(int idx)
 {
     access(idx);
     node[idx].splay();
     node[idx].setRev();
 }
 inline void link(int u,int v)
 {
     setAsRoot(u);
     node[u].parent=node+v;
 }
 inline void cut(int u,int v)
 {
     setAsRoot(u);
     access(v);
     node[v].splay();
     node[v].child[]=node[u].parent=;
     node[v].update();
 }
 inline int query(int u,int v)
 {
     setAsRoot(u);
     return access(v);
 }

 int N,M;
 int center[maxN];

 int getCenter(int idx)
 {
     return center[idx]==idx ? idx :
         center[idx]=getCenter(center[idx]);
 }
 void input()
 {
     scanf("%d%d",&N,&M);
     int u,v,wa,wb;
     ;i<=M;i++)
     {
         scanf("%d%d%d%d",&u,&v,&wa,&wb);
         if(u==v) { --i; --M; }
         else elist[i].assign(u,v,wa,wb);
     }
 }
 void init()
 {
     elist[].weightB=-inf;
     std::sort(elist+,elist+M+);
     ;i<=M;i++) node[i].init(i);
     ;i<=N;i++) node[i+M].init();
     ;i<=N;i++) center[i]=i;
 }
 void addEdge(int e)
 {
     int& u=elist[e].u;
     int& v=elist[e].v;
     int cu=getCenter(u);
     int cv=getCenter(v);
     if(cu!=cv)
     {
         link(e,M+u);
         link(e,M+v);
         center[cu]=cv;
     }
     else {
         int mx=query(M+u,M+v);
         if(elist[e].weightB < elist[mx].weightB)
         {
             cut(mx,M+elist[mx].u);
             cut(mx,M+elist[mx].v);
             link(e,M+u);
             link(e,M+v);
         }
     }
 }
 int solve()
 {
     int ans(inf);
     init();
     ;i<=M;i++)
     {
         addEdge(i);
         )==getCenter(N))
         {
             ,M+N);
             ans=std::min(ans,elist[i].weightA+elist[mx].weightB);
         }
     }
     :ans;
 }

 int main()
 {
     input();
     printf("%d\n",solve());
     ;
 }

——————分割线——————

这道题我Debug了2天共计5h,最后偶然间查明了死因居然是——

 #ifdef WRONG_SPLAY_CODE
 void Splay::splay()
 {
     reverse();
     while(!isRoot())
     {
         );
         ]) st|=;
         ;
         if(!parent->isRoot())
         {
             ]) st|=;
             ;
         }
         switch(st)
         {
         : rotate(); break;
         : rotate(); break;
         : parent->rotate(); ); break;
         : rotate(); rotate(); break;
         : rotate(); rotate(); break;
         :parent->rotate(); ); break;
         }
     }
 }
 #endif

I FELL COLLAPSED BECAUSE OF THIS

不过在Debug期间,参考了不少其他神犇的AC代码,也学到了各种姿势,算是因祸得福吧……

Vijos1865 NOI2014 魔法森林 LCT维护生成树的更多相关文章

  1. P2387 [NOI2014]魔法森林 LCT维护最小生成树

    \(\color{#0066ff}{ 题目描述 }\) 为了得到书法大家的真传,小 E 同学下定决心去拜访住在魔法森林中的隐 士.魔法森林可以被看成一个包含 n 个节点 m 条边的无向图,节点标号为 ...

  2. 【BZOJ 3669】 [Noi2014]魔法森林 LCT维护动态最小生成树

    这道题看题意是在求一个二维最小瓶颈路,唯一可行方案就是枚举一维在这一维满足的条件下使另一维最小,那么我们就把第一维排序利用A小的边在A大的情况下仍成立来动态加边维护最小生成树. #include &l ...

  3. [Luogu P2387] [NOI2014]魔法森林 (LCT维护边权)

    题面 传送门:https://www.luogu.org/problemnew/show/P2387 Solution 这题的思想挺好的. 对于这种最大值最小类的问题,很自然的可以想到二分答案.很不幸 ...

  4. BZOJ 3669: [Noi2014]魔法森林( LCT )

    排序搞掉一维, 然后就用LCT维护加边MST. O(NlogN) ------------------------------------------------------------------- ...

  5. bzoj 3669: [Noi2014]魔法森林 (LCT)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3669 题面: 3669: [Noi2014]魔法森林 Time Limit: 30 Sec  ...

  6. [NOI2014]魔法森林 LCT

    题面 [NOI2014]魔法森林 题解 一条路径的代价为路径上的\(max(a[i]) + max(b[i])\),因为一条边同时有$a[i], b[i]$2种权值,直接处理不好同时兼顾到,所以我们考 ...

  7. bzoj3669: [Noi2014]魔法森林 lct版

    先上题目 bzoj3669: [Noi2014]魔法森林 这道题首先每一条边都有一个a,b 我们按a从小到大排序 每次将一条路劲入队 当然这道题权在边上 所以我们将边化为点去连接他的两个端点 当然某两 ...

  8. loj2245 [NOI2014]魔法森林 LCT

    [NOI2014]魔法森林 链接 loj 思路 a排序,b做动态最小生成树. 把边拆成点就可以了. uoj98.也许lct复杂度写假了..越卡常,越慢 代码 #include <bits/std ...

  9. bzoj 3669: [Noi2014] 魔法森林 LCT版

    Description 为了得到书法大家的真传,小E同学下定决心去拜访住在魔法森林中的隐士.魔法森林可以被看成一个包含个N节点M条边的无向图,节点标号为1..N,边标号为1..M.初始时小E同学在号节 ...

随机推荐

  1. iso学习网站记录

    [零基础学习iOS开发] http://www.cnblogs.com/mjios/archive/2013/04/24/3039357.html 非零基础学习iOS开发2-Objective-C h ...

  2. 数据库连接&数据库进程&数据库操作

    root@webwall:/home/xiachengjiao# vi/webwall/mysql/my.cnf(看配置文件中的参数) root@webwall:/webwall/mysql/bin# ...

  3. iOS 多线程学习笔记 —— GCD

    本文复制.参考自文章:iOS多线程编程之Grand Central Dispatch(GCD)介绍和使用 ,主要为了加强个人对知识的理解和记忆,不做他用.原作者声明: 著作权声明:本文由http:// ...

  4. HDOJ/HDU 1062 Text Reverse(字符串翻转~)

    Problem Description Ignatius likes to write words in reverse way. Given a single line of text which ...

  5. JavaScript 设计风格&模式 概览 20140418

    基本的概念 在JavaScript中,一旦定义好一个变量,该变量会自动成为内置对象的一个属性,(如果该变量是全局变量,那么会成为全局对象的一个属性). 定义的变量实际上也是一个伪类,拥有自身的属性,该 ...

  6. SRM 389(1-250pt)

    题意:按一定方法生成n个分数,求他们的和.n <= 20 解法:暴力.我只是没想到,10000^20用double算也能被接受0 0 tag:brute-force // BEGIN CUT H ...

  7. 虚拟机linux系统下ifconfig获取不到ip

    原因:网卡未激活 1.输入ifup eth0命令激活网卡 2.输入ifconfig查询ip

  8. winform 播放声音方式 分类: WinForm 2014-07-25 14:16 194人阅读 评论(0) 收藏

    声音文件folder.wav放置在bin目录下debug下 1.通过API调用 [c-sharp] view plaincopy using System.Runtime.InteropService ...

  9. 机房收费系统个人重构关于SQLHelper

    近期在敲机房,对于SQLHelper也是心里有些怵,由于原来没用过,可是看了一些博客和资料后发现,假设不用这个类,会大大添加代码量,并且,事实上它并不新,它是一个有多个关于数据库增删改查操作的语句函数 ...

  10. 将.lib库文件转换成.a库文件的工具

    分享如下两个链接: 微盘: http://vdisk.weibo.com/s/ztzPTJIC52mz2 百度云盘: http://pan.baidu.com/s/11gTOc 使用方法,解压文件mi ...