"HDU 4305" Lightning (Spanning tree count)

Source: Internet
Author: User

Problem Descriptionthere is 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'll spread lightning to the near one.


The spreading happens when:
Robot A is overladen and 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, spreading happens at a same time and no to both robots stand at a same position.


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

Inputthere is several cases.
The first line was an integer t (t < =), indicate the test cases.
For each case, the first line contains integer N (1 < = N < =) and R (0 < = r < = 20000), indicate the Re stand N robots; Following N lines, each contains, integers (x, y) ( -10000 < = x, y < = 10000), indicate the position of the R Obot.

Outputone line is contains the answer.

Sample Input33 2-1 00 11 03 2-1 00 01 03 1-1 00 11 0

Sample output31-1 Test Instructions:

Give the coordinates of n points, the distance of not more than r points if there is no other point in the middle can be connected to a side, the last to survive into the number of trees, to 10007 modulo.

Analysis:

Matrix-tree theorem: Theabsolute value of the determinant of an arbitrary n-1-order matrix of the Kirchhoff matrix is the number of spanning trees of the non-graph.

The definition of the Kirchhoff matrix is the degree matrix-adjacency matrix.

1, G of the degree matrix D[g]:n*n Matrix, DII equals the degree of VI, the remaining 0.
2, G of the adjacency matrix A[g]:n*n Matrix, Vi, VJ is directly connected to the edge, then aij=1, otherwise 0.

With this theorem, we need to construct the Kirchhoff matrix, and then calculate the determinant on the line, pay attention to modulo.

The construction matrix needs to judge whether there are other points between two points that meet the distance not exceeding R, and the direct three-layer loop uses the cross product to determine if the Ijk is collinear, if the collinear k is between IJ.

Then there is the determinant of the calculation:

is the knowledge of linear algebra, first through the elementary transformation into the upper triangular determinant, the main diagonal product is the determinant of the value.

And the process of elementary transformation, if the exchange of two lines, the determinant to multiply 1, so the record exchange several times, and finally according to the odd-even to multiply-1. We're going to use CII to eliminate the number below it. The first line of each number is divided into CII, this process because we are modulo, so we have to use the inverse, and then pre-preprocess the inverse.

#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #define SQR (x) ((x) * (  x)) using namespace std;const int M=10007;const int n=301; int inv[m],mat[n][n];void init () {//Seek inverse inv[1]=1;for (int i=2;i<m;i++) inv[i]= (m-m/i) *inv[m%i]%m;} int det (int c[][n],int N) {//N-Order order of Matrix C Master int i,j,k,w=0,ans=1;for (i=0;i<n;i++) for (j=0;j<n;j++) c[i][j]= (C[i][j] %m+m)%m;for (i=0;i<n;i++) {for (j=i;j<n;j++)//Find row I column not 0 line if (C[i][j]) break;if (i!=j) {w++;//interchange number swap (c[i],c [j]);} Ans=ans*c[i][i]%m;for (j=i+1;j<n;j++)//The first column of line J becomes 0for (k=n;k>i;k--)//The row subtracts the value of column I in each column *dc[j][k]= (c[j][k]-c[i][k]* inv[c[i][i]]%m*c[j][i]%m+m)%M;} Return (ans* (w&1?-1:1) +m)%M; struct point{int x, y;} P[n];int Same (Point A,point b,point c) {//Determine if collinear return (a.x-c.x) * (b.y-c.y) = = (b.x-c.x) * (A.Y-C.Y) &&min (a.x,c.x ) <=b.x&&max (a.x,c.x) >=b.x&&min (A.Y,C.Y) <=b.y&&max (A.Y,C.Y) >=b.y;} int main () {init (); int t,n,r;scanf ("%d", &t), while (t--) {memset (mat,0,sizeof mat); SCANF ("%d%d", &n,&r), for (int i=0;i<n;i++) scanf ("%d%d", &p[i].x,&p[i].y), and for (int i=0;i<n;i++) for (int j=i+1;j<n;j++) if (sqrt (SQR (p[i].x-p[j].x) +SQR (P[I].Y-P[J].Y)) <=r) {//Distance not greater than rint ok=1;for (int k=0;k<n;k + +) if (K!=i&&k!=j&&same (P[i],p[k],p[j])) ok=0;if (OK) {//construct Kirchhoff matrix Mat[i][j]=mat[j][i]=-1;mat[i ][i]++;mat[j][j]++;}} int Ans=det (mat,n-1);p rintf ("%d\n", Ans?ans:-1);}}

  

"HDU 4305" Lightning (Spanning tree count)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.