题目: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 ;
}

最新文章

  1. ObjC宏定义小细节
  2. Intel CPU MMX SSE SSE2/3/4指令集手册下载URL
  3. solr与.net系列课程(四)solr查询参数的讲解与.net如何获取solr数据
  4. net use
  5. Python字节流打包拆包
  6. 使用Memcache在PHP中调试方法的介绍及应用
  7. JIRA项目跟踪管理工具简介与安装
  8. 有关怎样入门ACM
  9. 【HTML+CSS】在书写代码时的便捷应用
  10. Express学习 ------模版引擎(handlebars)
  11. 【转】Qt之JSON保存与读取
  12. bzoj 4084 双旋转字符串
  13. day 26 元类
  14. 概率论与数理统计 Q&amp;A:
  15. MFC中不同对话框间使用SendMessage发送自定义消息的具体实现
  16. Service Fabric &mdash;&mdash; Stateful Service 概念
  17. macOS -- Mac系统如何编辑hosts文件
  18. python下载网页上公开数据集
  19. sql随机插入数据--记录
  20. oracle 百万行数据优化查询

热门文章

  1. C#使用CurrentUICulture切换语言
  2. HDU 5289 Assignment(单调队列)
  3. xgboost的SparkWithDataFrame版本实现
  4. MySQL提示Access denied for user &amp;#39;&amp;#39;@&amp;#39;localhost&amp;#39;”的解决
  5. idea自动注入和自动编译
  6. 使用Python处理CSV文件的一些代码示例
  7. Attempting to write a row[5] in the range [0,394] that is already written to disk.
  8. mysql启动warning: World-writable config file
  9. windows常用命令(转载)
  10. iOS Dev (26) 初步了解下UIColor的最常用知识