UvaLive 6600 Spanning trees in a secure lock pattern matrix, uvalivespanning
Link: https://icpcarchive.ecs.baylor.edu/index.php? Option = com_onlinejudge & Itemid = 8 & page = show_problem & problem = 4611
Question: Give a matrix of N * N vertices (N <= 6). Each vertex can only be connected to eight neighboring vertices and ask how many tree generation methods are available.
Idea: The question is very clear, that is, to list a matrix of the edge of each vertex, and then obtain the determinant of the submatrix. Because N is only 6, you can create a table.
Code for table creation:
#include <algorithm>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <ctime>#include <ctype.h>#include <iostream>#include <map>#include <queue>#include <set>#include <stack>#include <string>#include <vector>#define eps 1e-8#define INF 0x7fffffff#define PI acos(-1.0)#define seed 31//131,1313typedef long long LL;typedef unsigned long long ULL;using namespace std;#define MOD 1000#define maxn 40#define maxm 40struct Matrix{ int n,m; double a[maxn][maxm]; void change(int c,int d) { n=c; m=d; for(int i=0; i<n; i++) for(int j=0; j<m; j++) a[i][j]=0; } void Copy(const Matrix &x) { n=x.n; m=x.m; for(int i=0; i<n; i++) for(int j=0; j<m; j++) a[i][j]=x.a[i][j]; } void build(int n) { change(n*n,n*n); for(int i=0; i<n*n; i++) { if(i%n!=0) { a[i][i-1]=-1; a[i-1][i]=-1; a[i][i]++; a[i-1][i-1]++; } if(i%n!=0&&i/n!=0) { a[i][i-n-1]=-1; a[i-n-1][i]=-1; a[i][i]++; a[i-n-1][i-n-1]++; } if(i%n!=0&&i/n!=n-1) { a[i][i+n-1]=-1; a[i+n-1][i]=-1; a[i][i]++; a[i+n-1][i+n-1]++; } if(i/n!=n-1) { a[i][i+n]=-1; a[i+n][i]=-1; a[i][i]++; a[i+n][i+n]++; } } } double det() { for(int i=1; i<n; i++) { for(int j=0; j<i; j++) if(a[i][j]!=0) { for(int k=j+1; k<m; k++) a[i][k]-=(a[j][k]*a[i][j]/a[j][j]); a[i][j]=0; } } double ans=1; for(int i=0; i<n-1; i++) ans*=a[i][i]; return ans; }};int main(){ int t; scanf("%d",&t); Matrix A; A.build(t); printf("%.0f\n",A.det()); return 0;}
AC code:
int main(){ char ss[10][40]={"1","16","17745","1064918960","3271331573452806","504061943351319050000000"}; int T; scanf("%d",&T); while(T--) { int a; scanf("%d",&a); puts(ss[a-1]); }}