暖气来啦~

动态树维护最大生成树裸题

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
const int maxn=;
int es[maxn][];
int n,m;
int rt,Size;
struct LCT
{
int son[maxn][],f[maxn],val[maxn],size[maxn],cnt[maxn],rev[maxn],st[maxn];
int t[maxn],len[maxn],mn[maxn],sum[maxn];
inline void update(int x)
{
int &o=x,lch=son[x][],rch=son[x][];
mn[o]=x;sum[o]=len[o];
if(son[x][])
{
if(t[mn[lch]]<t[mn[x]]) mn[o]=mn[lch];
sum[o]+=sum[lch];
}
if(son[x][])
{
if(t[mn[rch]]<t[mn[x]]) mn[o]=mn[rch];
sum[o]+=sum[rch];
}
}
inline int isroot(int x){return son[f[x]][]!=x && son[f[x]][]!=x;}
inline void rotate(int x)
{
int y=f[x],z=f[y],l,r;
if(son[y][]==x)l=;
else l=;r=l^;
if(!isroot(y))
{
if(son[z][]==y)son[z][]=x;
else son[z][]=x;
}
f[x]=z;f[y]=x;f[son[x][r]]=y;
son[y][l]=son[x][r];son[x][r]=y;
update(y);
}
inline void pushdown(int id)
{
int lch=son[id][],rch=son[id][];
if(rev[id])
{
rev[id]^=;rev[lch]^=;rev[rch]^=;
swap(son[id][],son[id][]);
}
}
inline void Splay(int x)
{
int top=;st[++top]=x;
for(int i=x;!isroot(i);i=f[i])st[++top]=f[i];
for(int i=top;i;i--)pushdown(st[i]);
while(!isroot(x))
{
int y=f[x],z=f[y];
if(!isroot(y))
{
if(son[y][]==x^son[z][]==y)rotate(x);
else rotate(y);
}
rotate(x);
}
update(x);
}
inline void access(int x)
{
int t=;
while(x)
{
Splay(x);
son[x][]=t;
t=x;
x=f[x];
update(x);
}
}
inline void makert(int x){access(x);Splay(x);rev[x]^=;}
inline void link(int x,int y){makert(x);f[x]=y;}
inline void cut(int x,int y){makert(x);access(y);Splay(y);son[y][]=f[x]=;update(y);}
inline int findf(int x)
{
access(x);Splay(x);
int y=x;
while(son[y][])y=son[y][];
return y;
}
}Tree;
char opt[];
int main()
{
scanf("%d%d",&n,&m);
int k,x,y,z;
for(int i=;i<=n;i++)Tree.t[i]=;
while(m--)
{
scanf("%s",opt);
if(opt[]=='f')
{
scanf("%d%d%d",&z,&x,&y);z++,x++,y++;
z+=n;es[z][]=x,es[z][]=y;
scanf("%d%d",&Tree.t[z],&Tree.len[z]);
if(Tree.findf(x)!=Tree.findf(y)){Tree.link(z,x),Tree.link(z,y);}
else
{
Tree.makert(x);Tree.access(y);Tree.Splay(y);
if(Tree.t[Tree.mn[y]]<Tree.t[z])
{
k=Tree.mn[y];Tree.cut(k,es[k][]);Tree.cut(k,es[k][]);
Tree.link(z,x),Tree.link(z,y);
}
}
}
else if(opt[]=='m')
{
scanf("%d%d",&x,&y);x++,y++;
if(Tree.findf(x)!=Tree.findf(y))puts("-1");
else
{
Tree.makert(x);Tree.access(y);Tree.Splay(y);
printf("%d\n",Tree.sum[y]);
}
}
else
{
scanf("%d%d",&x,&y);x++;
x+=n;Tree.Splay(x);Tree.len[x]=y;Tree.update(x);
}
}
}

最新文章

  1. C# XMLDocument
  2. 按要求编写一个Java应用程序: (1)编写一个矩形类Rect,包含: 两个属性:矩形的宽width;矩形的高height。 两个构造方法: 1.一个带有两个参数的构造方法,用于将width和height属性初化; 2.一个不带参数的构造方法,将矩形初始化为宽和高都为10。 两个方法: 求矩形面积的方法area() 求矩形周长的方法perimeter() (2)通过继承Rect类编写一个具有确定位
  3. Node.js 快速了解
  4. Entity Framework Fluent API
  5. UVA 10325 The Lottery( 容斥原理)
  6. ARM安装ROS- indigo
  7. python 单元测试-unittest
  8. %s 与 %0s在 verilog中的区别
  9. OC基础11:基本的C语言特性2
  10. CDN库地址搜集2
  11. Cookie概念
  12. bzoj千题计划276:bzoj4515: [Sdoi2016]游戏
  13. JS-预解析(提升)与代码执行过程
  14. strcmp()字符串比较函数用法
  15. MySql concat与字符转义
  16. Flutter 学习资料
  17. codeblocks “can&#39;t find compiler executable in yourconfigured search ……”
  18. HDU1208:Pascal&#39;s Travels(DP)
  19. 作为从业人员,如果一定要学一门新的编程语言,那么它一定是c++
  20. android 实践项目四

热门文章

  1. Android SQLite基本用法
  2. 去除app中的标题栏
  3. Spring 定时作业
  4. SDOI2012 Round1 day2 集合(set)解题报告
  5. [T-SQL] 获取拼音
  6. 牛人blog汇总
  7. Lock和Condition
  8. jQuery-Ajax-Timeout属性不生效的问题
  9. lua的弱弱引用表
  10. 变量动态选取资源ID