hdu 4305 生成树计数问题
Lightning
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1457 Accepted Submission(s): 469

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.
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.
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 生成树计数问题的更多相关文章
- HDU - 4305 - Lightning 生成树计数 + 叉积判断三点共线
HDU - 4305 题意: 比较裸的一道生成树计数问题,构造Krichhoof矩阵,求解行列式即可.但是这道题还有一个限制,就是给定的坐标中,两点连线中不能有其他的点,否则这两点就不能连接.枚举点, ...
- HDU 4305 Lightning(计算几何,判断点在线段上,生成树计数)
Lightning Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- HDU 4305 Lightning Matrix Tree定理
题目链接:https://vjudge.net/problem/HDU-4305 解法:首先是根据两点的距离不大于R,而且中间没有点建立一个图.之后就是求生成树计数了. Matrix-Tree定理(K ...
- 【HDU 4305】Lightning(生成树计数)
Problem Description There are N robots standing on the ground (Don't know why. Don't know how). Sudd ...
- HDU 4786 生成树 并查集+极大极小值 黑白边 确定选择白边的数量
题意: 给定一个无向图 n 个点 m条无向边 u v val val == 1 表示边(u, v) 为白边 问能否找到n个点的生成树, 使得白边数为斐波那契数 思路: 并查集求图是否连通( 是否存在生 ...
- HDU 4832(DP+计数问题)
HDU 4832 Chess 思路:把行列的情况分别dp求出来,然后枚举行用几行,竖用几行.然后相乘累加起来就是答案 代码: #include <stdio.h> #include < ...
- 使用Matrix-tree与它的行列式来解决生成树计数问题
我又把Matrix写错啦 这东西讲课的时候竟然一笔带过了,淦 好吧这东西我不会证 那我们来愉快的看结论吧 啦啦啦 预备工作 你有一个 $ n $ 个点的图 比如说 5 /|\ / | \ 2--1-- ...
- HDU 1010生成树
求起点到终点的最短权值和
- hdu 4305 概率dp
/* 题目大意:有n个房间由n-1个隧道连接起来,从1号房间开始, 每个节点i都有三种可能: 1.被杀死,回到节点1,概率为ki; 2.找到出口,离开迷宫,概率ei; 3.与它相连的有m个房间,到任意 ...
随机推荐
- SC || 解决在git中上传过大文件的问题(如何将提交过的彻底删除
就在我在ddl前续命的时候……不知道怎么想不开,把v2的压力测试的日志(500多M)也往github上传 之前听说过好多因为传了大文件的锅…… 我竟然还想不开的往上传…… 真实又傻又蠢又自闭(T T ...
- java基础—多态(动态加载)
一.面向对象最核心的机制——动态绑定,也叫多态
- Js笔记-第17课
课 // 作业 //深度拷贝 var obj = { name:'rong', age:'25', card:['visa','alipay'], nam :['1','2','3','4','4'] ...
- Maven各种常用架包配置文件,保存一份
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- C语言输出多位小数
#include<stdio.h>#include<stdlib.h>int main(){int i=0;int m=19;int n=3;int s=0;s=m/n;pri ...
- pandas学习series和dataframe基础
PANDAS 的使用 一.什么是pandas? 1.python Data Analysis Library 或pandas 是基于numpy的一种工具,该工具是为了解决数据分析人物而创建的. 2.p ...
- 【前端_js】Json对象和Json字符串的区别
转载1: Json对象和Json字符串的区别 转载2: JSON字符串与JSON对象的区别
- (71)Received empty response from Zabbix Agent问题解决
刚接触zabbix新手少部分会出现如下错误: Received empty response from Zabbix Agent at [192.168.1.2]. Assuming that age ...
- php使用curl访问https返回无结果的问题
最近在做一个微信自动登录,发起验证以后回调页面获取openid时 curl函数返回空. $appid = "appid appid "; $secret = "secre ...
- Python入门基础--字符编码与文件处理
字符编码 文本编辑器存取文件的原理 #1.打开编辑器就打开了启动了一个进程,是在内存中的,所以,用编辑器编写的内容也都是存放与内存中的,断电后数据丢失 #2.要想永久保存,需要点击保存按钮:编辑器把内 ...