The odd number of Spanning Tree in HDU 4305

Source: Internet
Author: User
Lightning

Time Limit: 4000/2000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1457 accepted submission (s): 469


Problem descriptionthere 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.

 

Inputthere 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.

 

Outputone line for each case contains the answer.

 

Sample input33 2-1 00 11 03 2-1 00 01 03 1-1 00 11 0 sample Output31-1 authorbupt source2012 multi-university training contest 1

Matrix-tree theorem (Kirchhoff matrix-tree theorem ). Matrix-tree theorem is one of the most powerful weapons to solve the problem of generating tree counts. It was first proved by Kirchhoff in 1847. Before introducing the theorem, we should first clarify several concepts:

1. G's degree matrix D [g] is a n * n matrix, and satisfies the following requirements: dij = 0 when I =j; when I = J, dij is equal to the level of VI.

2. The G adjacent matrix A [g] is also a n * n matrix, and satisfies the following requirements: If the VI and vj are directly connected by edges, AIJ = 1; otherwise, it is 0.

We define G's Kirchhoff matrix (also called Laplace operator) C [g] As C [g] = d [g]-A [g], the matrix-tree theorem can be described as follows: the number of all different spanning trees of G is equal to the absolute value of any n-1 primary-child type of its Kirchhoff matrix C [g. The n-1 primary subtype is the new matrix obtained by removing row R and column R of C [g] From R (1 ≤ r ≤ n, in cr [g] format.

 

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 using namespace STD; 6 7 typedef _ int64 ll; 8 const int maxn = 305; 9 const int mod = 10007; 10 int N, R; 11 int G [maxn] [maxn], d [maxn] [maxn], mat [maxn] [maxn]; // degree matrix, degree matrix, D-G Matrix 12 13 struct point 14 {15 int X, Y; 16 Point (INT x = 0, int y = 0): x (x), y (y) {} 17} p [maxn]; 18 typedef point vector; 19 vector operator-(vector A, vecto R B) {return vector (. x-B.x,. y-B.y);} 20 int dot (vector A, vector B) {return. x * B. X +. y * B. y;} // Point product 21 int cross (vector A, vector B) {return. x * B. y-A.y * B. X ;}// Cross Product 22 int lengh2 (vector A) {return dot (a, a);} // The square 23 bool onsegment (point P, Point A1, point A2) // whether the point is on a straight line (excluding the endpoint) 24 {25 return cross (a1-p, a2-p) = 0 & dot (a1-p, a2-p) <0; 26} 27 28 ll extended_euclid (ll a, LL B, ll & X, ll & Y) 29 {30 LL D, T; 31 If (B = 0) {x = 1; y = 0; return a;} 32 d = extended_euclid (B, A % B, x, y ); 33 t = x; X = y; y = T-A/B * Y; 34 return D; 35} 36 ll inv (LL A, ll N) // calculate the inverse of a under model n. If there is no inverse, return-1 37 {38 ll D, X, Y; 39 D = extended_euclid (A, n, x, y ); 40 return d = 1? (X + n) % N:-1; 41} 42 int det (int n) // mod for the value of the determinant. Use the reverse element 43 {44 int I, j, K, ret = 1; 45 for (I = 0; I <n; I ++) 46 for (j = 0; j <n; j ++) 47 mat [I] [J] = (MAT [I] [J] % mod + mod) % MOD; 48 for (I = 0; I <n; I ++) 49 {50 for (j = I; j <n; j ++) 51 if (MAT [J] [I]! = 0) 52 {53 for (k = I; k <n; k ++) Swap (MAT [I] [K], mat [J] [k]); 54 if (I! = J) ret = (-ret + mod) % MOD; 55 break; 56} 57 if (MAT [I] [I] = 0) 58 {59 ret = 0; break; 60} 61 for (j = I + 1; j <n; j ++) 62 {63 int mut = (MAT [J] [I] * inv (MAT [I] [I], MoD) % MOD; 64 for (k = I; k <n; k ++) 65 mat [J] [k] = (MAT [J] [k]-(MAT [I] [k] * MUT) % mod + mod) % MOD; 66} 67 ret = (Ret * mat [I] [I]) % MOD; 68} 69 return ret; 70} 71 72 void build_graph () 73 {74 memset (G, 0, sizeof (g); 75 memset (D, 0, sizeof (d); 76 int I, j, k; 77 for (I = 0; I <n; I ++) 78 {79 for (j = I + 1; j <n; j ++) 80 {81 If (length1 (P [J]-P [I])> r * r) continue; 82 bool flag = 1; 83 for (k = 0; k <n; k ++) 84 If (onsegment (P [K], p [I], p [J]) {flag = 0; break ;} 85 if (FLAG) {G [I] [J] = G [J] [I] = 1; d [I] [I] ++; d [J] [J] ++;} 86} 87} 88} 89 90 void get_dgmatrix () 91 {92 for (INT I = 0; I <N; I ++) 93 for (Int J = 0; j <n; j ++) 94 mat [I] [J] = d [I] [J]-G [I] [J]; 95} 96 97 int main () 98 {99 int t, i; 100 scanf ("% d", & T); 1 01 while (t --) 102 {103 scanf ("% d", & N, & R); 104 for (I = 0; I <n; I ++) scanf ("% d", & P [I]. x, & P [I]. y); 105 build_graph (); 106 get_dgmatrix (); 107 int ans = det (n-1); 108 printf ("% d \ n", ANS? Ans:-1); 109} 110 return 0; 111}

 

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.