「SPOJ104」Highways
Description
In some countries building highways takes a lot of time… Maybe that’s because there are many possiblities to construct a network of highways and engineers can’t make up their minds which one to choose. Suppose we have a list of cities that can be connected directly. Your task is to count how many ways there are to build such a network that between every two cities there exists exactly one path. Two networks differ if there are two cities that are connected directly in the first case and aren’t in the second case. At most one highway connects two cities. No highway connects a city to itself. Highways are two-way.
Input
The input begins with the integer t, the number of test cases (equal to about 1000). Then t test cases follow. The first line of each test case contains two integers, the number of cities (1<=n<=12) and the number of direct connections between them. Each next line contains two integers a and b, which are numbers of cities that can be connected. Cities are numbered from 1 to n. Consecutive test cases are separated with one blank line.
Output
The number of ways to build the network, for every test case in a separate line. Assume that when there is only one city, the answer should be 1. The answer will fit in a signed 64-bit integer.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<b><tt>Sample input:</tt></b> 4 4 5 3 4 4 2 2 3 1 2 1 3 2 1 2 1 1 0 3 3 1 2 2 3 3 1 <b><tt>Sample output:</tt></b> 8 1 1 3 |
题解
参见
http://wenku.baidu.com/link?url=65Ux-q9xTLx1gcqmiWx6LmYAAUMTgIU3Qm3m4MrBGE4J_YpA6f-4CHOPYukUhvRL9H2BP_cVSscFOi0BWpShzTYJzMCbf30wNuqvmoit6uu
行列式的绝对值:先用高斯消元消成上三角矩阵,把对角线乘起来
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
#include<set> #include<map> #include<ctime> #include<queue> #include<cmath> #include<cstdio> #include<vector> #include<cstring> #include<cstdlib> #include<complex> #include<iostream> #include<algorithm> #define pi acos(-1) #define eps 1e-8 #define ll long long using namespace std; int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int T,n,m; int A[15][15],B[15][15]; double a[15][15]; void gauss() { int now=1; for(int i=1;i<=n;i++) { int j=now; while(fabs(a[j][now])<eps&&j<=n)j++; if(j==n+1){puts("0");return;} for(int k=1;k<=n;k++)swap(a[now][k],a[j][k]); for(int j=now+1;j<=n;j++) { double t=a[j][now]/a[now][now]; for(int k=1;k<=n;k++) a[j][k]-=t*a[now][k]; } now++; } double ans=1; if(n&1)ans=-ans; for(int i=1;i<=n;i++)ans*=a[i][i]; printf("%.0lf\n",abs(ans)); } int main() { T=read(); while(T--) { memset(A,0,sizeof(A)); memset(B,0,sizeof(B)); n=read();m=read(); n--; for(int i=1;i<=m;i++) { int u=read(),v=read(); u--;v--; A[u][u]++;A[v][v]++; B[u][v]++;B[v][u]++; } for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) a[i][j]=A[i][j]-B[i][j]; gauss(); } return 0; } |