bzoj 4668 冷战——并查集结构
2024-09-08 09:31:27
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4668
不路径压缩,维护并查集的树的结构,查询链上最大值。按秩合并就可以暴爬。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=5e5+;
int n,m,fa[N],w[N],siz[N],tot,ans;
int rdn()
{
int ret=;bool fx=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')fx=;ch=getchar();}
while(ch>=''&&ch<='') ret=(ret<<)+(ret<<)+ch-'',ch=getchar();
return fx?ret:-ret;
}
int find(int a)
{
if(fa[a]==a) return a;
int f=find(fa[a]);
return f;
}
void merge(int u,int v)
{
++tot;//写在return前!
if(u==v) return;//
if(siz[u]<siz[v]) swap(u,v);
fa[v]=u; siz[u]+=siz[v];
w[v]=tot;
}
void query(int u,int v)
{
int cr=u,d1=,d2=,f1,f2;
while(fa[cr]!=cr)d1++,cr=fa[cr]; f1=cr;
cr=v; while(fa[cr]!=cr)d2++,cr=fa[cr]; f2=cr;
if(f1!=f2){puts("");ans=;return;}
if(d1>d2) swap(u,v),swap(d1,d2);
ans=;
while(d2>d1)
ans=max(ans,w[v]),v=fa[v],d2--;
while(u!=v)
ans=max(ans,max(w[u],w[v])),
u=fa[u],v=fa[v];
printf("%d\n",ans);
}
int main()
{
n=rdn(); m=rdn();
for(int i=;i<=n;i++) fa[i]=i,siz[i]=;
for(int i=,op,u,v;i<=m;i++)
{
op=rdn(); u=rdn()^ans; v=rdn()^ans;
if(!op) merge(find(u),find(v));
else query(u,v);
}
return ;
}
最新文章
- ObjC宏定义小细节
- Intel CPU MMX SSE SSE2/3/4指令集手册下载URL
- solr与.net系列课程(四)solr查询参数的讲解与.net如何获取solr数据
- net use
- Python字节流打包拆包
- 使用Memcache在PHP中调试方法的介绍及应用
- JIRA项目跟踪管理工具简介与安装
- 有关怎样入门ACM
- 【HTML+CSS】在书写代码时的便捷应用
- Express学习 ------模版引擎(handlebars)
- 【转】Qt之JSON保存与读取
- bzoj 4084 双旋转字符串
- day 26 元类
- 概率论与数理统计 Q&;A:
- MFC中不同对话框间使用SendMessage发送自定义消息的具体实现
- Service Fabric &mdash;&mdash; Stateful Service 概念
- macOS -- Mac系统如何编辑hosts文件
- python下载网页上公开数据集
- sql随机插入数据--记录
- oracle 百万行数据优化查询
热门文章
- C#使用CurrentUICulture切换语言
- HDU 5289 Assignment(单调队列)
- xgboost的SparkWithDataFrame版本实现
- MySQL提示Access denied for user &;#39;&;#39;@&;#39;localhost&;#39;”的解决
- idea自动注入和自动编译
- 使用Python处理CSV文件的一些代码示例
- Attempting to write a row[5] in the range [0,394] that is already written to disk.
- mysql启动warning: World-writable config file
- windows常用命令(转载)
- iOS Dev (26) 初步了解下UIColor的最常用知识