前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SP104 HIGH - Highways

SP104 HIGH - Highways

作者头像
attack
发布2018-04-11 10:51:07
6230
发布2018-04-11 10:51:07
举报

题目描述

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.

输入输出格式

输入格式:

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.

输出格式:

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.

输入输出样例

输入样例#1:

代码语言:javascript
复制
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

输出样例#1: 

代码语言:javascript
复制
8
1
1
3

MatrixTree定理的裸题

记录给出的图中每个点的度数矩阵与邻接矩阵

做差

高斯消元

把对角线乘起来

输出

代码语言:javascript
复制
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
const int MAXN=3001;
const double eps=1e-12;
inline int read()
{
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}
double G[MAXN][MAXN],a[MAXN][MAXN];
char s[MAXN][MAXN];
int xx[5]={0,-1,+1,0,0};
int yy[5]={0,0,0,-1,+1};
int N,M;
int dcmp(int x)
{
    if(x<=eps||x>=-eps) return 0;
    else return x<0?-1:1;
}
void Gauss()
{
    N--;
    for(int i=1;i<=N;i++)//每一行 
    {
        int mx=i;
        for(int j=i+1;j<=N;j++)//下面的每一行 
            if(dcmp(G[mx][i]-G[j][i])<0) mx=j;
        if(mx!=i) swap(G[i],G[mx]);
        if(!G[i][i]) {printf("0\n");return ;}
        for(int j=i+1;j<=N;j++)
        {
            double t=G[j][i]/G[i][i];
            for(int k=i;k<=N+1;k++)
                G[j][k]-=t*G[i][k];
        }
    }
    double ans=1;
    for(int i=1;i<=N;i++) ans=ans*G[i][i];
    printf("%.0f\n",abs(ans));
}
int main()
{  
    int T=read();
    while(T--)
    {
        memset(G,0,sizeof(G));
        N=read(),M=read();
        for(int i=1;i<=M;i++)
        {
            int x=read(),y=read();
            G[x][x]++;G[y][y]++;
            G[x][y]--;G[y][x]--;
        }
        Gauss();  
    }
    return 0;  
}  
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-02-21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目描述
  • 输入输出格式
  • 输入输出样例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档