带权并查集,似乎必须要路径压缩才能过,带了又不会写了
1≤n≤40,000
1≤m≤100,000
1≤a,b≤n 1≤e≤10 ^ 9
D∈ ′ N ′ , ′S ′ , ′ W ′ , ′ E ′
写了一半的代码(query和pii是历史遗留产物
#include<bits/stdc++.h>
using namespace std;
const int N=4e5+10;
int fa[N],n,m,xl[N],yl[N];
struct Pii{
int fir,sec;
};
char ch;
int find(int x){
if(fa[x]==x) return x;
else {
xl[x]+=xl[fa[x]];
yl[x]+=yl[fa[x]];
fa[x]=find(fa[x]);
return fa[x];
}
}
Pii query(int now){
return {xl[now],yl[now]};
}
void uni(int x,int y,int len,int t){
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
fa[i]=i;
}
int x,y,len,t;
while(m--){
scanf("%c",&ch);
if(ch=='?'){
scanf("%d%d",&x,&y);
if(find(x)!=find(y)) printf("?\n");
else {
Pii xx,yy;
xx=query(x);
yy=query(y);
printf("%d",abs(xx.fir+yy.fir)+abs(xx.sec+yy.sec));
}
}
else{
scanf("%d%d%d",&x,&y,&len);
if(ch=='E') t=0;
else if(ch=='S') t=1;
else if(ch=='W') t=2;
else t=3;
uni(x,y,len,t);
}
}
return 0;
}
除了加边问题似乎不大