首页 技术 正文
技术 2022年11月21日
0 收藏 448 点赞 2,610 浏览 1131 个字

题目描述:

  一个由n个部门组成的公司现在需要分层,但是由于员工间的一些小小矛盾,使得他们并不愿意做上下级,问在满足他们要求以后有多少种分层的方案数?

解题思路:

  生成树计数模板题,建立Kirchhoff矩阵,利用Matrix_tree定理求解。

  Kirchhoff矩阵:假设G为n*n矩阵,C为G的入度矩阵(i==j时,C[i][j]等于i的入度;i!=j时,C[i][j]等于零),A为G的邻接矩阵,那么就有Kirchhoff矩阵等于C-A。

  Matrix_tree定理:G的不同生成树的个数等于其所对应的kirchhoff矩阵的n-1阶行列式的绝对值(PS:n-1阶行列式等于Kirchhoff矩阵减去第r行,第r列后所形成的矩阵,其中1<=r<=n)

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long double LD;
const int maxn = ;
const LD sng = 1e-; LD b[maxn][maxn];
int a[maxn][maxn];
bool Exp(LD x)
{
return ((x>=)?x:-x)<sng;
}
LD MTree (int n)
{
int sign = , j;
LD res = ;
for (int i=; i<n; i++)
{
if (Exp(b[i][i]))
{
for (j=i+; j<n; j++)
if (!Exp(b[j][i]))
break;
if (j == n)
return ;
for (int k=i; k<n; k++)
swap (b[i][k], b[j][k]);
sign ++;
}
res *= b[i][i];
for (j=i+; j<n; j++)
b[i][j] /= b[i][i];
for (j=i+; j<n; j++)
for (int k=i+; k<n; k++)
b[j][k] -= b[j][i] * b[i][k];
}
if (sign % )
res = -res;
return res;
}
int main ()
{
int n, m, k;
while (scanf ("%d %d %d", &n, &m, &k) != EOF)
{
memset (a, , sizeof(a));
memset (b, , sizeof(b));
while (m --)
{
int u, v;
scanf ("%d %d", &u, &v);
u--, v--;
a[u][v] = a[v][u] = ;
}
for (int i=; i<n; i++)
for (int j=; j<n; j++)
if (!a[i][j] && i != j)
{
b[i][i] ++;
b[i][j] = -;
}
printf ("%.0f\n", (double)MTree (n - ));
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,954
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,479
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,291
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,108
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,740
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,774