SPOJ104. Highways

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

Sample output:
8
1
1
3

Added by: Piotr ?owiec
Date: 2004-07-02
Time limit: 7s
Source limit: 50000B
Memory limit: 1536MB
Cluster: Cube (Intel Pentium G860 3GHz)
Languages: All except: SCM chicken

生成树计数(不取模)

/*************************************************************************
    > File Name: spoj104.cpp
    > Author: ALex
    > Mail: [email protected]
    > 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;
}
时间: 2024-10-14 17:47:39

SPOJ104. Highways的相关文章

SPOJ104 Highways,生成树计数

高速公路(SPOJ104 Highways) 一个有n座城市的组成国家,城市1至n编号,其中一些城市之间可以修建高速公路.现在,需要有选择的修建一些高速公路,从而组成一个交通网络.你的任务是计算有多少种方案,使得任意两座城市之间恰好只有一条路径? 数据规模:1≤n≤12. 生成树计数 算法步骤: 1. 构建拉普拉斯矩阵 Matrix[i][j] = degree(i) , i==j -1,i-j有边 0,其他情况 2. 去掉第r行,第r列(r任意) 3. 计算矩阵的行列式 #include <m

SPOJ104 Highways,跨越数

高速公路(SPOJ104 Highways) 一个有n座城市的组成国家,城市1至n编号,当中一些城市之间能够修建快速公路.如今,须要有选择的修建一些快速公路.从而组成一个交通网络.你的任务是计算有多少种方案,使得随意两座城市之间恰好仅仅有一条路径? 数据规模:1≤n≤12. 生成树计数 算法步骤: 1. 构建拉普拉斯矩阵 Matrix[i][j] = degree(i) , i==j -1,i-j有边 0,其它情况 2. 去掉第r行,第r列(r随意) 3. 计算矩阵的行列式 #include <

[spoj104][Highways] (生成树计数+矩阵树定理+高斯消元)

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 c

【SPOJ104】【矩阵树定理】Highways

什么?你说你看不懂英文题面? 好吧其实我也看不懂- 把论文里题面翻译放在英文题面下面 HIGH - Highways no tags 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 wh

spoj104 HIGH - Highways 矩阵树定理

欲学矩阵树定理必先自宫学习一些行列式的姿势 然后做一道例题 #include <iostream> #include <cstring> #include <cstdio> #include <cmath> using namespace std; typedef long long ll; int T, n, m, uu, vv; double w[15][15]; const double eps=1e-7; void gauss(){ n--; for

UVA - 1393 Highways

Description Hackerland is a happy democratic country with m×n cities, arranged in a rectangular m by n grid and connected by m roads in the east-west direction and n roads in the north-south direction. By public demand, this orthogonal road system is

poj 2485 Highways

链接:poj 2485 题意:输入n个城镇相互之间的距离,输出将n个城镇连通费用最小的方案中修的最长的路的长度 这个也是最小生成树的题,只不过要求的不是最小价值,而是最小生成树中的最大权值,只需要加个判断 比较最小生成树每条边的大小就行 #include<cstdio> #include<algorithm> using namespace std; int f[510],n,m; struct stu { int a,b,c; }t[20100]; int cmp(struct

POJ 2485 Highways 最小生成树 (Kruskal)

Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They're planning to build some highways so that i

POJ 2485 Highways (求最小生成树中最大的边)

Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They're planning to build some highways so that i