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个房间,到任意 ...
随机推荐
- 【转】Java8学习笔记(1) -- 从函数式接口说起
http://blog.csdn.net/zxhoo/article/details/38349011 函数式接口 理解Functional Interface(函数式接口,以下简称FI)是学习Jav ...
- jvm | 基于栈的解释器执行过程
一段简单的算术代码: public class Demo { public static void main(String[] args) { int a = 1; int b = 2; int c ...
- Vue 后台管理
这里是结合vue和element快速成型的一个demo 里面展示了基本的后台管理界面的大体结构和element的基本操作 GitHub的地址:https://github.com/wwwming/ad ...
- MySQL中同时存在创建和更新时间戳字段解决方法浅析
MySQL中同时存在创建和更新时间戳字段解决方法浅析 明确我的MySQL版本.mysql> SELECT VERSION();+------------+| VERSION() |+------ ...
- Spinal Tap Case -freecodecamp算法题目
Spinal Tap Case 1.要求 将字符串转换为 spinal case. Spinal case 是 all-lowercase-words-joined-by-dashes 这种形式的,也 ...
- 2018 noip 提高组初赛参考答案
这里有pdf文件:戳这儿
- C语言程序运行
vs2013编辑器 c程序的运行 一.启动Microsoft Visual C++ 2013版.新建项目 . 1. 文件——> 新建——> 项目. 2. 确定之后 弹出 ...
- 【莫队】bzoj4542: [Hnoi2016]大数
挺有意思的,可以仔细体味一下的题:看白了就是莫队板子. Description 小 B 有一个很大的数 S,长度达到了 N 位:这个数可以看成是一个串,它可能有前导 0,例如00009312345.小 ...
- pm2日志记录和日志分割
pm2日志记录和日志分割 pm2介绍 pm2是nodejs进程管理工具,现在基本是node生产服务器的标准选择,可以帮助我们实现node多进程服务,开启的多个实例自动实现负载均衡. 最重要的是保证no ...
- JAVA基础篇—Servlet小结
一.get请求和post请求的区别: 1.get请求是通过url传递参数,post请求是通过请求体传递参数的 2.get请求最多允许传递255个字符,对长度有限制,所以数据比较大的时候我们使用post ...