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 ...
随机推荐
- lintcode: Missing String
Missing String 描述: Given two strings, you have to find the missing string. Have you met this questi ...
- 基于MTCNN多任务级联卷积神经网络进行的人脸识别 世纪晟人脸检测
神经网络和深度学习目前为处理图像识别的许多问题提供了最佳解决方案,而基于MTCNN(多任务级联卷积神经网络)的人脸检测算法也解决了传统算法对环境要求高.人脸要求高.检测耗时高的弊端. 基于MTCNN多 ...
- [leetcode-753-Open the Lock]
You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: '0', '1', '2', '3', ...
- LVS+Keepalive+Nginx实现负载均衡
本文参考:http://blog.csdn.net/yinwenjie/article/details/47211551 简单粗暴写一下,做备忘,刚刚搭好没做优化呢,后期补充 一.机器准备 LVS-M ...
- 针对XX系统的可用性和易用性构想
可用性是与系统故障有关的一个质量属性,是指系统正常运行的时间的比例,一般通过两次故障之间的时间长度或在系统崩溃情况下能恢复正常运行的速度来衡量,同时此概念涉及一个公式的计算,就是系统正常运行时间的百分 ...
- python学习第一天-语法学习
1.python简介 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,Guido开始写能够解释Python语言语法的解释器.Python这个名字,来自 ...
- [计算机网络-传输层] 面向连接的传输:TCP
参考:http://blog.csdn.net/macdroid/article/details/49070185 在学习TCP之前我们先来看一下可靠数据传输需要提供什么样的机制: ·差错检测机制:检 ...
- 第20天:京东nav、footer部分制作
一.鼠标的4种状态 cursor:pointer; 鼠标变成小手cursor:default;小白cursor:move;移动cursor:text;文本输入 二.网页布局:1.input.butto ...
- ASP.NET MVC下使用文件上传和IIS7下的默认设置限制了上传大小的方法
不多说了,直接用别人的 http://www.cnblogs.com/jiekzou/p/4491505.html
- 当线程是继承Thread时候 实现方法是静态方法时候 可以用锁修饰静态方法 此时锁对象是类 为啥继承的线程要用 类对象呢 因为他能生成很多实例 接口实现为啥用this 呢因为他就一个