Lightning

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1457    Accepted Submission(s): 469

Problem Description
There are N robots standing on the ground (Don't know why. Don't know how). 

Suddenly the sky turns into gray, and lightning storm comes! Unfortunately, one of the robots is stuck by the lightning!

So it becomes overladen. Once a robot becomes overladen, it will spread lightning to the near one.

The spreading happens when: 
  Robot A is overladen but robot B not.
  The Distance between robot A and robot B is no longer than R.
  No other robots stand in a line between them.
In this condition, robot B becomes overladen.

We assume that no two spreading happens at a same time and no two robots stand at a same position.


The problem is: How many kind of lightning shape if all robots is overladen? The answer can be very large so we output the answer modulo 10007. If some of the robots cannot be overladen, just output -1. 

 
Input
There are several cases.
The first line is an integer T (T < = 20), indicate the test cases.
For each case, the first line contains integer N ( 1 < = N < = 300 ) and R ( 0 < = R < = 20000 ), indicate there stand N robots; following N lines, each contains two integers ( x, y ) ( -10000 < = x, y < = 10000 ), indicate the position of the robot. 
 
Output
One line for each case contains the answer.
 
Sample Input
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
 
Sample Output
3
1
-1
 
Author
BUPT
 
Source
 

Matrix-Tree定理(Kirchhoff矩阵-树定理)。Matrix-Tree定理是解决生成树计数问题最有力的武器之一。它首先于1847年被Kirchhoff证明。在介绍定理之前,我们首先明确几个概念:

1、G的度数矩阵D[G]是一个n*n的矩阵,并且满足:当i≠j时,dij=0;当i=j时,dij等于vi的度数。

2、G的邻接矩阵A[G]也是一个n*n的矩阵, 并且满足:如果vi、vj之间有边直接相连,则aij=1,否则为0。

我们定义G的Kirchhoff矩阵(也称为拉普拉斯算子)C[G]为C[G]=D[G]-A[G],则Matrix-Tree定理可以描述为:G的所有不同的生成树的个数等于其Kirchhoff矩阵C[G]任何一个n-1阶主子式的行列式的绝对值。所谓n-1阶主子式,就是对于r(1≤r≤n),将C[G]的第r行、第r列同时去掉后得到的新矩阵,用Cr[G]表示。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std; typedef _int64 LL;
const int maxn=;
const int MOD=;
int n,R;
int g[maxn][maxn],d[maxn][maxn],mat[maxn][maxn];//邻接矩阵,度数矩阵,D-G矩阵 struct Point
{
int x,y;
Point(int x=,int y=):x(x),y(y){}
}p[maxn];
typedef Point Vector;
Vector operator -(Vector A,Vector B){return Vector(A.x-B.x,A.y-B.y);}
int Dot(Vector A,Vector B){ return A.x*B.x+A.y*B.y;}//点积
int Cross(Vector A,Vector B){ return A.x*B.y-A.y*B.x;}//叉积
int Length2(Vector A){return Dot(A,A);}//向量模的平方
bool OnSegment(Point p,Point a1,Point a2)//点是否在直线上(不包括端点)
{
return Cross(a1-p,a2-p)== && Dot(a1-p,a2-p)<;
} LL Extended_Euclid(LL a,LL b,LL &x,LL &y)
{
LL d,t;
if(b==){x=;y=;return a;}
d=Extended_Euclid(b,a%b,x,y);
t=x;x=y;y=t-a/b*y;
return d;
}
LL inv(LL a,LL n)//计算模n下a的逆,若不存在逆返回-1
{
LL d,x,y;
d=Extended_Euclid(a,n,x,y);
return d==?(x+n)%n:-;
}
int Det(int n)//求行列式的值模上MOD,需要使用逆元
{
int i,j,k,ret=;
for(i = ;i < n;i++)
for(j = ;j < n;j++)
mat[i][j] = (mat[i][j]%MOD+MOD)%MOD;
for(i = ;i < n;i++)
{
for(j = i;j < n;j++)
if(mat[j][i]!=)
{
for(k = i;k < n;k++) swap(mat[i][k],mat[j][k]);
if(i != j) ret = (-ret+MOD)%MOD;
break;
}
if(mat[i][i]==)
{
ret=;break;
}
for(j=i+;j<n;j++)
{
int mut=(mat[j][i]*inv(mat[i][i],MOD))%MOD;
for(k=i;k<n;k++)
mat[j][k]=(mat[j][k]-(mat[i][k]*mut)%MOD+MOD)%MOD;
}
ret=(ret*mat[i][i])%MOD;
}
return ret;
} void build_graph()
{
memset(g,,sizeof(g));
memset(d,,sizeof(d));
int i,j,k;
for(i=;i<n;i++)
{
for(j=i+;j<n;j++)
{
if(Length2(p[j]-p[i])>R*R) continue;
bool flag=;
for(k=;k<n;k++)
if(OnSegment(p[k],p[i],p[j])){flag=;break;}
if(flag){ g[i][j]=g[j][i]=;d[i][i]++;d[j][j]++;}
}
}
} void get_DGMatrix()
{
for(int i=;i<n;i++)
for(int j=;j<n;j++)
mat[i][j]=d[i][j]-g[i][j];
} int main()
{
int t,i;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&R);
for(i=;i<n;i++) scanf("%d%d",&p[i].x,&p[i].y);
build_graph();
get_DGMatrix();
int ans=Det(n-);
printf("%d\n",ans?ans:-);
}
return ;
}

