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 HIGH

高斯消元求行列式入门题。矩阵树定理,用D矩阵和A矩阵作差得到G,然后求G的任意一个$n−1$阶矩阵行列式,其中用到高斯消元

代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <bitset>
#include <string>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 15;
double Mat[N][N];
void init()
{
CLR(Mat, 0);
}
double Gaussian(int ne, int nv)
{
int i, j;
double ans = 1;
for (int ce = 1, cv = 1; ce <= ne && cv <= nv; ++ce, ++cv)
{
int te = ce;
for (i = ce + 1; i <= ne; ++i)
if (fabs(Mat[i][cv]) > fabs(Mat[ce][cv]))
te = ce;
if (Mat[te][cv] == 0)
return 0;
if (te != ce)
{
for (i = cv; i <= nv; ++i)
swap(Mat[ce][i], Mat[te][i]);
ans *= -1;
}
ans *= Mat[ce][cv];
for (j = cv + 1; j <= nv; ++j)
Mat[ce][j] /= Mat[ce][cv];
for (i = ce + 1; i <= ne; ++i)
for (j = cv + 1; j <= nv; ++j)
Mat[i][j] -= Mat[i][cv] * Mat[ce][j];
}
return ans;
}
int main(void)
{
int T;
int n, m, u, v, i;
scanf("%d", &T);
while (T--)
{
init();
scanf("%d%d", &n, &m);
for (i = 0; i < m; ++i)
{
scanf("%d%d", &u, &v);
++Mat[u][u];
++Mat[v][v];
Mat[u][v] = -1;
Mat[v][u] = -1;
}
debug(n,n);
printf("%.0f\n", Gaussian(n - 1, n - 1));
}
return 0;
}

SPOJ HIGH(生成树计数,高斯消元求行列式)的更多相关文章

  1. HDU4870_Rating_双号从零单排_高斯消元求期望

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4870 原题: Rating Time Limit: 10000/5000 MS (Java/Other ...

  2. HDU 5833 (2016大学生网络预选赛) Zhu and 772002(高斯消元求齐次方程的秩)

    网络预选赛的题目……比赛的时候没有做上,确实是没啥思路,只知道肯定是整数分解,然后乘起来素数的幂肯定是偶数,然后就不知道该怎么办了… 最后题目要求输出方案数,首先根据题目应该能写出如下齐次方程(从别人 ...

  3. 【BZOJ2137】submultiple 高斯消元求伯努利数

    [BZOJ2137]submultiple Description 设函数g(N)表示N的约数个数.现在给出一个数M,求出所有M的约数x的g(x)的K次方和. Input 第一行输入N,K.N表示M由 ...

  4. 【bzoj2115】[Wc2011] Xor DFS树+高斯消元求线性基

    题目描述 输入 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M 行描述 M 条边,每行三个整数Si,Ti ,Di,表示 Si 与Ti之间存在 一条权值为 Di的无向边. 图 ...

  5. 【bzoj3105】[cqoi2013]新Nim游戏 高斯消元求线性基

    题目描述 传统的Nim游戏是这样的:有一些火柴堆,每堆都有若干根火柴(不同堆的火柴数量可以不同).两个游戏者轮流操作,每次可以选一个火柴堆拿走若干根火柴.可以只拿一根,也可以拿走整堆火柴,但不能同时从 ...

  6. 【bzoj4004】[JLOI2015]装备购买 贪心+高斯消元求线性基

    题目描述 脸哥最近在玩一款神奇的游戏,这个游戏里有 n 件装备,每件装备有 m 个属性,用向量zi(aj ,.....,am) 表示 (1 <= i <= n; 1 <= j < ...

  7. 【bzoj4269】再见Xor 高斯消元求线性基

    题目描述 给定N个数,你可以在这些数中任意选一些数出来,每个数可以选任意多次,试求出你能选出的数的异或和的最大值和严格次大值. 输入 第一行一个正整数N. 接下来一行N个非负整数. 输出 一行,包含两 ...

  8. hdu 4870 rating(高斯消元求期望)

    Rating Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  9. HDU3949/AcWing210 XOR (高斯消元求线性基)

    求第k小的异或和,用高斯消元求更简单一些. 1 //用高斯消元求线性基 2 #include<bits/stdc++.h> 3 using namespace std; 4 #define ...

随机推荐

  1. Vue插槽 slot

    1. 什么是插槽 插槽slot 是往父组件中插入额外内容,实现组件的复用,一个插槽插入到一个对应的标签中 2. 实例: 一个组件中不允许有两个匿名插槽 </head> <body&g ...

  2. 操作BOM

    BOM的作用是将相关的元素组织包装起来,提供给程序设计人员使用,从而降低开发人员的劳动量,提高设计Web页面的能力. 整个window对象是整个BOM的核心. 通过BOM可实现的功能: 弹出新的浏览器 ...

  3. Tornado学习

    为什么用Tornado? 异步编程原理 服务器同时要对许多客户端提供服务,他的性能至关重要.而服务器端的处理流程,只要遇到了I/O操作,往往需要长时间的等待.   屏幕快照 2018-10-31 上午 ...

  4. ajax提交时 富文本CKEDITOR 获取不到内容

    ckeditor数据向content(页面用以替换的编辑框)的同步 问题: 我们发现,在数据通过ajaxSubmit提交的过程中,并不能将最新的数据进行提交.换句话说,最新的数据无法被jQuery.f ...

  5. 让UltraEdit-32成为Delphi 7编译器的工具设置

    UltraEdit-32编译Delphi的工具设置 {================================================}Dcc32 命令行(&C):C:\Pro ...

  6. hadoop生态搭建(3节点)-06.hbase配置

    # http://archive.apache.org/dist/hbase/1.2.4/ # ==================================================== ...

  7. 日志管理——rsyslog

    官方文档(必看) http://www.rsyslog.com/doc/v8-stable/ 简介 rsyslog是linux自带日志管理工具,分为客户端\服务端,包含日志收集\过滤\分析\转储. 数 ...

  8. 2.从print到自省

    print是一个函数   为什么print是一个函数呢?可以在交互式解释器下 输入: >>> type(print) 输出: <class 'builtin_function_ ...

  9. loj136 (最小瓶颈路,多次询问)

    题目描述 给定一个包含 n nn 个节点和 m mm 条边的图,每条边有一个权值.你的任务是回答 k kk 个询问,每个询问包含两个正整数 s ss 和 t tt 表示起点和终点,要求寻找从 s ss ...

  10. git 本地分支与远程分支 新建/删除/合并

    github上已经有master分支 和dev分支 在本地 git checkout -b dev 新建并切换到本地dev分支 git pull origin dev 本地分支与远程分支相关联 在本地 ...