SPOJ HIGH(生成树计数,高斯消元求行列式)
HIGH - Highways
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(生成树计数,高斯消元求行列式)的更多相关文章
- HDU4870_Rating_双号从零单排_高斯消元求期望
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4870 原题: Rating Time Limit: 10000/5000 MS (Java/Other ...
- HDU 5833 (2016大学生网络预选赛) Zhu and 772002(高斯消元求齐次方程的秩)
网络预选赛的题目……比赛的时候没有做上,确实是没啥思路,只知道肯定是整数分解,然后乘起来素数的幂肯定是偶数,然后就不知道该怎么办了… 最后题目要求输出方案数,首先根据题目应该能写出如下齐次方程(从别人 ...
- 【BZOJ2137】submultiple 高斯消元求伯努利数
[BZOJ2137]submultiple Description 设函数g(N)表示N的约数个数.现在给出一个数M,求出所有M的约数x的g(x)的K次方和. Input 第一行输入N,K.N表示M由 ...
- 【bzoj2115】[Wc2011] Xor DFS树+高斯消元求线性基
题目描述 输入 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M 行描述 M 条边,每行三个整数Si,Ti ,Di,表示 Si 与Ti之间存在 一条权值为 Di的无向边. 图 ...
- 【bzoj3105】[cqoi2013]新Nim游戏 高斯消元求线性基
题目描述 传统的Nim游戏是这样的:有一些火柴堆,每堆都有若干根火柴(不同堆的火柴数量可以不同).两个游戏者轮流操作,每次可以选一个火柴堆拿走若干根火柴.可以只拿一根,也可以拿走整堆火柴,但不能同时从 ...
- 【bzoj4004】[JLOI2015]装备购买 贪心+高斯消元求线性基
题目描述 脸哥最近在玩一款神奇的游戏,这个游戏里有 n 件装备,每件装备有 m 个属性,用向量zi(aj ,.....,am) 表示 (1 <= i <= n; 1 <= j < ...
- 【bzoj4269】再见Xor 高斯消元求线性基
题目描述 给定N个数,你可以在这些数中任意选一些数出来,每个数可以选任意多次,试求出你能选出的数的异或和的最大值和严格次大值. 输入 第一行一个正整数N. 接下来一行N个非负整数. 输出 一行,包含两 ...
- hdu 4870 rating(高斯消元求期望)
Rating Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- HDU3949/AcWing210 XOR (高斯消元求线性基)
求第k小的异或和,用高斯消元求更简单一些. 1 //用高斯消元求线性基 2 #include<bits/stdc++.h> 3 using namespace std; 4 #define ...
随机推荐
- Linux系统定时任务crond那些事
1 Linux系统定时任务 1.1 定时任务介绍 1.1.1 Crond是什么? Crond是linux系统中用来定期执行命令或指定程序任务的一种服务或软件.Centos5/ linux系统安装完操作 ...
- Hue联合(hdfs yarn hive) 后续......................
1.启动hdfs,yarn start-all.sh 2.启动hive $ bin/hive $ bin/hive --service metastore & $ bin/hive --ser ...
- CentOS6.9重新安装python2.6.6和yum
CentOS6.9重新安装python2.6.6和yum 本文转载自昔日暖阳,原文地址:http://www.osheep.cn/4801.html 最近为了部署一个Python应用到腾讯云服务器,强 ...
- Python正则表达式-基础
Python正则表达式-基础 本文转载自昔日暖阳,原文地址:http://www.osheep.cn/4806.html python使用正则,需要先引入re模块 import re 匹配符 单个字符 ...
- C++远征之封装篇(下)-学习笔记
C++远征之封装篇(下) c++封装概述 下半篇依然围绕类 & 对象进行展开 将原本学过的简单元素融合成复杂的新知识点. 对象 + 数据成员 = 对象成员(对象作为数据成员) 对象 + 数组 ...
- Kubernetes-ELK
ElasticSearch日志搜集查询和展现案例 容器中输出到控制台的日志都会以*-json.log的命名方式存储在/var/lib/container目录之下: Kubernetes采用Fluent ...
- Android Studio modify language level to Java 8
If you need use lambda, should modify language level File -> Project Structure -> app -> Pr ...
- LeetCode:15. 3Sum(Medium)
1. 原题链接 https://leetcode.com/problems/3sum/description/ 2. 题目要求 数组S = nums[n]包含n个整数,请问S中是否存在a,b,c三个整 ...
- 阿牛的EOF牛肉串(递推)
阿牛的EOF牛肉串 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Sub ...
- Python 常见的错误类型和继承关系
Python所有的错误都是从BaseException类派生 BaseException +-- SystemExit +-- KeyboardInterrupt +-- GeneratorExit ...