本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。

本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!

题目链接:SPOJ104

正解:矩阵树定理+高斯消元

解题报告:

  写的第一发$Matrix-Tree$定理题QAQ

  这道题要求一张无向图的生成树个数,既然是模板题,窝直接上结论辣:构造一个新的矩阵:度数矩阵-邻接矩阵,

  对这个新的矩阵去掉第$r$行、第$r$列之后得到的矩阵,高斯消元,消成上三角的形式(也就是左下角都是$0$),然后主对角线上的数的乘积就是答案了…

  两点注意事项:

  1、最后答案要取绝对值;

  2、发现$a[i][i]=0$时会出现$/0$这种尴尬事情,同时这也意味着我可以直接$return 0$了,因为主对角线上已经出现了$0$。

//It is made by ljh2000
//有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <complex>
#include <bitset>
using namespace std;
typedef long long LL;
typedef long double LB;
typedef complex<double> C;
const double pi = acos(-1);
const int MAXN = 12;
int n,m;
LB a[MAXN][MAXN],ans; inline int getint(){
int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
} inline void Gauss(int n){
for(int i=0;i<n;i++) {
for(int j=i+1;j<n;j++) {
if(a[i][i]==0) { ans=0; return ; }//!!!
LB t=a[j][i]/a[i][i];
for(int k=i;k<n;k++) {
a[j][k]-=a[i][k]*t;
}
}
} for(int i=0;i<n;i++)
ans*=a[i][i];
} inline void work(){
int T=getint(); int x,y;
while(T--) {
n=getint(); m=getint(); ans=1;
memset(a,0,sizeof(a));
for(int i=1;i<=m;i++) {
x=getint(); y=getint(); x--; y--;
a[x][y]--; a[y][x]--;
a[x][x]++; a[y][y]++;
}
Gauss(n-1);
ans=fabs(ans);//!!!
printf("%.0Lf\n",ans);
}
} int main()
{
work();
return 0;
}
//有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。

  

SPOJ104 HIGH - Highways的更多相关文章

  1. spoj104 HIGH - Highways 矩阵树定理

    欲学矩阵树定理必先自宫学习一些行列式的姿势 然后做一道例题 #include <iostream> #include <cstring> #include <cstdio ...

  2. SPOJ104 Highways,跨越数

    高速公路(SPOJ104 Highways) 一个有n座城市的组成国家,城市1至n编号,当中一些城市之间能够修建快速公路.如今,须要有选择的修建一些快速公路.从而组成一个交通网络.你的任务是计算有多少 ...

  3. SPOJ104 Highways 【矩阵树定理】

    SPOJ104 Highways Description In some countries building highways takes a lot of time- Maybe that's b ...

  4. [spoj104][Highways] (生成树计数+矩阵树定理+高斯消元)

    In some countries building highways takes a lot of time... Maybe that's because there are many possi ...

  5. spoj104 highways 生成树计数(矩阵树定理)

    https://blog.csdn.net/zhaoruixiang1111/article/details/79185927 为了学一个矩阵树定理 从行列式开始学(就当提前学线代了.. 论文生成树的 ...

  6. H:Highways

    总时间限制: 1000ms 内存限制: 65536kB描述The island nation of Flatopia is perfectly flat. Unfortunately, Flatopi ...

  7. Highways(prim & MST)

    Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23421   Accepted: 10826 Descri ...

  8. poj2485 Highways

    Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...

  9. poj 2485 Highways 最小生成树

    点击打开链接 Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19004   Accepted: 8815 ...

随机推荐

  1. linux系统centOS在虚拟机下的自定义安装

    一  前戏 1.1在官网找到合适的版本,镜像文件 1.2安装VMware FF590-2DX83-M81LZ-XDM7E-MKUT4 CG54H-D8D0H-H8DHY-C6X7X-N2KG6 ZC3 ...

  2. Object-Detection中常用的概念解析

    常用的Region Proposal Selective Search Edge Boxes Softmax-loss softmax-loss层和softmax层计算大致是相同的,softmax是一 ...

  3. 生信-cufflinks输入输出文件分析

    转自:https://wenku.baidu.com/view/8d6a95d20d22590102020740be1e650e52eacf2a.html 输出包含3个文件:转录组的组装.gtf    ...

  4. Spring框架第五篇之Spring与AOP

    一.AOP概述 AOP(Aspect Orient Programming),面向切面编程,是面向对象编程OOP的一种补充.面向对象编程是从静态角度考虑程序的结构,而面向切面编程是从动态角度考虑程序运 ...

  5. Java队列存储结构及实现

    一.队列(Queue) 队列是一种特殊的线性表,它只允许在表的前段(front)进行删除操作,只允许在表的后端(rear)进行插入操作.进行插入操作的端称为队尾,进行删除操作的端称为队头. 对于一个队 ...

  6. 剑指offer-java

    面试题67 机器人的运动范围 题意: 地上有一个m行和n列的方格.一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子. ...

  7. HTML5游戏开发系列教程6(译)

    原文地址:http://www.script-tutorials.com/html5-game-development-lesson-6/ 这是我们最新一篇HTML5游戏开发系列文章.我们将继续使用c ...

  8. android 显示internet 图片

    try { HttpGet httpRequest = new HttpGet(edtUrl.getText() .toString()); HttpClient httpclient = new D ...

  9. Codeforces Round #425 (Div. 2) B - Petya and Exam

    地址:http://codeforces.com/contest/832/problem/B 题目: B. Petya and Exam time limit per test 2 seconds m ...

  10. CDH5离线安装简记

    需要的介质如下:CM: cloudera-manager-el6-cm5.4.3_x86_64.tar.gzCDH parcel: CDH-5.4.0-1.cdh5.4.0.p0.27-el6.par ...