你需要写一个在线的并查集
M 个操作,操作分为两类:
• 0 u v,连接 u 号军工厂及 v 号军工厂的铁路,注意铁路都是双向的;
• 1 u v, Reddington 需要知道 u 号军工厂及 v 号军工厂最早在加入第
几条条铁路后会联通,假如到这次操作都没有联通,则输出 0;
普通按秩合并无脑乱搞:
#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
typedef long long ll;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
#define MAXN (500000+10)
#define MAXM (500000+10)
class bingchaji
{
public:
int father[MAXN],tcn[MAXN],ra[MAXN],vis[MAXN],n;
void mem(int _n)
{
n=_n;
For(i,n) father[i]=i,tcn[i]=ra[i]=0,vis[i]=-1;
}
int getfather(int x)
{
if (father[x]==x) return x;
return getfather(father[x]);
}
bool unite(int x,int y,int t)
{
x=getfather(x),y=getfather(y);
if (x==y) return 0;
if (ra[x]>ra[y]) {
father[y]=x;
tcn[y]=t;
} else {
father[x]=y;
tcn[x]=t;
if (ra[x]==ra[y]) ++ra[y];
}
return 1;
}
int Query(int x,int y) {
if ( getfather(x)^getfather(y) ) return 0;
int p=x,cost=0;
while (1) {
vis[p]=cost;
if (p==father[p]) break;
cost=max(cost,tcn[p]);
p=father[p];
}
p=y; cost=0;
int res=0;
while (1) {
if (vis[p]>=0) {
res=max(cost,vis[p]);
break;
}
if (p==father[p]) break;
cost=max(cost,tcn[p]);
p=father[p];
}
p=x;
while (1) {
vis[p]=-1;
if (p==father[p]) break;
p=father[p];
}
return res;
}
}S;
int main()
{
// freopen("bzoj4668.in","r",stdin);
// freopen(".out","w",stdout);
int n=read();
S.mem(n);
int m=read();
int ans=n,p1=0;
int t=0;
For(i,m) {
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
b^=p1,c^=p1;
if (a==0) {
++t;
if (S.getfather(b)==S.getfather(c)) continue;
S.unite(b,c,t);
} else {
printf("%d\n", p1 = S.Query(b,c) );
}
}
return 0;
}