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个房间,到任意 ...
随机推荐
- 关于flyme5显示不到和卸载不到旧应用解决方法
笔者买入一台mx5,升级flyme5后旧应用没有显示出来,而且在设置的应用管理都没显示旧应用. 通过adb命令: adb shell pm list packages显示所有包名, 查看自己要删除应用 ...
- TypeError: Cannot read property 'tap' of undefined
E:\vue-project\vue-element-admin-master>npm run build:prod vue-element-admin@3.8.1 build:prod E:\ ...
- mysql 在线添加字段
使用工具pt-online-schema-change #! /bin/bash stime=`date +%s` echo "增加字段开始测试时间为:`date +%H:%M:%S`&qu ...
- python 程序小测试
python 程序小测试 对之前写的程序做简单的小测试 ... # -*- encoding:utf-8 -*- ''' 对所写程序做简单的测试 @author: bpf ''' def GameOv ...
- day12-迭代器
迭代器的概念 内部含有_next_和_iter_方法的就是迭代器. 可以被for循环的都是可迭代的,只有是可迭代对象,才能用for循环. 可迭代的内部都有_iter_方法——可迭代协议. 只要是迭代器 ...
- Python使用gevent实现协程
Python中多任务的实现可以使用进程和线程,也可以使用协程. 一.协程介绍 协程,又称微线程.英文名Coroutine.协程是Python语言中所特有的,在其他语言中没有. 协程是python中另外 ...
- Python头脑风暴1
发个致富脑洞:我就在想本人虽然单身,但本人恋爱经历很多,追女生技术十足,女朋友漂亮又贤惠.如果本人开个平台帮人诚心介绍女朋友,男女成男女朋友经男方同意我收2.5万(IT界平均月收入的1.5倍不到),双 ...
- request response cookie session
request 1. url传递参数 1)参数没有命名, 如: users/views def weather(request, city, year): print(city) print(year ...
- 用python编写简易登录接口
需求: 让用户输入用户名密码 认证成功后显示欢迎信息 输错三次后退出程序 可以支持多个用户登录 用户3次认证失败后,退出程序,再次启动程序尝试登陆时,还是锁定状态 下面是我写的代码,如果有BUG或者不 ...
- QT添加自定义信号后编译出现undefined reference
QT添加自定义信号后编译出现undefined reference 这是需要重新生成qmake: build --->run qmake