SPOJ HIGH (生成树计数)
2016-05-05 21:12
190 查看
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 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 numberof 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
Submit solution!
题意:无向图的生成树计数.
没有自环没有重边,裸题.
#include <bits/stdc++.h>
using namespace std;
#define maxn 15
#define eps 1e-10
long long a[maxn][maxn];
int n, m;
long long det () //按列化为下三角
{
long long res = 1;
for(int i = 0; i < n; ++i)
{
if (!a[i][i])
{
bool flag = false;
for (int j = i + 1; j < n; ++j)
{
if (a[j][i])
{
flag = true;
for (int k = i; k < n; ++k)
{
swap (a[i][k], a[j][k]);
}
res = -res;
break;
}
}
if (!flag)
{
return 0;
}
}
for (int j = i + 1; j < n; ++j)
{
while (a[j][i])
{
long long t = a[i][i] / a[j][i];
for (int k = i; k < n; ++k)
{
a[i][k] = a[i][k] - t * a[j][k];
swap (a[i][k], a[j][k]);
}
res = -res;
}
}
res *= a[i][i];
}
return res;
}
int main()
{
//freopen("in.txt", "r", stdin);
int t;
cin >> t;
while (t--) {
cin >> n >> m;
memset (a, 0, sizeof a);
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v; u--, v--;
a[u][u]++, a[v][v]++;
a[u][v] = a[v][u] = -1;
}
n--;
cout << det () << "\n";
}
return 0;
}
相关文章推荐
- Android----ObjectAnimator and ValueAnimator
- 第十周项目—阅读程序,写出执行结果(2)
- 定时器
- CRF(条件随机场)
- 在tomcat上全手工部署Servlet3.0
- 【前端】screenX/Y, clientX/Y, pageX/Y 的区别
- LeetCode-10. Regular Expression Matching
- NYOJ-183赚钱啦,bellman//spfa水过,,题还是蛮变态的赶脚~~
- 关于View.getHeight和getWidth为0的解决方案(待深入学习)
- 原型模式(Prototype)以及深浅复制
- 快速排序复习
- Hadoop2.4.1伪分布式搭建
- leetcode_097 Interleaving String
- 详解mysql int数字类型的长度值大小上限
- Spring的反射工具类 ReflectionUtils
- 地图处理之基本使用汇总
- 要读的论文
- 关于堆栈的一些认识
- Android Volley增加Https支持,允许SSL
- linux添加用户、权限