SPOJ - HIGH :Highways (生成树计数)
Highways
题目链接:https://vjudge.net/problem/SPOJ-HIGH
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
题意:
给出n个点,以及m条无向边,问生成树的个数。
题解:
生成树计数模板题。。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = ;
int t;
int n,m;
ll b[N][N];
int g[N][N];
ll Det(int n){
int i,j,k;
ll ret = ;
for(i=;i<=n;i++){
for(j = i+;j <= n;j++){
while(b[j][i]){
ll tmp=b[i][i]/b[j][i];//不存在除不尽的情况
for(k = i;k <= n;k++)
b[i][k] -= tmp*b[j][k];
for(k=i;k<=n;k++)
swap(b[i][k],b[j][k]);
ret = -ret;
}
}
if(!b[i][i]) return ;
ret *= b[i][i];
}
if(ret < ) ret = -ret;
return ret;
}
int main(){
cin>>t;
while(t--){
scanf("%d%d",&n,&m);
memset(b,,sizeof(b));
memset(g,,sizeof(g));
for(int i=;i<=m;i++){
int u,v;
scanf("%d%d",&u,&v);
g[u][v]=g[v][u]=;
}
for(int i=;i<=n;i++){
for(int j=i;j<=n;j++){
if(g[i][j]){
b[i][i]++;b[j][j]++;
b[i][j]=b[j][i]=-;
}
}
}
cout<<Det(n)<<endl;
}
return ;
}
SPOJ - HIGH :Highways (生成树计数)的更多相关文章
- [spoj] HIGH - Highways (生成树计数)
传送门 输入格式: 第一行一个整数T,表示测试数据的个数 每个测试数据第一行给出 n,m 分别表示点数与边数 接下来 m 行,每行给出两个数 a,b ,表示 a,b 之间有一条无向边 输出格式: 每个 ...
- SPOJ 104 HIGH - Highways 生成树计数
题目链接:https://vjudge.net/problem/SPOJ-HIGH 解法: 生成树计数 1.构造 基尔霍夫矩阵(又叫拉普拉斯矩阵) n阶矩阵 若u.v之间有边相连 C[u][v]=C[ ...
- [spoj104][Highways] (生成树计数+矩阵树定理+高斯消元)
In some countries building highways takes a lot of time... Maybe that's because there are many possi ...
- spoj104 highways 生成树计数(矩阵树定理)
https://blog.csdn.net/zhaoruixiang1111/article/details/79185927 为了学一个矩阵树定理 从行列式开始学(就当提前学线代了.. 论文生成树的 ...
- spoj 104 Highways (最小生成树计数)
题目链接:http://www.spoj.pl/problems/HIGH/ 题意:求最小生成树个数. #include<algorithm> #include<cstdio> ...
- spoj 104 Highways(Matrix-tree定理)
spoj 104 Highways 生成树计数,matrix-tree定理的应用. Matrix-tree定理: D为无向图G的度数矩阵(D[i][i]是i的度数,其他的为0),A为G的邻接矩阵(若u ...
- SPOJ.104.Highways([模板]Matrix Tree定理 生成树计数)
题目链接 \(Description\) 一个国家有1~n座城市,其中一些城市之间可以修建高速公路(无自环和重边). 求有多少种方案,选择修建一些高速公路,组成一个交通网络,使得任意两座城市之间恰好只 ...
- kuangbin带你飞 生成树专题 : 次小生成树; 最小树形图;生成树计数
第一个部分 前4题 次小生成树 算法:首先如果生成了最小生成树,那么这些树上的所有的边都进行标记.标记为树边. 接下来进行枚举,枚举任意一条不在MST上的边,如果加入这条边,那么肯定会在这棵树上形成一 ...
- 生成树计数 Matrix-Tree 定理 学习笔记
一直都知道要用Matrix-Tree定理来解决生成树计数问题,但是拖到今天才来学.博主数学不好也只能跟着各位大佬博客学一下它的应用以及会做题,证明实在是不会. 推荐博客: https://www.cn ...
随机推荐
- TW实习日记:第17天
今天又改了一堆bug,比如界面的显示bug,Html字符串的处理.优化了一些交互界面,处理了一个模块数据传输的问题.我发现这些bug真的有一半是粗心导致的,真的真的是,写代码一定要细心细心再细心,不然 ...
- vista x64 vs2010 win32添加资源 未能完成操作解决办法
非常痛苦的感觉,不能用vc6,msdn library也不好用,去2k3系统试了下,没有任何问题,无奈想重装系统了,但是太浪费时间,装了虚拟机也是vistax64的,安装之后正常... 卸载重新安装依 ...
- 剑指offer-从上往下打印二叉树22
题目描述 从上往下打印出二叉树的每个节点,同层节点从左至右打印. class Solution: # 返回从上到下每个节点值列表,例:[1,2,3] def PrintFromTopToBottom( ...
- UVa -1584 Circular Sequence 解题报告 - C语言
1.题目大意 输入长度为n$(2\le n\le 100)$的环状DNA串,找出该DNA串字典序最小的最小表示. 2.思路 这题特别简单,一一对比不同位置开始的字符串的字典序,更新result. 3. ...
- 【转载】android 常用开源框架
对于Android初学者以及对于我们菜鸟,这些大神们开发的轻量级框架非常有用(更别说开源的了). 下面转载这10个框架的介绍:(按顺序来吧没有什么排名). 一. Afinal 官方介绍: Afina ...
- HADOOP docker(九):hdfs权限
1. 概述2. 用户身份标识3. 组映射4.关于权限的实现5.文件系统API的变更6.应用程序shell的变更7.超级用户8.ACLs9.ACL 文件系统API10.ACL命令11.参数配置12.总结 ...
- C语言文件进阶操作
Description文件a.dic.b.dic.c.dic中分别存有张三的三科成绩,每个文件都是16字节:前8个字节存储其英文名字zhangsan,后面是一个空格,其后的2个字节存储其年龄(文本方式 ...
- <Effective C++>读书摘要--Templates and Generic Programming<一>
1.The initial motivation for C++ templates was straightforward: to make it possible to create type-s ...
- 使用Quartz.Net同时执行多个任务
在Quartz.Net中可能我们需要在某一时刻执行多个任务操作,而又不想创建多个任务.Quartz.Net为我们提供了多个ScheduleJob的重载来实现多个一次执行多个任务. // 创建一个组任务 ...
- html .net 网页,网站标题添图标
<link rel="icon" href="../favicon.ico" type="image/x-icon" /> &l ...