SPOJ104 Highways


Description

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.

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


矩阵树定理模板题,有一些细节:

  • 发现自由元立即切出去
  • 输出答案时向下取整(懵)

然后就没了

#include<bits/stdc++.h>
using namespace std;
#define N 3010
int n,m;
double g[N][N];
void gauss(){
n--;
for(int i=1;i<=n;i++){
int r=i;
for(int j=i+1;j<=n;j++)
if(fabs(g[j][i])>fabs(g[r][i]))r=j;
if(r!=i)for(int j=1;j<=n+1;j++)swap(g[i][j],g[r][j]);
if(!g[i][i]){cout<<0<<endl;return;}
for(int k=i+1;k<=n;k++){
double f=g[k][i]/g[i][i];
for(int j=i;j<=n+1;j++)g[k][j]-=f*g[i][j];
}
}
double ans=1;
for(int i=1;i<=n;i++)ans*=g[i][i];
printf("%.0f\n",abs(ans));//一定要加 向下取整
}
int main(){
int T;cin>>T;
while(T--){
memset(g,0,sizeof(g));
cin>>n>>m;
for(int i=1;i<=m;i++){
int x,y;cin>>x>>y;
g[x][x]++;g[y][y]++;
g[x][y]--;g[y][x]--;
}
gauss();
}
return 0;
}

SPOJ104 Highways 【矩阵树定理】的更多相关文章

  1. spoj104 HIGH - Highways 矩阵树定理

    欲学矩阵树定理必先自宫学习一些行列式的姿势 然后做一道例题 #include <iostream> #include <cstring> #include <cstdio ...

  2. SPOJ Highways [矩阵树定理]

    裸题 注意: 1.消元时判断系数为0,退出 2.最后乘ans要用double.... #include <iostream> #include <cstdio> #includ ...

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

    In some countries building highways takes a lot of time... Maybe that's because there are many possi ...

  4. spoj104 highways 生成树计数(矩阵树定理)

    https://blog.csdn.net/zhaoruixiang1111/article/details/79185927 为了学一个矩阵树定理 从行列式开始学(就当提前学线代了.. 论文生成树的 ...

  5. 【SPOJ】Highways(矩阵树定理)

    [SPOJ]Highways(矩阵树定理) 题面 Vjudge 洛谷 题解 矩阵树定理模板题 无向图的矩阵树定理: 对于一条边\((u,v)\),给邻接矩阵上\(G[u][v],G[v][u]\)加一 ...

  6. 算法复习——矩阵树定理(spoj104)

    题目: In some countries building highways takes a lot of time... Maybe that's because there are many p ...

  7. BZOJ 4766: 文艺计算姬 [矩阵树定理 快速乘]

    传送门 题意: 给定一个一边点数为n,另一边点数为m,共有n*m条边的带标号完全二分图$K_{n,m}$ 求生成树个数 1 <= n,m,p <= 10^18 显然不能暴力上矩阵树定理 看 ...

  8. bzoj 4596 [Shoi2016]黑暗前的幻想乡 矩阵树定理+容斥

    4596: [Shoi2016]黑暗前的幻想乡 Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 559  Solved: 325[Submit][Sta ...

  9. 【LOJ#6072】苹果树(矩阵树定理,折半搜索,容斥)

    [LOJ#6072]苹果树(矩阵树定理,折半搜索,容斥) 题面 LOJ 题解 emmmm,这题似乎猫讲过一次... 显然先\(meet-in-the-middle\)搜索一下对于每个有用的苹果数量,满 ...

随机推荐

  1. centos安装新版svn

    CentOS/RHEL yum 安装的 subversion 是 1.6.11 版本,连VisualSVN服务器时会有"Key usage violation"的错误 将subve ...

  2. Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 离散化+逆元

    E. Mike and Geometry Problem time limit per test 3 seconds memory limit per test 256 megabytes input ...

  3. 一切从Trade开始(转)

    taobao.trades.sold.get 取得交易列表 从店铺取得时间 taobao.shop.get (nick,field)shop.created 循环读取从开店时间到当前时间的所有交易 t ...

  4. canvas drawimage绘制图像出错(默认canvas300*150)解决办法

    今天在开发中,再一次踩了以前踩过的坑,写完程序在这里写一下,分享给大家也避免再次遇坑. //默认的canvas <canvas id ="canvas"></ca ...

  5. spring mvc: json练习

    spring mvc: json练习 本例需要用到的json包: 如下: jackson-databind jackson-core jackson-annotations <!-- https ...

  6. android 命令行签名apk文件

    签名apk 1.将apk格式改为zip格式包,然后删除原来apk里面的META-INF文件夹,之后改回apk文件格式 2.cmd命令行: jarsigner -verbose -keystore C: ...

  7. CentOS 7.2下编译安装PHP7.0.10+MySQL5.7.14+Nginx1.10.1

    一.安装前的准备工作 1.yum update    #更新系统 2.yum install gcc gcc-c++ autoconf automake cmake bison m4 libxml2  ...

  8. 6.你以为你真的了解final吗?

    1. final的简介 final可以修饰变量,方法和类,用于表示所修饰的内容一旦赋值之后就不会再被改变,比如String类就是一个final类型的类.即使能够知道final具体的使用方法,我想对fi ...

  9. oom_killer

    Limited Memory 今天在虚拟机里面用Word处理文档的时候,突然硬盘灯一阵狂闪,然后虚拟机就一起消失了. 这种事情屡见不鲜,很明显是Linux内核把占用最多内存的程序(这次是Virtual ...

  10. 收集的几个好用的maven mirror

    <mirrors> <mirror> <id>jboss-public-repository-group</id> <mirrorOf>ce ...