您的位置:首页 > 其它

poj2236 并查集

2014-03-18 00:29 302 查看
/**
* poj2236 并查集
*/
#include <cstdio>
#include <cstring>
const int MAX_NUM = 1002;

int n,d,d2;
struct node_t{
int father;
int x;
int y;//position
bool repaired;
} nod[MAX_NUM];

int getfather(int x){
if(nod[x].father == x){
return x;
}
else{
int oldfa = nod[x].father;
nod[x].father = getfather(nod[x].father);
return nod[x].father;
}
}

void union_set(int a,int b){//O(n)
int fa = getfather(a), fb = getfather(b);
if(fa == fb){
return;
}
else{//set fb's father to fa
nod[fb].father = fa;
}
}

bool test(int a,int b){
if(getfather(a) == getfather(b)){
return true;
}
else{
return false;
}
}

int main(){
char op[2];
scanf("%d%d",&n,&d);
d2 = d*d;
for(int i=1;i<=n;++i){//O(n^2) input
scanf("%d%d",&nod[i].x,&nod[i].y);
nod[i].father = i;
nod[i].repaired = false;
}

int repaired_node,testa,testb;
while(scanf("%s",op) != EOF){
switch(op[0]){
case 'O':
scanf("%d",&repaired_node);
if(!nod[repaired_node].repaired){
nod[repaired_node].repaired = true;
for(int i=1;i<=n;++i){//
if(nod[i].repaired){
if((nod[repaired_node].x-nod[i].x)*(nod[repaired_node].x-nod[i].x) + (nod[repaired_node].y-nod[i].y)*(nod[repaired_node].y-nod[i].y) <= d2){
union_set(i,repaired_node);
}
}
}
}
break;
case 'S':
scanf("%d%d",&testa,&testb);
if(test(testa,testb)){
printf("SUCCESS\n");
}
else{
printf("FAIL\n");
}
break;
default:
printf("error");
break;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: