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; }
相关文章推荐
- poj2236(并查集)
- poj2236 并查集
- POJ2236 Wireless Network 并查集
- Wireless Network--poj2236(并查集)
- POJ2236 Wireless Network 并查集
- POJ2236(并查集)
- 并查集入门 hud1213 poj2236
- poj2236 并查集
- POJ2236 Wireless Network(并查集)
- poj2236(并查集)
- poj2236 Wireless Network(并查集)
- poj2236(并查集)
- poj2236 wireless network(并查集)
- poj2236 并查集第一题 思路分析
- poj2236 基础并查集
- [数据结构]并查集水体POJ2236(并查集)
- 并查集-poj2236
- POJ2236-Wireless Network-并查集
- poj2236 Wireless Network(并查集)
- poj2236 并查集