【SPOJ 104】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.
输入输出格式
输入格式:
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.
输出格式:
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.
输入输出样例
输入样例#1:
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
输出样例#1:
8
1
1
3
先用高斯消元转化为上三角,后求对角线之积算出行列式绝对值
code:
//Menteur_Hxy
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
#define ll long long
ll rd() {
ll x=0,fla=1; char c=' ';
while(c<'0' || c>'9') {c=getchar();if(c=='-') fla=-fla;}
while(c>='0' && c<='9') x=x*10+c-'0',c=getchar();
return x*fla;
}
const double eps=1e-12;
int T,n,m;
double a[3010][3010];
void gauss() {
for(int i=1;i<=n;i++) {
int mx=i;
for(int j=i+1;j<=n;j++) mx=(a[mx][i]-a[j][i]<-eps)?i:mx;
if(mx!=i) swap(a[i],a[mx]);
if(!a[i][i]) {printf("0\n");return ;}
for(int j=i+1;j<=n;j++) {
double t=a[j][i]/a[i][i];
for(int k=i;k<=n+1;k++)
a[j][k]-=t*a[i][k];
}
}
double ans=1;
for(int i=1;i<=n;i++) ans=ans*a[i][i];
printf("%.0f\n",fabs(ans));
}
int main() {
T=rd();
while(T--) {
memset(a,0,sizeof a);
n=rd()-1,m=rd();
for(int i=1;i<=m;i++) {
int u=rd(),v=rd();
a[u][u]++,a[v][v]++;
a[u][v]--,a[v][u]--;
}
gauss();
}
return 0;
}
【SPOJ 104】HIGH - Highways (高斯消元)的更多相关文章
- SPOJ HIGH Highways ——Matrix-Tree定理 高斯消元
[题目分析] Matrix-Tree定理+高斯消元 求矩阵行列式的值,就可以得到生成树的个数. 至于证明,可以去看Vflea King(炸树狂魔)的博客 [代码] #include <cmath ...
- SPOJ HIGH(生成树计数,高斯消元求行列式)
HIGH - Highways no tags In some countries building highways takes a lot of time... Maybe that's bec ...
- [spoj104][Highways] (生成树计数+矩阵树定理+高斯消元)
In some countries building highways takes a lot of time... Maybe that's because there are many possi ...
- 【BZOJ-3143】游走 高斯消元 + 概率期望
3143: [Hnoi2013]游走 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2264 Solved: 987[Submit][Status] ...
- 【BZOJ-3270】博物馆 高斯消元 + 概率期望
3270: 博物馆 Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 292 Solved: 158[Submit][Status][Discuss] ...
- *POJ 1222 高斯消元
EXTENDED LIGHTS OUT Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9612 Accepted: 62 ...
- [bzoj1013][JSOI2008][球形空间产生器sphere] (高斯消元)
Description 有一个球形空间产生器能够在n维空间中产生一个坚硬的球体.现在,你被困在了这个n维球体中,你只知道球 面上n+1个点的坐标,你需要以最快的速度确定这个n维球体的球心坐标,以便于摧 ...
- hihoCoder 1196 高斯消元·二
Description 一个黑白网格,点一次会改变这个以及与其连通的其他方格的颜色,求最少点击次数使得所有全部变成黑色. Sol 高斯消元解异或方程组. 先建立一个方程组. \(x_i\) 表示这个点 ...
- BZOJ 2844 albus就是要第一个出场 ——高斯消元 线性基
[题目分析] 高斯消元求线性基. 题目本身不难,但是两种维护线性基的方法引起了我的思考. void gauss(){ k=n; F(i,1,n){ F(j,i+1,n) if (a[j]>a[i ...
随机推荐
- AIX下sort命令简介及使用
AIX下sort命令简介及使用 sort -rn +3 , r倒排序,n按照数字排序: +3按照第四列排序: 第一列是+0: 学习:http://blog.csdn.net/chen_linbo/a ...
- Maven学习--------基础2
继续学习Maven. 4.2 变量 1) 问:怎样使用变量替换?项目中的某个配置文件比方jdbc.properties使用了一些pom中的变量,怎样在公布中使用包括真实内容的终于结果文件? 答:使用资 ...
- Mina airQQ聊天开门见山篇(一)
Mina airQQ聊天开门见山篇(一) 近期项目可能要用到Mina,这个礼拜就在看这个框架,所以想写个小小的聊天的demo来巩固下,打算用几篇博客来记录下相关的知识 client用的是Flex Ai ...
- sql server中的悲观锁和乐观锁
https://www.cnblogs.com/chenwolong/p/Lock.html https://www.cnblogs.com/dengshaojun/p/3955826.html ht ...
- 解决生成主键 id重复的解决办法
作者:董春秋链接:https://www.zhihu.com/question/30674667/answer/49082988来源:知乎著作权归作者所有,转载请联系作者获得授权. 全局id生成器.我 ...
- 文档控件NTKO OFFICE 详细使用说明之预览Excel文件(查看、编辑、保存回服务器)
1.在线预览Excel文件 (1) 运行环境 ① 浏览器:支持IE7-IE11(平台版本还支持Chrome和Firefox) ② IE工具栏-Internet 选项:将www.ntko.com加入到浏 ...
- webpack打包css自动添加css3前缀
为了浏览器的兼容性,有时候我们必须加入-webkit,-ms,-o,-moz这些前缀.目的就是让我们写的页面在每个浏览器中都可以顺利运行. 1.安装 cnpm i postcss-loader aut ...
- 登录linux,输入ls显示anaconda-ks.cfg cobbler.ks ....., 原因在于root@ ~ / 区别
今天登录linux测试机,想要创建目录,ls的时候,找不到之前的的目录,才发现是目录不对的问题. 首先,先要弄清楚 [root@330c353813ea ~] 和 [root@330c353813ea ...
- Charles设置抓取https请求
1.在手机上设置HTTP代理,将手机与电脑连接. 打开手机设置-WLAN,设置代理 2.(iPhone)使用iPhone自带的浏览器打开Safari,并输入地址:http://charlesproxy ...
- Android拍照,录制视频,相机简单功能实现
1.效果图,功能没有录制出来. 基本实现了拍照,录制视频,前后摄像头切换的功能,可以转屏,聚焦. 代码在最下面,可以看代码,运行apk看实际效果. 2.权限不能忘 <uses-permissio ...