POJ2236 并查集 比1611 复杂一些
2016-10-29 20:02
260 查看
这是自己打的 结果第一次就wa了 一直没有找到原因 后来才发现 原来是 数组开小了一个数 原来开成 了 1001 其实开成1002 就能过 #include<stdio.h> #include<string.h> int d; struct node { int father; int xi,yi; }p[1011]; int find(int x) { return x == p[x].father ? x : find(p[x].father); } void merge(const node p1,const node p2) { int x = find(p1.father); int y = find(p2.father); if(x != y) if((p1.xi - p2.xi)*(p1.xi - p2.xi) + (p1.yi - p2.yi)*(p1.yi - p2.yi) <= d*d) p[y].father = x; } int main() { int num,i,n,m; bool ok[1011]; char pp; memset(ok,0,sizeof(ok)); scanf("%d%d",&num,&d); for(i = 1;i <= num ;i++) p[i].father = i; for ( i = 1; i <= num ;i++) scanf("%d%d",&p[i].xi ,&p[i].yi); while(scanf("\n%c",&pp)!=EOF) { if(pp == 'O') { scanf("%d",&n); ok = 1; for ( i = 1; i <= num ;i++) if(ok[i]&& i != n) merge(p[i],p ); } else { scanf("%d%d",&m,&n); if(find(m) == find(n)) printf("SUCCESS\n"); else printf("FAIL\n"); } } return 0; } #include<string.h> #include<stdio.h> #define N 1110 int d; bool use ; struct node { int pre; int x, y; }p ; int find(int x) { return x == p[x].pre ? x : find(p[x].pre); } void join(const node p1, const node p2) { int root1, root2; root1 = find(p1.pre); root2 = find(p2.pre); if(root1 != root2) if((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y) <= d * d) p[root2].pre = root1; } int main() { //freopen("Input.txt", "r", stdin); int num; char ope; int ok; int from, to; scanf("%d%d", &num, &d); for(int i = 1; i <= num; ++i) p[i].pre = i; memset(use, false, sizeof(use)); for(int i = 1; i <= num; ++i) scanf("%d%d", &p[i].x, &p[i].y); while(scanf("\n%c", &ope) != EOF) { if(ope == 'O') { scanf("%d", &ok); use[ok] = true; for(int i = 1; i <= num; ++i) if(use[i] && i != ok) join(p[i], p[ok]); } else { scanf("%d%d", &from, &to); if(find(from) == find(to)) printf("SUCCESS\n"); else printf("FAIL\n"); } } return 0; }
相关文章推荐
- POJ1611 并查集 基础 比 2524 复杂一些
- poj-2236 Wireless Network &&poj-1611 The Suspects && poj-2524 Ubiquitous Religions (基础并查集)
- POJ 1611 The Suspects(并查集)
- POJ_1611(并查集应用)
- pku 2236 poj 并查集一般题
- poj 2236 Wireless Network (并查集)
- poj 1611 The Suspects (并查集)
- POJ_2236_并查集
- poj 1611 (并查集)
- PKU POJ 1611解题报告(并查集)
- POJ 1611 The Suspects (并查集)
- 【并查集】POJ-1611
- POJ2236 Wireless Network(并查集)
- poj 2236 Wireless Network (并查集)
- POJ 1988 并查集的应用 很好地体现了并查集的一些特点
- POJ 2236 WIRELESS NETWORK(并查集)
- POJ 2236 Wireless Network(并查集)
- 【原】 POJ 1611 The Suspects 并查集 解题报告
- POJ 2236 并查集
- POJ 1611 (并查集)