生成树的计数(基尔霍夫矩阵):UVAoj 10766 Organising the Organisation SPOJ HIGH - Highways
HIGH - Highways
In some countries building highways takes a lot of time... Maybe that's because there are many possiblities to construct a network of highways and engineers can't make up their minds which one to choose. Suppose we have a list of cities that can be connected directly. Your task is to count how many ways there are to build such a network that between every two cities there exists exactly one path. Two networks differ if there are two cities that are connected directly in the first case and aren't in the second case. At most one highway connects two cities. No highway connects a city to itself. Highways are two-way.
Input
The input begins with the integer t, the number of test cases (equal to about 1000). Then t test cases follow. The first line of each test case contains two integers, the number of cities (1<=n<=12) and the number of direct connections between them. Each next line contains two integers a and b, which are numbers of cities that can be connected. Cities are numbered from 1 to n. Consecutive test cases are separated with one blank line.
Output
The number of ways to build the network, for every test case in a separate line. Assume that when there is only one city, the answer should be 1. The answer will fit in a signed 64-bit integer.
Example
Sample input:
4
4 5
3 4
4 2
2 3
1 2
1 3 2 1
2 1 1 0 3 3
1 2
2 3
3 1 Sample output:
8
1
1
3
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=;
int C[maxn][maxn],n,m; int Solve(){
for(int i=;i<n;i++){
for(int j=i+;j<n;j++)
while(C[j][i]){
int r=C[i][i]/C[j][i];
for(int k=i;k<n;k++)
C[i][k]-=C[j][k]*r;
swap(C[i],C[j]);
}
}
long long ret=;
for(int i=;i<n;i++)
ret*=C[i][i];
return ret<?-ret:ret;
} int main(){
int T;
scanf("%d",&T);
while(T--){
memset(C,,sizeof(C));
scanf("%d%d",&n,&m);
while(m--){
int a,b;
scanf("%d%d",&a,&b);
C[a][a]++;C[b][b]++;
C[a][b]=C[b][a]=-;
}
printf("%d\n",Solve());
}
return ;
}
10766 - Organising the Organisation
Time limit: 3.000 seconds
Input: Standard Input
Output: Standard Output
Time Limit: 2 Second
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, 0 ≤ m ≤ 1500, 1 ≤ k ≤ n). ndenotes 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 jcannot 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.
Sample Input Output for Sample Input
5 5 2 3 1 3 4 4 5 1 4 5 3 4 1 1 1 4 3 0 2 |
3 8 3 |
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=;
int n,m,rt,G[maxn][maxn];
long long C[maxn][maxn];
long long Solve(){
for(int i=;i<n;i++){
for(int j=i+;j<n;j++)
while(C[j][i]){
long long r=C[i][i]/C[j][i];
for(int k=i;k<n;k++)
C[i][k]-=C[j][k]*r;
swap(C[i],C[j]);
}
}
long long ret=;
for(int i=;i<n;i++)
ret*=C[i][i];
return ret<?-ret:ret;
}
int main(){
while(scanf("%d%d%d",&n,&m,&rt)==){
memset(G,-,sizeof(G));
memset(C,,sizeof(C));
while(m--){
int a,b;
scanf("%d%d",&a,&b);
G[a][b]=G[b][a]=;
}
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(i!=j&&G[i][j]){
C[i][j]=-;
C[i][i]+=;
}
printf("%lld\n",Solve());
}
return ;
}
生成树的计数(基尔霍夫矩阵):UVAoj 10766 Organising the Organisation SPOJ HIGH - Highways的更多相关文章
- 生成树的计数(基尔霍夫矩阵):BZOJ 1002 [FJOI2007]轮状病毒
1002: [FJOI2007]轮状病毒 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 3928 Solved: 2154[Submit][Statu ...
- 无向图生成树计数 基尔霍夫矩阵 SPOJ Highways
基尔霍夫矩阵 https://blog.csdn.net/w4149/article/details/77387045 https://blog.csdn.net/qq_29963431/articl ...
- BZOJ 4031 HEOI2015 小Z的房间 基尔霍夫矩阵+行列式+高斯消元 (附带行列式小结)
原题链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4031 Description 你突然有了一个大房子,房子里面有一些房间.事实上,你的房子可 ...
- BZOJ 1002: [FJOI2007]轮状病毒【生成树的计数与基尔霍夫矩阵简单讲解+高精度】
1002: [FJOI2007]轮状病毒 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 5577 Solved: 3031[Submit][Statu ...
- 疯子的算法总结(九) 图论中的矩阵应用 Part 2 矩阵树 基尔霍夫矩阵定理 生成树计数 Matrix-Tree
定理: 1.设G为无向图,设矩阵D为图G的度矩阵,设C为图G的邻接矩阵. 2.对于矩阵D,D[i][j]当 i!=j 时,是一条边,对于一条边而言无度可言为0,当i==j时表示一点,代表点i的度. 即 ...
- 【BZOJ】1002:轮状病毒(基尔霍夫矩阵【附公式推导】或打表)
Description 轮状病毒有很多变种,所有轮状病毒的变种都是从一个轮状基产生的.一个N轮状基由圆环上N个不同的基原子和圆心处一个核原子构成的,2个原子之间的边表示这2个原子之间的信息通道.如下图 ...
- bzoj1002 轮状病毒 暴力打标找规律/基尔霍夫矩阵+高斯消元
基本思路: 1.先观察规律,写写画画未果 2.写程序暴力打表找规律,找出规律 1-15的答案:1 5 16 45 121 320 841 2205 5776 151 ...
- bzoj 1002 [FJOI2007]轮状病毒 高精度&&找规律&&基尔霍夫矩阵
1002: [FJOI2007]轮状病毒 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 2234 Solved: 1227[Submit][Statu ...
- BZOJ1002 FJOI2007 轮状病毒 【基尔霍夫矩阵+高精度】
BZOJ1002 FJOI2007 轮状病毒 Description 轮状病毒有很多变种,所有轮状病毒的变种都是从一个轮状基产生的.一个N轮状基由圆环上N个不同的基原子和圆心处一个核原子构成的,2个原 ...
随机推荐
- linux与Windows共享文件配置
linux与Windows共享文件配置: 1.进入超级用户:$su root 2.启动tftp服务器:#netstat -a | grep tftp,出现如图所示的消息表示tftp服务器已安装成功: ...
- Java实现对文件的上传下载操作
通过servlet,实现对文件的上传功能 1.首先创建一个上传UploadHandleServlet ,代码如下: package me.gacl.web.controller; import jav ...
- Eclipse设立不格式化注释
From:http://www.educity.cn/wenda/467693.html Eclipse设置不格式化注释 注释中写点带格式的文字,format后全乱了,解决办法如下: Windows ...
- C# - Sql数据类型的对应关系
<Language From="SQL" To="C#"> <Type From="bigint" To="lo ...
- sqlserver触发器如何将一个库中的数据插入到另外一个库中
需求:实现的功能就是,查询当前表的所有信息,插入到另外一个库中(同一台机器,同一个SqlServer) 解决:insert into dB2.dbo.TB2 select * from db1.dbo ...
- Xcode升后插件失效
Xcode升后插件失效,与添加插件不小心点击Skip Bundle解决办法 字数267 阅读4731 评论1 喜欢12 今天升级了xcode到6.4 发现之前装的插件不能使用了.这里有一个解决的方案: ...
- 解决右滑返回手势和UIScrollView中的手势冲突
当在一个viewController中添加了scrollView或者tableView的时候,贴边侧滑返回的时候会首先触发滚动而失效,要解决这个问题,需要通过requireGestureRecogni ...
- javascript中常用的DOM事件
//常用事件 onclick 点击事件 onmousedown 鼠标按下 onmousemove 鼠标移动 onmouseup 鼠标抬起 onmouseover 鼠标放上 onmouseout 鼠标放 ...
- Jquery 点击空白处消失
$(document).bind("click", function (e){ if ( $((e.target || e.srcElement)).closest("# ...
- mysql 数据sqoop到hive 步骤
1.hive建表 hive是支持分区的,但是这次建表没有写分区. CREATE TABLE `cuoti_rpt` ( `COURSE_ID` string, `NAME` string, `PERI ...