题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2594

时间倒序一下,就是 魔法森林 那道题;

有个不解的地方,是 access 里面关于 pushup 的地方,两种写法都可以,但不明白没有注释掉的写法为什么也可以。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int const maxn=,maxm=1e6+;
int n,m,Q,val[maxn],mx[maxn],fa[maxn],pre[maxn],c[maxn][],sta[maxn],rev[maxn];
struct N{int hd,to,w,id; bool b;}edge[maxm];
struct T{int u,v,ans,f,id;}q[];
bool cmp(N x,N y){return x.w<y.w;}
bool cmp2(N x,N y){return x.hd<y.hd || (x.hd==y.hd && x.to<y.to);}
bool cmpp(N x,N y){return x.id<y.id;}
int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
int rd()
{
int ret=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-; ch=getchar();}
while(ch>=''&&ch<='')ret=ret*+ch-'',ch=getchar();
return ret*f;
}
int get(int x,int y)
{
int l=,r=m;
while(l<=r)
{
int mid=((l+r)>>);
if(edge[mid].hd==x && edge[mid].to==y)return mid;
if(edge[mid].hd<x || (edge[mid].hd==x && edge[mid].to<y))l=mid+;
else r=mid-;
}
}
bool isroot(int x)
{
return c[pre[x]][]!=x && c[pre[x]][]!=x;
}
void pushup(int x)
{
int ls=c[x][],rs=c[x][];
mx[x]=x;
if(val[mx[ls]]>val[mx[x]])mx[x]=mx[ls];
if(val[mx[rs]]>val[mx[x]])mx[x]=mx[rs];
}
void reverse(int x)
{
int ls=c[x][],rs=c[x][];
if(rev[x])
{
rev[ls]^=; rev[rs]^=; rev[x]=;
swap(c[x][],c[x][]);
}
}
void rotate(int x)
{
int y=pre[x],z=pre[y],d=(c[y][]==x);
if(!isroot(y))c[z][c[z][]==y]=x;
pre[x]=z; pre[y]=x; pre[c[x][!d]]=y;
c[y][d]=c[x][!d]; c[x][!d]=y;
pushup(y); pushup(x);
}
void splay(int x)
{
int top=; sta[++top]=x;
for(int i=x;!isroot(i);i=pre[i])sta[++top]=pre[i];
for(int i=top;i;i--)reverse(sta[i]);
for(;!isroot(x);rotate(x))
{
int y=pre[x],z=pre[y];
if(isroot(y))continue;
((c[y][]==x)^(c[z][]==y))?rotate(x):rotate(y);
}
pushup(x);//和下面配套
}
void access(int x)
{
for(int t=;x;c[x][]=t,t=x,x=pre[x])splay(x);//这样为什么也可以?
}
//void access(int x)
//{
// int t=0;
// while(x)
// {
// splay(x);c[x][1]=t;pushup(x);t=x;x=pre[x];
// }
//}
void makeroot(int x)
{
access(x); splay(x); rev[x]^=;
}
void link(int x,int y)
{
makeroot(x); pre[x]=y;//
}
void cut(int x,int y)
{
makeroot(x); access(y); splay(y);
pre[x]=; c[y][]=;
}
int query(int x,int y)
{
makeroot(x); access(y); splay(y); return mx[y];
}
int main()
{
n=rd(); m=rd(); Q=rd();
for(int i=;i<=n;i++)fa[i]=i;
for(int i=;i<=m;i++)
{
edge[i].hd=rd(); edge[i].to=rd(); edge[i].w=rd();
if(edge[i].hd>edge[i].to)swap(edge[i].hd,edge[i].to);
}
sort(edge+,edge+m+,cmp);//w
for(int i=;i<=m;i++)
{
edge[i].id=i;
val[i+n]=edge[i].w; mx[i+n]=i+n;//
}
sort(edge+,edge+m+,cmp2);//u,v
for(int i=;i<=Q;i++)
{
q[i].f=rd(); q[i].u=rd(); q[i].v=rd();
if(q[i].f==)
{
if(q[i].u>q[i].v)swap(q[i].u,q[i].v);
int t=get(q[i].u,q[i].v);
// edge[t].b=1; q[i].id=edge[i].id;
edge[t].b=; q[i].id=edge[t].id;
}
}
sort(edge+,edge+m+,cmpp);//w
for(int i=,tot=;i<=m;i++)
{
if(edge[i].b)continue;//kruskal 连不删的边
int u=edge[i].hd,v=edge[i].to;
if(find(u)!=find(v))
{
fa[find(u)]=find(v);
link(u,i+n); link(v,i+n);
tot++;
if(tot==n-)break;
}
}
for(int i=Q;i;i--)
{
int u=q[i].u,v=q[i].v;
if(q[i].f==)q[i].ans=val[query(u,v)];
else
{
int k=q[i].id,t=query(u,v);
if(edge[k].w<val[t])
{
cut(t,edge[t-n].hd); cut(t,edge[t-n].to);
link(u,k+n); link(v,k+n);
}
}
}
for(int i=;i<=Q;i++)
if(q[i].f==)printf("%d\n",q[i].ans);
return ;
}

最新文章

  1. Windows Live Writer测试
  2. HTTP与AJAX深入揭秘,不使用AJAX实现页面无刷新
  3. Oracle 11g 下载|Oracle 11g 官网下载|Oracle 11g 官网下载 带登录用户和密码
  4. MATLAB学习笔记(一)&mdash;&mdash;入门与操作
  5. 第一篇:数据库需求与ER建模
  6. 深入理解7816(1)---- 关于F/D和etu
  7. 设计模式之(二)Adapter模式
  8. Direct3D 光照和材质
  9. 实现TCP断点上传,后台C#服务实现接收
  10. POJ 1724 最短路费用限制
  11. 多个版本的Python如何设置不冲突
  12. 纯CSS3模拟星体旋转效果
  13. 过滤器Filter与监听器Listener
  14. Vue的router使用
  15. Swift 同构与异构
  16. 日常英语---十三、MapleStory/Monsters/Level 11-20(邪恶之眼)
  17. servlet第三篇
  18. SpringMVC+MyBatis+MySQL 8小时链接断开
  19. 适配器在JavaScript中的体现
  20. go语言基础之结构体成员的使用普通变量

热门文章

  1. linux命令 dig-域名查询工具
  2. MySQL-----改
  3. MapReduce架构与执行流程
  4. Android二级缓存之物理存储介质上的缓存DiskLruCache
  5. [luoguP2626] 斐波那契数列(升级版)(模拟)
  6. bzoj 1049 [HAOI2006]数字序列
  7. Linux下汇编语言学习笔记51 ---
  8. 确定位置的经纬度LocationUtil
  9. [运维]ELK实现日志监控告警
  10. Linux下tmp文件夹的文件自动删除的问题(转)