Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 20663 | Accepted: 8689 |
Description
Input
Output
Sample Input
4 1 0 1 0 2 0 3 0 4 O 1 O 2 O 4 S 1 4 O 3 S 1 4
Sample Output
FAIL SUCCESS
竟然一次就过了,好激动啊!!!
题目大意:首先输入四个坐标(刚开始......一直不明白这两个数字是什么,想了一下才知道这个是坐标)。其他的应该都还好。
遇到的问题和思路:
这道题的难点就是如何创建树根,什么时候放到一棵树上去,什么时候该判断。
首先,把所有树根初始化,然后每当出现一个O,就说明已经修好了,那就先定义一个数组bool,刚开始为false,后来变成了true。用两点间距离公式,判断能否两个点变成一棵树。具体的看代码吧。
给出代码:
#include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #include<iostream> using namespace std; const int MAX = 1000 + 5; int n, d, xiu, lian1, lian2; struct point{ int x, y; }dian[MAX]; int par[MAX],rank[MAX]; char p; bool us[MAX];//用来判断是否维修过 void inti(){//初始化 for(int i = 1; i <= n; i++){ par[i] = i; rank[i] = 0; } } int find(int i){//寻找树根 if(par[i] == i)return i; else{ return par[i] = find(par[i]); } } int same(int x, int y){//是同一个根,才能相互走 return find(x) == find(y); } void conbi(int x, int y){//连接 x = find(x); y = find(y); if(x == y)return; if(rank[y] > rank[x]){ par[x] = y; } else { par[y] = x; if(rank[x] == rank[y]) rank[x]++; } } void judge1(){//用来连接树根的 for(int i = 1; i <= n; i++){ if(xiu == i)continue; if(us[i] == true){ if(same(i, xiu))continue; else{ int g = (dian[i].x - dian[xiu].x)*(dian[i].x - dian[xiu].x) + (dian[i].y - dian[xiu].y)*(dian[i].y - dian[xiu].y); if(g <= d*d)conbi(i, xiu); else continue; } } } } void judge(){//用来判断是否不是树根的情况下还能够链接 int g = (dian[lian1].x - dian[lian2].x)*(dian[lian1].x - dian[lian2].x) + (dian[lian1].y - dian[lian2].y)*(dian[lian1].y - dian[lian2].y); if(g > d * d)printf("FAIL\n"); else printf("SUCCESS\n"); } int main(){ while(scanf("%d%d", &n, &d)!=EOF){ getchar(); memset(dian, 0, sizeof(dian)); memset(par, 0, sizeof(par)); memset(rank, 0, sizeof(rank)); memset(us, 0, sizeof(us)); inti(); for(int i = 1; i <= n; i++){ scanf("%d%d", &dian[i].x, &dian[i].y); getchar(); } while(scanf("%c",&p)!=EOF){ getchar(); if(p == 'O'){ scanf("%d",&xiu); getchar(); us[xiu] = true; judge1(); } else if(p == 'S'){ scanf("%d%d", &lian1, &lian2); getchar(); if(same(lian1, lian2)){ printf("SUCCESS\n"); } else { judge(); } } } } return 0; }