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 < ...
随机推荐
- webSocket使用心跳包实现断线重连
首先new一个webscoket的连接 let noticeSocketLink = new WebSocket(‘webSocket的地址’) 这里是连接成功之后的操作 linkNoticeWebs ...
- 数据库sql语句的exists和in的区别
性能变化的关键: #1 执行的先后顺序 谁是驱动表,谁先执行查询,谁后执行查询 #2 执行过程 exists的优点是:只要存在就返回了,这样的话很有可能不需要扫描整个表. in需要扫描完整个表,并 ...
- gitlab文件夹的权限不要随便给777
gitlab 权限给到777 不一定有用
- Python 正则表达式 贪心匹配和非贪心匹配
Python的正则表达式默认是“贪心匹配”,即在有第二义的情况下,尽可能匹配最长的字符串,在正则表达式的花括号后面跟上问号,可以变为非贪心模式 >>> >>> ha ...
- 记忆化搜索:HDU1078-FatMouse and Cheese(记忆化搜索)
FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- spark 的createDstream和createDirectStream区别
spark读取kafka数据流提供了两种方式createDstream和createDirectStream. 两者区别如下: 1.KafkaUtils.createDstream 构造函数为Kafk ...
- HBase(0.94.5)的Compact和Split源码分析
经过对比,0.94.5以后版本主要过程基本类似(有些新功能和细节增加) 一. Compact 2.1. Compact主要来源 来自四个方面:1.Memstoreflush时:2.HR ...
- 1010: [HNOI2008]玩具装箱toy(斜率优化)
1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 12280 Solved: 5277[Submit][S ...
- P2590 [ZJOI2008]树的统计(LCT)
P2590 [ZJOI2008]树的统计 题目描述 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w. 我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把 ...
- c++实验5
设计并实现一个机器宠物类MachinePets #include <iostream> #include <string> using namespace std; class ...