以前一直觉得lct特别难写,自从学了丽洁姐的lct之后,觉得lct居然能这么短,这个主程序能40行左右解决~~~~

这道嘛~~虽说能用spfa解决,但还是写下lct吧

把边按a值排序后一条一条插入并维护bmx值就行了= =

不过还是得膜拜云神在考场上a了这道题,我花了好久,自己还是太弱

NOIP就要来了,在这里祝自己RP++吧

毕竟升高二后,每天几乎都是怀着梦想睡觉的

希望这一年能不辜负我的愿望吧

向着最高点,冲吧!!!

CODE:

#include<cstdio>

#include<iostream>

#include<cstring>

#include<algorithm>

#include<map>

using namespace std;

#define maxn 101000

struct node{

node *ch[2],*p,*mx;bool rec;int val;

node(int v=0):val(v) {

rec=false;

p=ch[0]=ch[1]=NULL;

mx=this;

}

inline bool d();

inline bool isroot();

inline void update();

inline void relax();

}t[maxn];

inline bool node::d(){return p->ch[1]==this;}

inline bool node::isroot(){

return (p==NULL)||(p->ch[1]!=this&&p->ch[0]!=this);

}

inline void node::relax(){

if (!this) return;

if (!rec) return ;

if (ch[1]) ch[1]->rec^=1;

if (ch[0]) ch[0]->rec^=1;

swap(ch[1],ch[0]);

rec=0;

}

inline void node::update(){

if (!this) return ;

mx=this;

if (ch[0]&&ch[0]->mx->val > mx->val ) mx=ch[0]->mx;

if (ch[1]&&ch[1]->mx->val > mx->val ) mx=ch[1]->mx;

}

inline void rotate(node*u)  {

node *v=u->p;

v->relax();u->relax();

bool d=u->d();

if (!v->isroot()) v->p->ch[v->d()]=u;

u->p=v->p;

if (u->ch[d^1]) u->ch[d^1]->p=v;

v->ch[d]=u->ch[d^1];

u->ch[d^1]=v;

v->p=u;

v->update(),u->update();

}

inline void splay(node *u) {

u->relax();

while (!u->isroot()){

if (u->p->isroot()) rotate(u);

else if (u->d()!=u->p->d()) rotate(u),rotate(u);

else rotate(u->p),rotate(u);

}

u->update();

}

inline node* expose(node *u) {

node *v;

for (v=NULL;u;v=u,u=u->p){

splay(u);

u->ch[1]=v;

u->update();

}

return v;

}

inline void evert(node *u) {

expose(u)->rec^=1;

splay(u);

}

inline void link(node *u,node *v) {

evert(u);

u->p=v;

expose(u);

}

inline void cut(node *u,node *v) {

evert(u);

expose(v);

splay(v);

v->ch[0]=u->p=NULL;

v->update();u->update();

}

inline int query(node *u,node *v) {

evert(u);

expose(v);

splay(v);

return v->mx->val;

}

int fa[maxn];

inline int find(int x) {

if (fa[x]!=x) fa[x]=find(fa[x]);

return fa[x];

}

int x[maxn],y[maxn],a[maxn],b[maxn],id[maxn];

bool cmp(int p,int q) {return a[p]<a[q];}

map<node*,int> ma;

int n,m;

int main(){

scanf("%d%d",&n,&m);

for (int i=1;i<=n;i++) fa[i]=i;

for (int i=1;i<=m;i++) {

scanf("%d%d%d%d",x+i,y+i,a+i,b+i);

id[i]=i;

}

sort(id+1,id+1+m,cmp);

int ans=0x7fffffff;

for (int i=1;i<=m;i++) {

if (x[id[i]]==y[id[i]]) continue;

node *u=&t[x[id[i]]],*v=&t[y[id[i]]];

if (find(x[id[i]])!=find(y[id[i]])) {

node *e=new node(b[id[i]]);

ma[e]=id[i];

link(u,e);

link (v,e);

fa[find(x[id[i]])]=find(y[id[i]]);

}else {

evert(u);

expose(v);

splay(v);

if (v->mx->val>b[id[i]]){

node *e=v->mx;

splay(e);

node *_u=&t[x[ma[e]]],*_v=&t[y[ma[e]]];

cut(e,_u);

cut(e,_v);

delete e;

e=new node(b[id[i]]);

ma[e]=id[i];

link (u,e);

link (v,e);

}

}

if (find(1)==find(n)) ans=min(ans,a[id[i]]+query(&t[1],&t[n]));

}

if (ans==0x7fffffff) ans=-1;

printf("%d\n",ans);

return 0;

}

