【SPOJ104】【矩阵树定理】Highways

根据矩阵树定理,解决一个涉及城市和直接连接的高速公路网络构建问题。需计算在1到12个城市间,使得每对城市间恰好存在一条路径的不同构建方式数量。输入包含测试用例数量及每例的城市数和连接数,输出每例的构建方式计数。示例给出了4个测试用例及其对应答案。
摘要由CSDN通过智能技术生成

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

[例一]高速公路(SPOJ p104 Highways)
一个有 n 座城市的组成国家,城市 1 至 n 编号,其中一些城市之间可以修建
高速公路。现在,需要有选择的修建一些高速公路,从而组成一个交通网络。你
的任务是计算有多少种方案,使得任意两座城市之间恰好只有一条路径?
数据规模:1≤n≤12。

题意就是有n个点m条边,求生成树的个数.
显然Matrix-tree定理可以秒掉…
用Gauss消元 O(n3) 的计算行列式的值最后取一下绝对值.
(作为之前连Gauss消元都没有写过的人竟然能1A真是有点爽233)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 20
#define eps 1e-9
using namespace std;
int A[MAXN][MAXN],D[MAXN][MAXN];//A是邻接矩阵D是度数矩阵 
double C[MAXN][MAXN];//基尔霍夫矩阵 
int T,n,m;
void in(int &x)
{
    x = 0;
    char ch = getchar();
    while (!(ch >= '0' && ch <= '9')) ch = getchar();
    while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0',ch = getchar();
}
void gauss()
{
    int now = 1;
    for (int i = 1;i <= n;i++)
    {
        int x = now;
        while (fabs(C[x][now]) < eps && x <= n) x++;
        if (x == n + 1) 
        {
            puts("0");
            return;
        }
        for (int j = 1;j <= n;j++)  swap(C[now][j],C[x][j]);
        for (int j = now + 1;j <= n;j++)
        {
            double temp = C[j][now] / C[now][now];
            for (int k = 1;k <= n;k++)
                C[j][k] -= temp * C[now][k];
        }
        now++;
    }
    double ans=1;
    for (int i = 1;i <= n;i++) ans*=C[i][i];
    ans=fabs(ans);
    printf("%.0lf\n",ans);
}
int main()
{
    in(T);
    while (T--)
    {
        memset(A,0,sizeof(A));
        memset(D,0,sizeof(D));
        in(n);in(m);
        n--;
        for (int i = 1;i <= m;i++)
        {
            int u,v;
            in(u);in(v);
            D[u][u]++;D[v][v]++;
            A[u][v]++;A[v][u]++;
        }
        for (int i = 1;i <= n;i++)
            for (int j = 1;j <= n;j++)
                C[i][j] = D[i][j] - A[i][j];
        gauss();
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值