标签:矩阵
题意:给一个N*N个点的矩阵(N<=6),每个点只能和周围八个点相连,问有多少种生成树的方式。
思路:题里给的很明白,就是列一个每个点的边的矩阵,然后求子矩阵的行列式就可以了,因为N只有6,所以打表就可以了。
打表代码:
#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,1313 typedef long long LL; typedef unsigned long long ULL; using namespace std; #define MOD 1000 #define maxn 40 #define maxm 40 struct 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代码:
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]); } }
UvaLive 6600 Spanning trees in a secure lock pattern 矩阵行列式
标签:矩阵
原文地址:http://blog.csdn.net/ooooooooe/article/details/38899517