hdu 4305 生成树计数问题的更多相关文章

  1. HDU - 4305 - Lightning 生成树计数 + 叉积判断三点共线

    HDU - 4305 题意: 比较裸的一道生成树计数问题,构造Krichhoof矩阵,求解行列式即可.但是这道题还有一个限制,就是给定的坐标中,两点连线中不能有其他的点,否则这两点就不能连接.枚举点, ...

  2. HDU 4305 Lightning(计算几何,判断点在线段上,生成树计数)

    Lightning Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  3. HDU 4305 Lightning Matrix Tree定理

    题目链接:https://vjudge.net/problem/HDU-4305 解法:首先是根据两点的距离不大于R,而且中间没有点建立一个图.之后就是求生成树计数了. Matrix-Tree定理(K ...

  4. 【HDU 4305】Lightning(生成树计数)

    Problem Description There are N robots standing on the ground (Don't know why. Don't know how). Sudd ...

  5. HDU 4786 生成树 并查集+极大极小值 黑白边 确定选择白边的数量

    题意: 给定一个无向图 n 个点 m条无向边 u v val val == 1 表示边(u, v) 为白边 问能否找到n个点的生成树, 使得白边数为斐波那契数 思路: 并查集求图是否连通( 是否存在生 ...

  6. HDU 4832(DP+计数问题)

    HDU 4832 Chess 思路:把行列的情况分别dp求出来,然后枚举行用几行,竖用几行.然后相乘累加起来就是答案 代码: #include <stdio.h> #include < ...

  7. 使用Matrix-tree与它的行列式来解决生成树计数问题

    我又把Matrix写错啦 这东西讲课的时候竟然一笔带过了,淦 好吧这东西我不会证 那我们来愉快的看结论吧 啦啦啦 预备工作 你有一个 $ n $ 个点的图 比如说 5 /|\ / | \ 2--1-- ...

  8. HDU 1010生成树

    求起点到终点的最短权值和

  9. hdu 4305 概率dp

    /* 题目大意:有n个房间由n-1个隧道连接起来,从1号房间开始, 每个节点i都有三种可能: 1.被杀死,回到节点1,概率为ki; 2.找到出口,离开迷宫,概率ei; 3.与它相连的有m个房间,到任意 ...

随机推荐

  1. Unity调用Windows窗口句柄,选择文件和目录

    T:2019-6-25 10:06:59 C:Scatt Kang using System; using System.Collections; using System.Collections.G ...

  2. iOS--UIScrollView基本用法和代理方法

    主要是为了记录下UIScrollView的代理方法吧 在帮信息学院的学长做东西的时候需要大量用到分块浏览,所以就涉及到很多的关于scrollview,所以也就有了这篇文章   - (void)view ...

  3. 【线段树分治 线性基】luoguP3733 [HAOI2017]八纵八横

    不知道为什么bzoj没有HAOI2017 题目描述 Anihc国有n个城市,这n个城市从1~n编号,1号城市为首都.城市间初始时有m条高速公路,每条高速公路都有一个非负整数的经济影响因子,每条高速公路 ...

  4. Robot Framework user guide

    http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html

  5. thinkcmf5更新模板代码分析,解决模板配置json出错导致数据库保存的配置项内容丢失问题

    private function updateThemeFiles($theme, $suffix = 'html') { $dir = 'themes/' . $theme; $themeDir = ...

  6. Python基础-os模块 sys模块

    sys模块 与操作系统交互的一个接口 文件夹相关 os.makedirs('dirname1/dirname2')    可生成多层递归目录 os.removedirs('dirname1')    ...

  7. urllib、requests库整理

  8. redis集群监控之Redis-monitor部

    为了对以后有可能面临的redis集群监控做准备,这两天在准备这方面的事情,现在将其中的过程记录一下. 首先是“Ronney-Hua”的这篇文章对三中开源监控软件做了对比 文章地址:https://bl ...

  9. pandas-Notes1

    #coding = utf-8 import pandas as pd import numpy as np import matplotlib as plt # series, like vecto ...

  10. LeetCode(172)Factorial Trailing Zeroes

    题目 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in ...