题意:在平面坐标系上有n个坏掉的电脑,电脑之前只有距离小于d才能通信(在两台电脑都修好的情况下),
有两个查询第一种O x即修理电脑x,第二种S x y即查询x电脑和y电脑之间是否可以通信(间接的也可以)。
题解:并查集裸题。
#include #include #include #include #include #include #include #include #include #include #include #include #include #define ll long long #define Max 100000 using namespace std; const int inf = 0x3f3f3f3f; int d; int use[1110]; struct node{ int x,y; int pre; int root; }a[1110]; int init(int n){ for(int i = 1 ; i <= n ; i++){ a[i].pre = i; a[i].root = 1; } } int find_pre(int x){ if(a[x].pre==x){ return x; } return a[x].pre = find_pre(a[x].pre); } bool is_s(int x,int y){ return find_pre(x)==find_pre(y); } void unite(node x,node y){ int rootx,rooty; rootx = find_pre(x.pre); rooty = find_pre(y.pre); if(rootx == rooty){ return; } if((x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y) <= d*d){ if(a[rootx].root > a[rooty].root){ a[rooty].pre = rootx; }else{ if(a[rootx].root == a[rooty].root){ a[rooty].root++; } a[rootx].pre = rooty; } } } int main(){ int n; cin>>n>>d; for(int i = 1 ; i <= n ; i++){ scanf("%d %d",&a[i].x,&a[i].y); } init(n); getchar(); char x; int k,j; while(true){ scanf("%c",&x); if(x=='O'){ scanf("%d",&k); use[k] = 1; for(int i = 1 ; i <= n ; i++){ if(use[i]&&i!=k){ unite(a[i],a[k]); } } }else if(x=='S'){ scanf("%d %d",&k,&j); if(is_s(k,j)){ cout<<"SUCCESS"<