Matrix-tree theorem of bzoj2467 spanning tree

Source: Internet
Author: User

http://www.lydsy.com/JudgeOnline/problem.php?id=2467

Do not know why, see this problem decisively think of maxtrix-tree theorem, and then make Kirchhoff matrix.


But first I need to understand the sample, test 3 kinds of situations to understand the sample.

The example is that two Pentagon laps share an edge, all the points of the center polygon are in degrees 4, and the surrounding +1,-1 have edges and accumulate 1 (for 2 of cases do not write a special sentence).


Then YY processing each Pentagon the remaining 3 points, and then I listed (the code may be a bit long)


And then just abandon the last line of a column, the determinant of the value is good.

(PS: I forgot to take the mold, forgot to take the mold, forgot to take the mold ~ ~ ~ ~ and Kirchhoff matrix has negative numbers, I forgot to make this ~~~~~ the last step of the test 100 is a negative number, and then remembered that did not turn it into a positive (determinant transformation may become negative ) and then +p after mod p is over, 233~~~)


#include <cstdio> #include <iostream> #include <cstring> #include <cstdlib> #include <
algorithm> #include <queue> #include <cmath> using namespace std;
int T;
int n;
int f[1020][1020];
int cnt;
	void work () {for (int. i=1;i<=cnt;i++) {for (int j=1;j<=cnt;j++) f[i][j]= (f[i][j]+2007)%2007;
	} int det=1;
				for (int i=1;i<=cnt;i++) {to (int j=i+1;j<=cnt;j++) {while (f[j][i]) {int t=-f[i][i]/f[j][i];
				for (int k=i;k<=cnt;k++) f[i][k]= (f[i][k]+t*f[j][k])%2007;
				for (int k=i;k<=cnt;k++) swap (f[i][k],f[j][k]);
			Det=-det;
			}} if (f[i][i]==0) {printf ("0\n");
		return;
		} Det*=f[i][i];
	det%=2007;
	} det+=2007;
	det%=2007;
printf ("%d\n", Det);
	} int main () {scanf ("%d", &t);
		while (t--) {memset (f,0,sizeof (f));
		scanf ("%d", &n);
	    Cnt=n;
		    for (int i=1;i<=n;i++) {f[i][i]+=4;
		    f[i][(i+1<=n) i+1:1]+=-1;
	    f[i][(i-1) >=1?i-1:n]+=-1; } for (int i=1;i<=n;i++) {cnt++;
	    	F[i][cnt]=f[cnt][i]+=-1;
	    	f[cnt][cnt]+=2;
	    	F[cnt][cnt+1]+=-1;
	    	
	    	F[cnt+1][cnt]+=-1;
	    	cnt++;
	    	f[cnt][cnt]+=2;
	    	F[cnt][cnt+1]+=-1;
	    	
	    	F[cnt+1][cnt]+=-1;
	    	cnt++;
	    	f[cnt][cnt]+=2;
	    	f[cnt][(i+1) <=n?i+1:1]+=-1;
	    f[(i+1) <=n?i+1:1][cnt]+=-1;
	    } cnt--;
	Work ();
} 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.