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. Sorl初始

    Sorl是什么 ? Solr是Apache下的一个开源项目,使用Java基于Lucene开发的全文检索服务:是一个独立的企业级搜索应用服务器,它对外提供类似于Web-service的API接口.用户可 ...

  2. CountDownLatch await可能存在的问题

    执行countdown的某个子线程可能会因为某些原因无法执行countdown,这样就会导致await线程一直阻塞下去. 在线程池中多次调用await方法,因为await方法会阻塞一段时间,有可能导致 ...

  3. Linux查看和剔除当前登录用户

    Linux查看和剔除当前登录用户 如何在linux下查看当前登录的用户,并且踢掉你认为应该踢掉的用户? 看了网络中的一些例子.在这里总结一下.主要用到的命令有,w,who,ps,kill,pkill ...

  4. 黑客落网记:FBI如何抓捕Anonymous核心成员

    腾讯科技讯 美国新闻网站Daily Dot近日撰文,通过他们掌握的资料和实地采访,还原了Anonymous核心成员被捕的经过. 以下为文章全文: 哈蒙德被捕前夜 2012年3月,一个周六的下午,天气异 ...

  5. React 16.3.0 发布,构建用户界面的 JavaScript 库

    React 16.3.0 已发布,React 是 Facebook 推出的一个为数据提供渲染为 HTML 视图,用来构建用户界面的开源 JavaScript 库. React 视图通常采用包含以自定义 ...

  6. 小米2S刷Android4.4且双系统共存

    视频教程 http://v.youku.com/v_show/id_XNjQxNzQ5NDAw.html 必备知识 进入Recovery模式:同时按住电源键+音量上键,等到屏幕亮起时,放开电源键. 特 ...

  7. ubuntu 16.04 配置远程连接

    1.XDMCP远程连接 vi /usr/share/lightdm/lightdm.conf.d/50-ubuntu.conf 添加 greeter-show-manual-login=true [X ...

  8. 设计模式--桥梁模式C++实现

    1定义 将抽象和实现解耦,使得两者可以独立变化 2类图 3实现 #pragma once #include<iostream> using namespace std; class Imp ...

  9. ContOS网络连接及简单的ssh Xshell连接!

    这边简单的记录一下下ContOS网络连接及简单的ssh Xshell连接! 首先你得安装一个Contos Linux系统对吧! 1.找到设置--->网络-->有线连接-->IPv4 ...

  10. Intellij IDEA的一些操作小技巧

    1.Presentation Mode 我们可以使用 Presentation Mode,将IDEA弄到最大,可以让你只关注一个类里面的代码,进行毫无干扰的 coding.可以使用Alt+v快捷键,弹 ...