建无向图。如果两个点之间的距离小于R,并且两点之间无第三点可以用向量判断时间复杂度O(n^3).然后求生成树的个数。这里直接用Martix Tree定理。对于无向图G,它的kirchhoff矩阵C定义为它的度数矩阵D减去它的邻接矩阵A.然后用martix_Tree定理:对于一个无向图G,它的生成树个数等于kirchhoff矩阵任何一个n-1阶主子式的行列式的绝对值。关于无向图的生成树计数矩阵算法的相关的论文可以查看2007年周东《生成树的计数及应用》
/*
author : csuchenan
prog : hdu 4305
algorithm : martix tree
*/
#include <cstdio>
#include <cstring>
const int maxn = 305;
const int mod = 10007;
int degree[maxn][maxn];
int map[maxn][maxn];
int c[maxn][maxn];
bool vis[maxn];
struct Point{
int x, y;
}point[maxn];
int N, R;
int dist(int i, int j){
int x = point[i].x - point[j].x;
int y = point[i].y - point[j].y;
return x*x + y*y;
}
int min(int x, int y){
if(x > y){
return y;
}
return x;
}
int max(int x, int y){
if(x > y)
return y;
return x;
}
//检查三点共线的情况
bool check(int i, int j, int k){
// 首先确定是不是在两点之间
bool temp = (point[j].x - point[i].x)*(point[k].y - point[i].y) == (point[k].x - point[i].x)*(point[j].y - point[i].y);
bool flag = (point[k].x - point[i].x)*(point[j].x - point[k].x) >=0 &&
(point[k].y - point[i].y)*(point[j].y - point[k].y) >=0 ;
return flag && temp;
}
void dfs(int v){
vis[v] = true;
for(int i = 0; i < N; i ++){
if(!vis[i] && map[v][i]==1){
dfs(i);
}
}
}
void swap(int &x, int &y){
int tmp = x ;
x = y;
y = tmp;
}
int exgcd(int a, int b, int &x, int&y){
if(b==0){
x = 1;
y = 0;
return a;
}
int d = exgcd(b, a%b, x, y);
int t = x;
x = y;
y = t - a/b*y;
return d;
}
int det(int n){
int ans = 1;
int flag = 1;
int i, j, k;
for(i = 0 ; i < n; i++){
if(c[i][i]==0){
for(j = i + 1; j < n; j ++){
if(c[j][i] != 0)
break;
}
if(j == n)
return 0;
flag = !flag;
for(int k = i; k < n; k ++){
swap(c[i][k], c[j][k]);
}
}
ans = ans * c[i][i] %mod;
int x,y;
int tp = exgcd(c[i][i], mod, x, y);
for(k = i + 1; k < n; k++)
c[i][k] = c[i][k]*x%mod;
for(j = i + 1; j < n; j ++){
for(k = i + 1; k < n; k ++){
c[j][k] = (c[j][k] - c[j][i] * c[i][k])%mod;
if(j==k)
c[j][k] = (c[j][k] + mod)%mod;
}
}
}
ans = (ans%mod + mod)%mod;
if(flag)
return ans;
return mod - ans;
}
void build(){
//init
memset(degree, 0, sizeof(degree));
memset(map, 0, sizeof(map));
memset(c, 0, sizeof(c));
//build map and degree martix
for(int i = 0; i < N; i ++){
for(int j = i + 1 ; j < N; j ++){
if(dist(i, j) <= R*R){
bool flag = true;
for(int k = 0; k < N; k ++){
if(k != i && k != j){
if(check(i, j, k)){
flag = false;
break;
}
}
}
if(flag){
map[i][j] = 1;
map[j][i] = 1;
degree[i][i] ++;
degree[j][j] ++;
}
}
}
}
//build c
for(int i = 0; i < N; i ++){
for(int j = 0; j < N; j ++){
c[i][j] = degree[i][j] - map[i][j];
// printf("%d ", map[i][j]);
}
// printf("\n");
}
}
int main(){
int T;
// freopen("test.in", "r", stdin);
scanf("%d", &T);
while(T--){
scanf("%d%d", &N, &R);
for(int i = 0 ; i < N; i ++){
scanf("%d%d", &point[i].x, &point[i].y);
}
//build martix c ,
build();
memset(vis, false, sizeof(vis));
dfs(0);
bool flag= false;
for(int i = 0; i < N; i ++){
if(!vis[i]){
flag = true;
break;
}
}
if(flag){
printf("-1\n");
continue;
}
printf("%d\n" , det(N-1));
}
return 0;
}