UVA 10301

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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值