Well , this question is quite simple actually. Just use r1+r2 <d and fabs(r1-r2) < d to judge whether the two circle intersect with each other. Combine geometry and disjoint set together to get the solution. DON'T FORGET THE SITUATION THAT n == 0 !!!! :(
code:
#include<iostream>
#include<stdlib.h>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<algorithm>
#include <cstdio>
#define MAX(a,b) ((a)>(b)?(a):(b))
//#define maxfacprime 65536 // int^1/2
using namespace std;
struct circle{
double x, y, r;/* , ancestor;
circle(){
ancestor = -1;
}*/
};
circle C[105];
int p[105];
bool intersect(circle a, circle b)
{
double dis = (a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y);
dis = sqrt(dis);
if ((dis < a.r + b.r) && (dis >fabs(a.r-b.r)))
return true;
else return false;
}
int find(int element)
{
if (p[element] < 0)
return element;
else return p[element] = find(p[element]);
}
void union_set(int set1, int set2)
{
p[set1] += p[set2];
p[set2] = set1;
}
void Union(int element1, int element2)
{
int root1 = find(element1);
int root2 = find(element2);
if (p[root1] < p[root2])
union_set(root1, root2);
else union_set(root2, root1);
}
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int n;
while (cin >> n)
{
if (n == -1)
break;
memset(p, -1, sizeof(p));
for (int i = 1; i <= n; i++)
{
scanf("%lf%lf%lf", &C[i].x, &C[i].y, &C[i].r);
}
for (int i = 1; i <= n; i++)
for (int j = i + 1; j <= n; j++)
{
if (find(i)!=find(j))
if (intersect(C[i], C[j]))
Union(i, j);
}
int max = 0;
for (int i = 1; i <= n; i++)
if (p[i] < 0)
if ((-p[i])>max)
max = -p[i];
if (max >1||!max)
printf("The largest component contains %d rings.\n", max);
else printf("The largest component contains 1 ring.\n");
}
return 0;
}
//remember to use %lf to print double
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);