https://vjudge.net/problem/SPOJ-HIGH

题意:

给n个点m条边,求生成树个数。

思路:

矩阵树裸题。

具体的话可以看一下周冬的论文《生成树的计数及其应用》。

简单说一下,$A[ ][ ]$为邻接矩阵,有边为1(其实也就是边的个数,有重边时要注意),无边为0。$D[ ][ ]$为度数矩阵,$i=j$时为1,否则为0。

$C[ ][ ]$为关联矩阵,$C[ ][ ]=D[ ][ ]-A[ ][ ]$。

最后解任意n-1阶的主子式即可。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,ll> pll;
const int INF = 0x3f3f3f3f;
const int maxn=+; int n,m;
double C[maxn][maxn];
int A[maxn][maxn],D[maxn][maxn]; double Gauss()
{
for(int k=; k<=n; k++) //k表示当前行数,因为行数与列数一样,所以这里k也代表了列数
{
int max_r=k;
for(int i=k+;i<=n;i++)
if(fabs(C[i][k])>fabs(C[max_r][k])) max_r=i;
if(C[max_r][k]==) return ; //有一列为0,行列式的值必为0
if(max_r!=k)
{
for(int j=k;j<=n;j++)
swap(C[k][j],C[max_r][j]);
}
for(int i=k+;i<=n;i++)
{
double tmp=C[i][k]/C[k][k];
for(int j=k;j<=n;j++)
C[i][j]-=tmp*C[k][j];
}
}
double ans=;
for(int i=;i<=n;i++) ans*=C[i][i]; //化为三角阵后计算主对角线元素乘积
ans=fabs(ans);
return ans;
} int main()
{
//freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
memset(A,,sizeof(A));
memset(D,,sizeof(D));
scanf("%d%d",&n,&m);
for(int i=;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
D[u][u]++; D[v][v]++;
A[u][v]++; A[v][u]++;
}
n--;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
C[i][j]=D[i][j]-A[i][j];
printf("%.0lf\n",Gauss());
}
return ;
}

SPOJ - HIGH Highways(矩阵树定理)的更多相关文章

  1. spoj104 HIGH - Highways 矩阵树定理

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

  2. SPOJ Highways [矩阵树定理]

    裸题 注意: 1.消元时判断系数为0,退出 2.最后乘ans要用double.... #include <iostream> #include <cstdio> #includ ...

  3. 【SPOJ】Highways(矩阵树定理)

    [SPOJ]Highways(矩阵树定理) 题面 Vjudge 洛谷 题解 矩阵树定理模板题 无向图的矩阵树定理: 对于一条边\((u,v)\),给邻接矩阵上\(G[u][v],G[v][u]\)加一 ...

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

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

  5. SPOJ104 Highways 【矩阵树定理】

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

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

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

  7. 算法复习——矩阵树定理(spoj104)

    题目: In some countries building highways takes a lot of time... Maybe that's because there are many p ...

  8. BZOJ 4766: 文艺计算姬 [矩阵树定理 快速乘]

    传送门 题意: 给定一个一边点数为n,另一边点数为m,共有n*m条边的带标号完全二分图$K_{n,m}$ 求生成树个数 1 <= n,m,p <= 10^18 显然不能暴力上矩阵树定理 看 ...

  9. bzoj 4596 [Shoi2016]黑暗前的幻想乡 矩阵树定理+容斥

    4596: [Shoi2016]黑暗前的幻想乡 Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 559  Solved: 325[Submit][Sta ...

  10. 【LOJ#6072】苹果树(矩阵树定理,折半搜索,容斥)

    [LOJ#6072]苹果树(矩阵树定理,折半搜索,容斥) 题面 LOJ 题解 emmmm,这题似乎猫讲过一次... 显然先\(meet-in-the-middle\)搜索一下对于每个有用的苹果数量,满 ...

随机推荐

  1. UIPageViewController基本使用

    UIPageViewController基本使用 @interface ViewController ()<UIPageViewControllerDelegate,UIPageViewCont ...

  2. MYSQL中的BlackHole引擎

    MYSQL中的BlackHole引擎 http://blog.csdn.net/ylspirit/article/details/7234021 http://blog.chinaunix.net/u ...

  3. vue中定义多重样式

  4. (转)es进行聚合操作时提示Fielddata is disabled on text fields by default

    根据es官网的文档执行 GET /megacorp/employee/_search { "aggs": { "all_interests": { " ...

  5. POJ3096:Surprising Strings(map)

    http://poj.org/problem?id=3096 for循环真是奇妙! #include <string.h> #include <stdio.h> #includ ...

  6. [LeetCode] 674. Longest Continuous Increasing Subsequence_Easy Dynamic Programming

    Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...

  7. EXTJS4扩展实例:如何使用filter查询treepanel

    我们在使用普通的store时,extjs提供了filterBy,filter等多种方法来过滤数据达到查询效果,但在treepanel中的streeStore却没有实现这个查询,于是,就有了这篇文章. ...

  8. 原生http模块与使用express框架对比

    node的http创建服务与利用Express框架有何不同 原生http模块与使用express框架对比: const http = require("http"); let se ...

  9. DOM EVENT

    属性 此事件发生在何时... onabort 图像的加载被中断. onblur 元素失去焦点. onchange 域的内容被改变. onclick 当用户点击某个对象时调用的事件句柄. ondblcl ...

  10. java opencv使用相关

    Using OpenCV Java with Eclipse http://docs.opencv.org/2.4/doc/tutorials/introduction/java_eclipse/ja ...