无向联通图生成树计数 -- Lightning HDU - 4305

Lightning HDU - 4305

题意:
有n个机器人在二维平面上, (x, y)表示机器人所处的位置(位置不重复),一道是闪电会打到一个机器人身上,闪电会传导到与该机器人距离不大于r的机器人上面(但闪电不会隔着一个或几个机器人传导),每个机器人传导同理,问可以有多少种闪电纹路,如果有机器人没有被覆盖则输出-1。

思路:
无向联通生成树计数题,两个机器人距离不超过r就联通(但不可以隔人传导),把无向图处理出来套Kirchhoff 矩阵法就可以了。判断所有机器人能否被覆盖用并查集判断。

code:


#include<iostream>
#include<cmath>
#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
const int maxn = 3e2 + 5;
const double ipe = 1e-8;
const double mod = 1e4 + 7;
double A[maxn][maxn];
bool visx[maxn][maxn], visy[maxn][maxn];
struct node{
	double x, y;
} point[maxn];

bool check(double res){
	return (res < ipe || res > -ipe);
}
double get_mod(double res){
	while(res >= mod){
		res -= mod;
	}
	while(res <= -mod){
		res += mod;
	}
	return res;
}
double get_res(struct node a, struct node b){
	double res = (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
	return sqrt(res);   
}

int det(int n, double a[][maxn]){
	double temp;
	for(int i = 1; i <= n; i++){
		if(check(a[i][i]))
		  for(int j = i + 1; j <= n; j++) {
			 if(check(a[j][i])) swap(a[i], a[j]);
			 break;
		  }
		
	    for(int j = i + 1; j <= n; j++){
	    	temp = - a[j][i] / a[i][i];
	    	for(int k = 1; k <= n; k++)
	    	   a[j][k] = get_mod(a[j][k] + get_mod(a[i][k] * temp));
		}
		
	}
	
	temp = 1;
	for(int i = 1; i <= n; i++) temp = get_mod(temp * a[i][i]);
	if(n == 0) temp = 0;
	
	return (int)abs(temp);	
	
} 

int fa[maxn];
int find(int x){
	return fa[x] == x ? x : fa[x] = find(fa[x]);
}
void unite(int x, int y){
	int fx = find(x);
	int fy = find(y);
	if(fx != fy) fa[fx] = fy;
}
int main(){
	int t, n;
	double r;
	scanf("%d", &t);
	while(t--){
		scanf("%d%lf", &n, &r);
		for(int i = 1; i <= n; i++) scanf("%lf%lf", &point[i].x, &point[i].y);
		memset(visx, 0, sizeof(visx));
		memset(visy, 0, sizeof(visy));
		memset(A, 0, sizeof(A));
		for(int i = 1; i <= n; i++) fa[i] = i;
		for(int i = 1; i <= n; i++)
		for(int j = i + 1; j <= n; j++)
		  if(get_res(point[i], point[j]) <= r) {
		  	
		  	  if(point[i].x == point[j].x ) {         //判断是否直接相邻 
		  		if(visx[i][(int)point[i].x]) continue;
		  		visx[i][(int)point[i].x] = 1;		  		
			  }
			  if(point[i].y == point[j].y) {
			 	if(visy[i][(int)point[i].y]) continue;
			 	visy[i][(int)point[i].y] = 1;
			  }
			  
		  	  A[i][i]++;  //构造Kirchhoff矩阵 
		  	  A[j][j]++;
		  	  A[i][j] = A[j][i] = -1;
		  	  unite(i, j);
		  }
		
		int flag = 0;
		for(int i = 2; i <= n; i++)
			if(find(1) != find(i)){
			   flag = 1;
			   break;	
			}
		if(flag) printf("%d\n", -1);
		else printf("%d\n", det(n - 1, A));
		
		
	}
}

/*
3
3 2
-1 0
0 1
1 0
3 2
-1 0
0 0
1 0
3 1
-1 0
0 1
1 0



3
1
-1
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值