HDOJ 4305-Lightning: determining the online segment of a vertex + constructing a Matrix Tree to survive the decision Tree + Gaussian elimination

Source: Internet
Author: User

Question:

There are N points on the two-dimensional plane... if the distance between two points is not greater than R and there are no other points in the line segment .. you can connect an edge .. ask now .. how many spanning trees does this graph have?

Question:

I think this question is so embarrassing... some knowledge of computing + magic matrix + Gaussian elimination element (at the same time, the reverse element is required )...

Edge construction... according to the requirements of the question .. OK has an edge... construct the Matrix Tree directly when constructing the edge... the Matrix constructed by Matrix Tree indicates the degree of each vertex on the diagonal .. the non-diagonal line is the opposite number of The number of connected edges between two points .. this question has no overlap. then the edge is-1 .. 0 if no side exists... construct a matrix .. remove any cross .. the sum of the remaining determinant is the number of spanning trees of the graph .. it's amazing .. I don't know how to prove it... I used it directly... the final requirement is the determining factor of a matrix... there are many methods... it is generally used to describe the Gaussian element... the Gaussian elimination method is to keep one column with only one number at a time .. about other numbers... finally, the matrix is converted into an upper triangle .. the product of the diagonal line is the determining factor of the matrix... A few minutes will usually appear during manual calculation... if the program also uses double to write the last return to int .. there may be some precision problems .. however, Gaussian element elimination can make each step an integer. for example .. because the final answer requires mod a number... when Division is required .. just use reverse yuan directly .. if there is no modulo operation .. then perform lcm on all rows... and then disappear...

Program:

# Include <iostream> # include <stdio. h> # include <string. h> # include <cmath> # include <queue> # include <stack> # include <set> # include <time. h> # include <map> # include <algorithm> # define ll long # define eps 1e-5 # define oo 10007 # define pi acos (-1.0) # define MAXN 305 using namespace std; struct node {int x, y; node () {} node (int x, int y): x (x), y (y) {}} p [MAXN]; typedef node Vector; Vector operator-(node a, no De B) {return Vector (. x-B. x,. y-B. y);} int Dot (Vector a, Vector B) {return. x * B. x +. y * B. y;} int dis (node p1, node p2) {return (p1.x-p2.x) * (p1.x-p2.x) + (p1.y-p2.y) * (p1.y-p2.y );} int Cross (Vector a, Vector B) {return. x * B. y-. y * B. x;} bool InLine (node p1, node p2, node p3) {return Cross (p1-p2, p3-p2) = 0 & Dot (p1-p2, p3-p2) <0;} int C [MAXN] [MAXN]; void exgcd (int A, int B, int & x, int & y) {if (B = 0) {x = 1, y = 0; return;} exgcd (B, a % B, x, y); int t; t = x, x = y, y = t-a/B * y; return;} int det (int n) // calculate the absolute value % mod {int ans = 1, flag = 1; // The number of flag row and column exchanges .. *-1 int I, j, k; for (I = 0; I <n; I ++) {if (C [I] [I] = 0) {for (j = I + 1; j <n; j ++) if (C [j] [I]) break; if (j = n) return 0; // All values in a column are 0 ans = 0; flag =! Flag; for (int k = I; k <n; k ++) swap (C [I] [k], C [j] [k]); // I and j-row interchange} ans = ans * C [I] [I] % oo; // multiply the diagonal lines by int x and y; exgcd (C [I] [I], oo, x, y); // x indicates the reverse element. if no modulo operation is performed, LCM is required to convert it to for (k = I + 1; k <n; k ++) C [I] [k] = C [I] [k] * x % oo; for (int j = I + 1; j <n; j ++) for (int k = I + 1; k <n; k ++) {C [j] [k] = (C [j] [k]-C [j] [I] * C [I] [k]) % oo; C [j] [k] = (C [j] [k] + oo) % oo;} ans = (ans % oo + oo) % oo; if (flag) return ans; else return oo-ans;} int main () {int T, N, R, I, j, x; scanf ("% d", & T); while (T --) {scanf ("% d", & N, & R); R * = R; for (I = 0; I <N; I ++) scanf ("% d", & p [I]. x, & p [I]. y); memset (C, 0, sizeof (C); for (I = 0; I <N; I ++) for (j = I + 1; j <N; j ++) if (dis (p [I], p [j]) <= R) {for (x = 0; x <N; x ++) if (InLine (p [I], p [x], p [j]) goto A; C [I] [I] ++, C [j] [j] ++; C [I] [j] = C [j] [I] =-1; A:;} x = det (N-1 ); if (! X) x =-1; printf ("% d \ n", x);} return 0 ;}

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.