感觉码力严重下降~

#include <bits/stdc++.h>
#define N 400006
#define inf 1000000000
#define setIO(s) freopen(s".in","r",stdin)
using namespace std;
multiset<int>S;
multiset<int>::iterator it;
struct Edge
{
int u,v,c;
Edge(int u=0,int v=0,int c=0):u(u),v(v),c(c){}
}e[N];
bool cmp(Edge a,Edge b)
{
return a.c<b.c;
}
struct Union
{
int p[N];
void init()
{
for(int i=1;i<N;++i) p[i]=i;
}
int find(int x)
{
return p[x]==x?x:p[x]=find(p[x]);
}
int merge(int x,int y)
{
x=find(x),y=find(y);
if(x!=y)
{
p[x]=y;
return 1;
}
return 0;
}
}ufs;
struct Link_Cut_Tree
{
#define lson t[x].ch[0]
#define rson t[x].ch[1]
int sta[N];
struct Node
{
int ch[2],f,min,id,val,rev;
}t[N];
int isrt(int x)
{
return !(t[t[x].f].ch[0]==x||t[t[x].f].ch[1]==x);
}
int get(int x)
{
return t[t[x].f].ch[1]==x;
}
void pushup(int x)
{
t[x].min=t[x].val, t[x].id=x;
if(lson && t[lson].min<t[x].min) t[x].min=t[lson].min,t[x].id=t[lson].id;
if(rson && t[rson].min<t[x].min) t[x].min=t[rson].min,t[x].id=t[rson].id;
}
void mark(int x)
{
if(x) t[x].rev^=1,swap(lson,rson);
}
void pushdown(int x)
{
if(t[x].rev)
{
if(lson) mark(lson);
if(rson) mark(rson);
t[x].rev=0;
}
}
void rotate(int x)
{
int old=t[x].f,fold=t[old].f,which=get(x);
if(!isrt(old)) t[fold].ch[t[fold].ch[1]==old]=x;
t[old].ch[which]=t[x].ch[which^1], t[t[old].ch[which]].f=old;
t[x].ch[which^1]=old,t[old].f=x,t[x].f=fold;
pushup(old),pushup(x);
}
void splay(int x)
{
int v=0,u=x,fa;
for(sta[++v]=u;!isrt(u);u=t[u].f) sta[++v]=t[u].f;
for(;v;--v) pushdown(sta[v]);
for(u=t[u].f;(fa=t[x].f)!=u;rotate(x))
if(t[fa].f!=u)
rotate(get(fa)==get(x)?fa:x);
}
void Access(int x)
{
for(int y=0;x;y=x,x=t[x].f)
splay(x),rson=y,pushup(x);
}
void makeroot(int x)
{
Access(x),splay(x),mark(x);
}
void link(int x,int y)
{
makeroot(x),t[x].f=y;
}
void cut(int x,int y)
{
makeroot(x),Access(y),splay(y);
t[t[y].ch[0]].f=0;
t[y].ch[0]=0;
pushup(y);
}
void split(int x,int y)
{
makeroot(x),Access(y),splay(y);
}
#undef lson
#undef rson
}op;
int main()
{
int i,j,n,m,ans=inf;
// setIO("input");
scanf("%d%d",&n,&m);
for(i=1;i<=m;++i) scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].c);
sort(e+1,e+1+m,cmp);
ufs.init();
for(i=1;i<=n;++i) op.t[i].val=inf;
for(i=1;i<=m;++i)
{
int x=e[i].u,y=e[i].v,c=e[i].c,_new=i+n;
if(x==y) continue;
if(ufs.merge(x,y))
{
op.t[_new].val=c;
op.link(x,_new);
op.link(_new,y);
S.insert(c);
}
else
{
op.split(x,y);
if(op.t[y].min<c)
{
S.erase(S.find(op.t[y].min));
S.insert(c);
int kk=op.t[y].id;
int xx=e[kk-n].u;
int yy=e[kk-n].v;
op.cut(xx,kk);
op.cut(yy,kk);
op.t[_new].val=c;
op.link(x,_new);
op.link(_new,y);
}
}
if(S.size()>=n-1)
{
it=S.end();
it--;
ans=min(ans,*(it)-*(S.begin()));
}
}
printf("%d\n",ans);
return 0;
}

  

