UVA10766:Organising the Organisation(生成树计数)
Organising the Organisation
题目链接:https://vjudge.net/problem/UVA-10766
Description:
I am the chief of the Personnel Division of a moderate-sized company that wishes to remain anonymous, and I am currently facing a small problem for which I need a skilled programmer’s help. Currently, our company is divided into several more or less independent divisions. In order to make our business more efficient, these need to be organised in a hierarchy, indicating which divisions are in charge of other divisions. For instance, if there are four divisions A, B, C and D we could organise them as in Figure 1, with division A controlling divisions B and D, and division D controlling division C. One of the divisions is Central Management (division A in the figure above), and should of course be at the top of the hierarchy, but the relative importance of the remaining divisions is not determined, so in Figure 1 above, division C and D could equally well have switched places so that C was in charge over division D. One complication, however, is that it may be impossible to get some divisions to cooperate with each other, and in such a case, neither of these divisions can be directly in charge of the other. For instance, if in the example above A and D are unable to cooperate, Figure 1 is not a valid way to organise the company. In general, there can of course be many different ways to organise the organisation, and thus it is desirable to find the best one (for instance, it is not a good idea to let the programming people be in charge of the marketing people). This job, however, is way too complicated for you, and your job is simply to help us find out how much to pay the consultant that we hire to find the best organisation for us. In order to determine the consultant’s pay, we need to find out exactly how difficult the task is, which is why you have to count exactly how many different ways there are to organise the organisation. Oh, and I need the answer in five hours.
Input:
The input consists of a series of test cases, at most 50, terminated by end-of-file. Each test cases begins with three integers n, m, k (1 ≤ n ≤ 50, 1 ≤ m ≤ n, 0 ≤ k ≤ 1500). n denotes the number of divisions in the company (for convenience, the divisions are numbered from 1 to n), and k indicates which division is the Central Management division. This is followed by m lines, each containing two integers 1 ≤ i, j ≤ n, indicating that division i and division j cannot cooperate (thus, i cannot be directly in charge of j and j cannot be directly in charge of i). You may assume that i and j are always different.
Output:
For each test case, print the number of possible ways to organise the company on a line by itself. This number will be at least 1 and at most 1015 . Note: The three possible hierarchies in the first sample case
Sample Input:
5 5 2 3 1 3 4 4 5 1 4 5 3 4 1 1 1 4 3 0 2
Sample Output:
3 8 3
题意:
给出n个点,然后一个主结点k,之后给出m个关系,每个关系会输入u,v,表示这两个点不能直接相连。最后问总方案数为多少。
题解:
生成树计数模板题,主要是Matrix-Tree定理,我现在还不会证...但是对于这类题知道就好了吧?
之后构造基尔霍夫矩阵就行了,就是度数矩阵 - 邻接矩阵。
然后用类似于高斯消元的方法去求解n-1阶行列式,最后主对角线的乘积就是答案。
具体见代码吧:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = ;
ll b[N][N];
int g[N][N];
int n,m,k;
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(){
while(scanf("%d%d%d",&n,&m,&k)!=EOF){
memset(b,,sizeof(b));
memset(g,,sizeof(g));
for(int i=;i<=m;i++){
int u,v;
scanf("%d%d",&u,&v);
if(u==v) continue ;
g[u][v]=g[v][u]=;//考虑重边
}
for(int i=;i<=n;i++){
b[i][i]=n-;
for(int j=;j<=n;j++){
if(i==j) continue ;
b[i][j]=-;
}
}
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(g[i][j]){
b[i][i]--;
b[i][j]=b[j][i]=;
}
}
}
printf("%lld\n",Det(n));
}
return ;
}
UVA10766:Organising the Organisation(生成树计数)的更多相关文章
- UVa 10766 Organising the Organisation (生成树计数)
题意:给定一个公司的人数,然后还有一个boss,然后再给定一些人,他们不能成为直属上下级关系,问你有多少种安排方式(树). 析:就是一个生成树计数,由于有些人不能成为上下级关系,也就是说他们之间没有边 ...
- Uva10766 Organising the Organisation
题目链接戳这里 基尔霍夫矩阵裸题.构建基尔霍夫矩阵(度数矩阵-邻接矩阵),求他的任意\(n-1\)阶主子式的绝对值即为答案. 这题开始用java写,结果BigInteger太慢Tle了. 后来用c++ ...
- 「UVA10766」Organising the Organisation(生成树计数)
BUPT 2017 Summer Training (for 16) #6C 题意 n个点,完全图减去m条边,求生成树个数. 题解 注意可能会给重边. 然后就是生成树计数了. 代码 #include ...
- Organising the Organisation(uva10766)(生成树计数)
Input Output Sample Input 5 5 2 3 1 3 4 4 5 1 4 5 3 4 1 1 1 4 3 0 2 Sample Output 3 8 3 题意: 有一张图上有\( ...
- 生成树的计数(基尔霍夫矩阵):UVAoj 10766 Organising the Organisation SPOJ HIGH - Highways
HIGH - Highways In some countries building highways takes a lot of time... Maybe that's because th ...
- Uva 10766 Organising the Organisation (Matrix_tree 生成树计数)
题目描述: 一个由n个部门组成的公司现在需要分层,但是由于员工间的一些小小矛盾,使得他们并不愿意做上下级,问在满足他们要求以后有多少种分层的方案数? 解题思路: 生成树计数模板题,建立Kirchhof ...
- uva10766生成树计数
此类题是给定一个无向图,求所有生成树的个数,生成树计数要用到Matrix-Tree定理(Kirchhoff矩阵-树定理) G的度数矩阵D[G]是一个n*n的矩阵,并且满足:当i≠j时,dij=0:当i ...
- kuangbin带你飞 生成树专题 : 次小生成树; 最小树形图;生成树计数
第一个部分 前4题 次小生成树 算法:首先如果生成了最小生成树,那么这些树上的所有的边都进行标记.标记为树边. 接下来进行枚举,枚举任意一条不在MST上的边,如果加入这条边,那么肯定会在这棵树上形成一 ...
- 【BZOJ1002】【FJOI2007】轮状病毒(生成树计数)
1002: [FJOI2007]轮状病毒 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 1766 Solved: 946[Submit][Status ...
随机推荐
- 人脸识别 ArcFace Demo [Windows]
Arcsoft ArcfaceDemo for Windows, VS2013 C++ 使用虹软技术开发完成 使用步骤: 1.下载SDK包,32位Windows平台将五个SDK包里lib中的文件到 ...
- 感知机(perceptron)
- solidity 智能合约操作
合约编译 #!/usr/bin/env python # coding: utf8 import json import os # Solc Compiler from functools impor ...
- android课程第一节(TextView控件使用)
TextView控件使用 一.TextView基本使用(创建方式) 1.在程序中创建TextView对象 如下代码: @Override protected void onCreate(Bundle ...
- 【转】jQuery最佳实践
上周,我整理了<jQuery设计思想>. 那篇文章是一篇入门教程,从设计思想的角度,讲解"怎么使用jQuery".今天的文章则是更进一步,讲解"如何用好jQu ...
- Python中的赋值语法
Python中复制语法有6种 Basic Form >>>spam = 'spam' Tuple assignment >>>spam, ham = 'spam', ...
- 基于TensorFlow解决手写数字识别的Softmax方法、多层卷积网络方法和前馈神经网络方法
一.基于TensorFlow的softmax回归模型解决手写字母识别问题 详细步骤如下: 1.加载MNIST数据: input_data.read_data_sets('MNIST_data',one ...
- 代替iframe的方法
$('#framecont').html('').load("pageURL"); 使用jQuery.
- TCP系列06—连接管理—5、TCP fastopen(TFO)
一.TFO背景 当前web和web-like应用中一般都是在三次握手后开始数据传输,相比于UDP,多了一个RTT的时延,即使当前很多应用使用长连接来处理这种情况,但是仍然由一定比例的短连接,这额外多出 ...
- 3dContactPointAnnotationTool开发日志(二)
今天看的时候发现其实www的方式是可以根据指定路径读取本地图片到Image中的.也就是昨天提到的第二种方式. 随便选了个图片做示范: 修改后的代码如下: using System.Collec ...