SPOJ 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
图论 生成树 Matrix-Tree定理
按照给出的边关系建立邻接矩阵和度数矩阵,直接套用矩阵树定理。
给出的图可能不连通……无解时候要输出0
↑天真的我压根儿没想到无解的情况,WA了一串
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cmath>
using namespace std;
const double eps=1e-;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*-''+ch;ch=getchar();}
return x*f;
}
double G[mxn][mxn],A[mxn][mxn];
int n,m;
void Solve(){
int i,j;
double ans=;
for(i=;i<n;i++){
int p=i;
if(fabs(G[i][i])<eps){
for(j=i+;j<n;j++){
if(fabs(G[j][i])>eps)p=j;
}
if(p==i){
printf("0\n");return;
}
for(j=;j<n;j++)swap(G[i][j],G[p][j]);
ans=-ans;
}
for(j=i+;j<n;j++){
double c=G[j][i]/G[i][i];
for(int k=i;k<n;k++){
G[j][k]-=c*G[i][k];
}
}
ans*=G[i][i];
}
printf("%.0f\n",ans);
return;
}
int main(){
int i,j,u,v;
int T;
scanf("%d",&T);
while(T--){
memset(G,,sizeof G);
memset(A,,sizeof A);
scanf("%d%d",&n,&m);
for(i=;i<=m;i++){
scanf("%d%d",&u,&v);
++G[u][u];
++G[v][v];
++A[u][v];++A[v][u];
}
for(i=;i<=n;i++)
for(j=;j<=n;j++){
G[i][j]-=A[i][j];
}
Solve();
}
return ;
}
SPOJ HIGH Highways的更多相关文章
- 【SPOJ】Highways(矩阵树定理)
[SPOJ]Highways(矩阵树定理) 题面 Vjudge 洛谷 题解 矩阵树定理模板题 无向图的矩阵树定理: 对于一条边\((u,v)\),给邻接矩阵上\(G[u][v],G[v][u]\)加一 ...
- spoj 104 Highways(Matrix-tree定理)
spoj 104 Highways 生成树计数,matrix-tree定理的应用. Matrix-tree定理: D为无向图G的度数矩阵(D[i][i]是i的度数,其他的为0),A为G的邻接矩阵(若u ...
- 生成树的计数(基尔霍夫矩阵):UVAoj 10766 Organising the Organisation SPOJ HIGH - Highways
HIGH - Highways In some countries building highways takes a lot of time... Maybe that's because th ...
- spoj 104 Highways (最小生成树计数)
题目链接:http://www.spoj.pl/problems/HIGH/ 题意:求最小生成树个数. #include<algorithm> #include<cstdio> ...
- SPOJ HIGH Highways ——Matrix-Tree定理 高斯消元
[题目分析] Matrix-Tree定理+高斯消元 求矩阵行列式的值,就可以得到生成树的个数. 至于证明,可以去看Vflea King(炸树狂魔)的博客 [代码] #include <cmath ...
- SPOJ.104.Highways([模板]Matrix Tree定理 生成树计数)
题目链接 \(Description\) 一个国家有1~n座城市,其中一些城市之间可以修建高速公路(无自环和重边). 求有多少种方案,选择修建一些高速公路,组成一个交通网络,使得任意两座城市之间恰好只 ...
- SPOJ - HIGH Highways(矩阵树定理)
https://vjudge.net/problem/SPOJ-HIGH 题意: 给n个点m条边,求生成树个数. 思路: 矩阵树裸题. 具体的话可以看一下周冬的论文<生成树的计数及其应用> ...
- [spoj] HIGH - Highways (生成树计数)
传送门 输入格式: 第一行一个整数T,表示测试数据的个数 每个测试数据第一行给出 n,m 分别表示点数与边数 接下来 m 行,每行给出两个数 a,b ,表示 a,b 之间有一条无向边 输出格式: 每个 ...
- 基尔霍夫矩阵题目泛做(AD第二轮)
题目1: SPOJ 2832 题目大意: 求一个矩阵行列式模一个数P后的值.p不一定是质数. 算法讨论: 因为有除法而且p不一定是质数,不一定有逆元,所以我们用辗转相除法. #include < ...
随机推荐
- JSON.parse(text[, reviver])
1. JSON.parse(text[, reviver])text 必需 有效的json字符串reviver 可选 函数 2. 举栗子1) 只有第一个参数 let objStr = '{" ...
- 该网页无法正常运作 目前无法处理此请求HTTP ERROR 500?
由于php.ini配置文件中错误显示关闭导致. 将下值由Off 变更为 On display_errors = On display_startup_errors = On
- session在C#一般处理程序的调用方式
在C#中有一个一般处理程序,可以快速地进行一些逻辑运算等功能,但在这个页面上,不能直接选择使用session进行页面间的值的传递,如何使得页面可以使用session呢 在页面开头写上 using Sy ...
- git线上线下冲突
今天用git pull来更新代码,遇到了下面的问题: error: Your local changes to the following files would be overwritten by ...
- 第一篇:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc3 in position 0: invalid continuation byte
需求:python如何实现普通用户登录服务器后切换到root用户再执行命令 解决参考: 代码: def verification_ssh(host,username,password,port,roo ...
- DevOps - 配置管理 - Chef
#!/bin/sh # WARNING: REQUIRES /bin/sh # # - must run on /bin/sh on solaris 9 # - must run on /bin/sh ...
- php - 去除php代码中的多余空格
<?php class Test{ public function test(){ $tmplContent = file_get_contents('./test.php'); $tmplCo ...
- tp3.2框架中使用volist输出混乱的一点发现
在tp框架中,volist真的是一样很好用的东西,但是要是不注意,用起来也会有问题的 在Controller层中,将数据assign到页面 $this->assign('vo',$news); ...
- B1023 组个最小数 (20分)
B1023 组个最小数 (20分) 给定数字 0-9各若干个.你可以以任意顺序排列这些数字,但必须全部使用.目标是使得最后得到的数尽可能小(注意 0 不能做首位).例如:给定两个 0,两个 1,三个 ...
- 洛谷 P2205 [USACO13JAN]画栅栏
这题其实没什么,但用到的算法都十分有用.做一个不恰当的比喻,这是一只必须用牛刀杀的鸡,但因为我这个蒟蒻杀不死牛,所以只能找只鸡来练练手. 题目描述 Farmer John 想出了一个给牛棚旁的长围墙涂 ...