luogu 4234 最小差值生成树 LCT的更多相关文章

  1. Luogu 4234 最小差值生成树 - LCT 维护链信息

    Solution 将边从小到大排序, 添新边$(u, v)$时 若$u,v$不连通则直接添, 若连通则 把链上最小的边去掉 再添边. 若已经加入了 $N - 1$条边则更新答案. Code #incl ...

  2. 洛谷.4234.最小差值生成树(LCT)

    题目链接 先将边排序,这样就可以按从小到大的顺序维护生成树,枚举到一条未连通的边就连上,已连通则(用当前更大的)替换掉路径上最小的边,这样一定不会更差. 每次构成树时更新答案.答案就是当前边减去生成树 ...

  3. 洛谷4234最小差值生成树 (LCT维护生成树)

    这也是一道LCT维护生成树的题. 那么我们还是按照套路,先对边进行排序,然后顺次加入. 不过和别的题有所不同的是: 在本题中,我们需要保证LCT中正好有\(n-1\)条边的时候,才能更新\(ans\) ...

  4. P4234 最小差值生成树 LCT维护边权

    \(\color{#0066ff}{ 题目描述 }\) 给定一个标号为从 \(1\) 到 \(n\) 的.有 \(m\) 条边的无向图,求边权最大值与最小值的差值最小的生成树. \(\color{#0 ...

  5. Luogu P4234 最小差值生成树

    题意 给定一个 \(n\) 个点 \(m\) 条边的有权无向图,求出原图的一棵生成树使得该树上最大边权与最小边权的差值最小. \(\texttt{Data Range:}1\leq n\leq 5\t ...

  6. 洛谷 P4234 最小差值生成树(LCT)

    题面 luogu 题解 LCT 动态树Link-cut tree(LCT)总结 考虑先按边权排序,从小到大加边 如果构成一颗树了,就更新答案 当加入一条边,会形成环. 贪心地想,我们要最大边权-最小边 ...

  7. [luogu4234]最小差值生成树

    [luogu4234]最小差值生成树 luogu 从小到大枚举边,并连接,如果已连通就删掉路径上最小边 lct维护 \(ans=min(E_{max}-E_{min})\) #include<b ...

  8. LuoguP4234_最小差值生成树_LCT

    LuoguP4234_最小差值生成树_LCT 题意: 给出一个无向图,求最大的边权减最小的边权最小的一棵生成树. 分析: 可以把边权从大到小排序,然后类似魔法森林那样插入. 如果两点不连通,直接连上, ...

  9. P4234 最小差值生成树

    题目 P4234 最小差值生成树 做法 和这题解法差不多,稍微变了一点,还不懂就直接看代码吧 \(update(2019.2):\)还是具体说一下吧,排序,直接加入,到了成环情况下,显然我们要把此边代 ...

随机推荐

  1. Java虚拟机内存管理小结

  2. 数据分析—win7+ipython+notebook安装

    先安装python 3.x 然后 cmd 执行 pip3 ipython 然后 cmd 执行 pip3 install jupyter notebook 然后 cmd 执行 jupyter noteb ...

  3. 顶级Python库

    绝不能错过的24个顶级Python库 Python有以下三个特点: · 易用性和灵活性 · 全行业高接受度:Python无疑是业界最流行的数据科学语言 · 用于数据科学的Python库的数量优势 事实 ...

  4. xshell和xftp过期解决办法

    去官网 xshell:https://www.netsarang.com/download/down_form.html?code=522 xftp:https://www.netsarang.com ...

  5. pm2 常用操作

    PM2全局安装 npm i pm2 -g PM2启动.net core pm2 start "dotnet xxx.dll" --name api //name后面跟你要取的名字 ...

  6. 使用cublas 矩阵库函数实现矩阵相乘

    2014-08-10 cublas中执行矩阵乘法运算的函数 首先要注意的是cublas使用的是以列为主的存储方式,和c/c++中的以行为主的方式是不一样的.处理方法可参考下面的注释代码 // SOME ...

  7. SpringBoot-JPA入门

    SpringBoot-JPA入门 JPA就是Spring集成了hibernate感觉. 注解,方法仓库(顾名思义的方法,封装好了,还有自定义的方法). 案例: spring: datasource: ...

  8. JavaScript特点有哪些

    JavaScript特点有哪些 JavaScript 文字脚本语言是一种动态的.弱类型的.基于原型的语言,具有内置的支持类型.它的解释器被称为javascript引擎,是浏览器的一部分,广泛用于客户端 ...

  9. 使用SSH命令行远程登录运行在CloudFoundry上的应用

    当我试图用如下命令行采用SSH远程登录到运行在CloudFoundry环境下的应用时, cf ssh -N -T -L 9229:127.0.0.1:9229 jerry-demo-server 遇到 ...

  10. 【Day4】3.urllib模块使用案例

    import urllib.request as ur ret = ur.urlopen('https://edu.csdn.net/').read() with open('edu.html','w ...