[Noi2014]魔法森林( 动态mst lct)的更多相关文章

  1. bzoj 3669: [Noi2014]魔法森林 动态树

    3669: [Noi2014]魔法森林 Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 363  Solved: 202[Submit][Status] ...

  2. BZOJ3669: [Noi2014]魔法森林(瓶颈生成树 LCT)

    Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 3558  Solved: 2283[Submit][Status][Discuss] Descript ...

  3. [bzoj] 3669 NOI2014 魔法森林 || LCT

    原题 copy一篇题解:原链接 将边按照a排序,然后从小到大枚举,加入图中. 在图中用lct维护一棵两点之间b最大值尽量小的生成树. 当加入一条边(u, v)时: 如果(u, v)不连通,则直接加入这 ...

  4. BZOJ 3669 [Noi2014]魔法森林(贪心+LCT)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3669 [题目大意] 给出一张图,每条边上有两个值ai和bi,现在需要求出一条1到N的路 ...

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

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

  6. P2387 [NOI2014]魔法森林(LCT)

    P2387 [NOI2014]魔法森林 LCT边权维护经典题 咋维护呢?边化为点,边权变点权. 本题中我们把边对关键字A进行排序,动态维护关键字B的最小生成树 加边后出现环咋办? splay维护最大边 ...

  7. loj2245 [NOI2014]魔法森林 LCT

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

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

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

  9. [NOI2014]魔法森林 LCT

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

随机推荐

  1. Treap初步

    模板题 bzoj3224: Tyvj 1728 普通平衡树 #include <bits/stdc++.h> #define rep(i, a, b) for (int i = a; i ...

  2. IOS开发-UI学习-沙盒机制&文件操作

    ž苹果为软件的运行提供了一个沙盒机制 每个沙盒含有3个文件夹:Documents, Library 和 tmp.因为应用的沙盒机制,应用只能在几个目录下读写文件 žDocuments:苹果建议将程序中 ...

  3. Linux内核探索之路——关于方法

    转载自:http://blog.chinaunix.net/uid-20608849-id-3014502.html   Linux内核实践之路 -给那些想从Linux内核找点乐趣的人 一个不能回避的 ...

  4. SQLite高级:一库建多表,封装类

    package eoe.database; import android.content.Context; import android.database.sqlite.SQLiteDatabase; ...

  5. datalog

    https://en.wikipedia.org/wiki/Datalog http://www.csd.uoc.gr/~hy562/1112_spring/instr_material/WhatYo ...

  6. Django中扩展Paginator实现分页

    Reference:https://my.oschina.net/kelvinfang/blog/134342 Django中已经实现了很多功能,基本上只要我们需要的功能,都能够找到相应的包.要在Dj ...

  7. Compilation err ororg.eclipse.jdt.internal.compiler.classfmt.ClassFormatException

    严重: Compilation errororg.eclipse.jdt.internal.compiler.classfmt.ClassFormatExceptionat org.eclipse.j ...

  8. 最通用的ibatis.Net使用sql server存储过程返回分页数据的详细例子

    ibatis.Net是一个比较简单和灵活的ORM框架,今天我分享一个我的项目中使用sql server通用存储过程来分页的一个例子,用ibatis.Net框架统一返回分页数据为IList<Has ...

  9. iOS 之 #import与#include的区别及@class

    #import 相比#include不会引起交叉编译. @class一般用于头文件中需要声明该类的变量时用到

  10. jQuery 鼠标滚轮插件 jquery.mousewheel.js

    jQuery Mousewheel Plugin,用于添加跨浏览器的鼠标滚轮支持.mousewheel事件的处理函数有一点小小的变化,它除了第一个参数event 外,还接收到第二个参数delta.通过 ...