SPOJ104. Highways

来源:互联网 发布:linux结束进程命令 编辑:程序博客网 时间:2024/06/05 11:08

104. Highways

Problem code: HIGH

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 testcases 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

Sample input:44 53 44 22 31 21 32 12 11 03 31 22 33 1Sample output:8113

Added by:Piotr ŁowiecDate:2004-07-02Time limit:7sSource limit:50000BMemory limit:1536MBCluster:Cube (Intel Pentium G860 3GHz)Languages:All except: SCM chicken








生成树计数(不取模)


/*************************************************************************    > File Name: spoj104.cpp    > Author: ALex    > Mail: zchao1995@gmail.com     > Created Time: 2015年01月27日 星期二 05时04分40秒 ************************************************************************/#include <map>#include <set>#include <queue>#include <stack>#include <vector>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const double pi = acos(-1);const int inf = 0x3f3f3f3f;const double eps = 1e-18;typedef long long LL;const int N = 50;double mat[N][N];int sgn (double x){if (fabs(x) < eps){return 0;}if (x < 0){return -1;}return 1;}double Det (int n){double ans = 1;int i, j, k;int sign = 0;for (i = 0; i < n; ++i){if (!sgn (mat[i][i])){for (j = i + 1; j < n; ++j){if (sgn(mat[j][i])){break;}}if (j == n){return 0;}for (k = i; k < n; ++k){swap (mat[i][k], mat[j][k]);}++sign;}double t = mat[i][i];for (int j = i + 1; j < n; ++j){mat[i][j] /= t;}ans *= t;mat[i][i] = 1;for (int j = i + 1; j < n; ++j){t = mat[j][i];for (int k = i; k < n; ++k){mat[j][k] -= mat[i][k] * t;}}}if (sign & 1){ans = -ans;}return ans;}int main(){int t;scanf("%d", &t);while (t--){int n, m, u, v;scanf("%d%d", &n, &m);memset (mat, 0, sizeof(mat));for (int i = 0; i < m; ++i){scanf("%d%d", &u, &v);--u;--v;mat[u][v] = mat[v][u] = -1;}for (int i = 0; i < n; ++i){double ret = 0;for (int j = 0; j < n; ++j){ret += mat[i][j];}mat[i][i] = -ret;}if (n == 1){printf("1\n");continue;}printf("%.0f\n", Det (n - 1));}return 0;}


1 0
原创粉丝点击