# BZOJ 4668: 冷战

## Code

/**************************************************************
Problem: 4668
User: BeiYu
Language: C++
Result: Accepted
Time:1256 ms
Memory:9104 kb
****************************************************************/

#include <bits/stdc++.h>
using namespace std;

const int N = 5e5+50;

int n,lst;
struct UnionTable {
int f[N],s[N],v[N],d[N];
void init(int n) {
for(int i=1;i<=n;i++) f[i]=i,v[i]=0,s[i]=1;
}
int find(int x) {
if(f[x]^x) {
int fa=find(f[x]);
d[x]=d[f[x]]+1;
return fa;
}else return x;
}
void Union(int x,int y,int c) {
int f1=find(x),f2=find(y);
if(f1==f2) return;
if(s[f1]>s[f2]) f[f2]=f1,v[f2]=c,s[f1]+=s[f2];
else f[f1]=f2,v[f1]=c,s[f2]+=s[f1];
}
int Query(int x,int y) {
int f1=find(x),f2=find(y),r=0;
if(f1!=f2) return 0;
for(;x^y;) {
if(d[x]<d[y]) swap(x,y);
r=max(r,v[x]),x=f[x];
}return r;
}
}un;

inline int in(int x=0,char ch=getchar()) { while(ch>'9' || ch<'0') ch=getchar();
while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();return x; }
int main() {
n=in();
un.init(n);
for(int m=in(),c=0;m--;) {
int opt=in(),u=in()^lst,v=in()^lst;
if(!opt) un.Union(u,v,++c);
else printf("%d\n",lst=un.Query(u,v));
}return 0;
}


posted @ 2016-12-23 21:22  北北北北屿  阅读(180)  评论(0编辑  收藏  举报