生成树的计数(基尔霍夫矩阵):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
 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 using namespace std;
 5 const int maxn=20;
 6 int C[maxn][maxn],n,m;
 7
 8 int Solve(){
 9     for(int i=1;i<n;i++){
10         for(int j=i+1;j<n;j++)
11             while(C[j][i]){
12                 int r=C[i][i]/C[j][i];
13                 for(int k=i;k<n;k++)
14                     C[i][k]-=C[j][k]*r;
15                 swap(C[i],C[j]);
16             }
17     }
18     long long ret=1;
19     for(int i=1;i<n;i++)
20         ret*=C[i][i];
21     return ret<0?-ret:ret;
22 }
23
24 int main(){
25     int T;
26     scanf("%d",&T);
27     while(T--){
28         memset(C,0,sizeof(C));
29         scanf("%d%d",&n,&m);
30         while(m--){
31             int a,b;
32             scanf("%d%d",&a,&b);
33             C[a][a]++;C[b][b]++;
34             C[a][b]=C[b][a]=-1;
35         }
36         printf("%d\n",Solve());
37     }
38     return 0;
39 }

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 
 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 using namespace std;
 5 const int maxn=53;
 6 int n,m,rt,G[maxn][maxn];
 7 long long C[maxn][maxn];
 8 long long Solve(){
 9     for(int i=1;i<n;i++){
10         for(int j=i+1;j<n;j++)
11             while(C[j][i]){
12                 long long r=C[i][i]/C[j][i];
13                 for(int k=i;k<n;k++)
14                     C[i][k]-=C[j][k]*r;
15                 swap(C[i],C[j]);
16             }
17     }
18     long long ret=1;
19     for(int i=1;i<n;i++)
20         ret*=C[i][i];
21     return ret<0?-ret:ret;
22 }
23 int main(){
24     while(scanf("%d%d%d",&n,&m,&rt)==3){
25         memset(G,-1,sizeof(G));
26         memset(C,0,sizeof(C));
27         while(m--){
28             int a,b;
29             scanf("%d%d",&a,&b);
30             G[a][b]=G[b][a]=0;
31         }
32         for(int i=1;i<=n;i++)
33             for(int j=1;j<=n;j++)
34                 if(i!=j&&G[i][j]){
35                     C[i][j]=-1;
36                     C[i][i]+=1;
37                 }
38         printf("%lld\n",Solve());
39     }
40     return 0;
41 }
时间: 2024-10-21 09:48:39

生成树的计数(基尔霍夫矩阵):UVAoj 10766 Organising the Organisation SPOJ HIGH - Highways的相关文章

无向图生成树计数 基尔霍夫矩阵 SPOJ Highways

基尔霍夫矩阵 https://blog.csdn.net/w4149/article/details/77387045 https://blog.csdn.net/qq_29963431/article/details/51236064 题目链接  https://vjudge.net/problem/SPOJ-HIGH AC代码 1 #include <bits/stdc++.h> 2 #define pb push_back 3 #define mp make_pair 4 #define

hdu4305Lightning 生成树计数(基尔霍夫矩阵)+高斯消元+逆元

题目:http://acm.hdu.edu.cn/showproblem.php?pid=4305 题意:比较裸的生成树计数问题. 如何处理生成树计数问题? 基尔霍夫矩阵: if i==j  Kir[i][j] = i的度数 if i!=j   Kir[i][j] = i到j的平行边的个数的负数 即,基尔霍夫矩阵 = 度数矩阵 - 邻接矩阵 将基尔霍夫矩阵删去第i行和第i列,余下i-1阶的行列式的值即为生成树个数.(证明略) 求行列式的值可以将行列式转为上三角阵,求对角线上的积即为行列式的值.

[bzoj1002][FJOI2007]轮状病毒-题解[基尔霍夫矩阵][高精度][递推]

Description 轮状病毒有很多变种,所有轮状病毒的变种都是从一个轮状基产生的.一个N轮状基由圆环上N个不同的基原子和圆心处一个核原子构成的,2个原子之间的边表示这2个原子之间的信息通道.如下图所示 N轮状病毒的产生规律是在一个N轮状基中删去若干条边,使得各原子之间有唯一的信息通道,例如共有16个不同的3轮状病毒,如下图所示 现给定n(N<=100),编程计算有多少个不同的n轮状病毒 Input 第一行有1个正整数n Output 计算出的不同的n轮状病毒数输出 Sample Input

[BZOJ1002] [FJOI2007] 轮状病毒 (基尔霍夫矩阵)

Description 给定n(N<=100),编程计算有多少个不同的n轮状病毒. Input 第一行有1个正整数n. Output 将编程计算出的不同的n轮状病毒数输出 Sample Input 3 Sample Output 16 HINT Source Solution 基尔霍夫矩阵,左转生成树的计数及其应用 推出本题的递推式:f[n] = f[n - 1] * 3 - f[n - 2] + 2 如果你能看懂,拜托给我讲讲,本人不懂. 注意要使用高精度 1 #include <cstri

bzoj1002 轮状病毒 暴力打标找规律/基尔霍夫矩阵+高斯消元

基本思路: 1.先观察规律,写写画画未果 2.写程序暴力打表找规律,找出规律 1-15的答案:1    5    16    45    121 320 841     2205   5776 15125 39601  103680  271441    710645      1860496 第1.3.5.7...[奇数位]位是平方数 : 1*1  4*4  11*11   29*29   76*76   199*199  521*521... 第2.4.6.8...[偶数位]位除以5后也是平

bzoj 1002 [FJOI2007]轮状病毒 高精度&amp;&amp;找规律&amp;&amp;基尔霍夫矩阵

1002: [FJOI2007]轮状病毒 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2234  Solved: 1227[Submit][Status] Description 给定n(N<=100),编程计算有多少个不同的n轮状病毒. Input 第一行有1个正整数n. Output 将编程计算出的不同的n轮状病毒数输出 Sample Input 3 Sample Output 16 HINT Source 基尔霍夫矩阵总算编出来了,这道题考

BZOJ 1002 + SPOJ 104 基尔霍夫矩阵 + 一个递推式。

BZOJ 1002 高精度 + 递推 f[1] = 1; f[2] = 5; f[i] = f[i - 1] * 3 - f[i - 2] + 2; SPOJ 104 裸 + 不用Mod 1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <algorithm> 5 #include <iostream> 6 7 using namespace std;

BZOJ 1002: [FJOI2007]轮状病毒 递推/基尔霍夫矩阵树定理

f[n]=3*f[n-1]-f[n-2]+2 1002: [FJOI2007]轮状病毒 Time Limit: 1 Sec  Memory Limit: 162 MB Submit: 2959  Solved: 1644 [Submit][Status][Discuss] Description 给定n(N<=100),编程计算有多少个不同的n轮状病毒. Input 第一行有1个正整数n. Output 将编程计算出的不同的n轮状病毒数输出 Sample Input 3 Sample Outpu

uva 10766 Organising the Organisation 生成树计数

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1707 题意:给出n, m, k.表示n个点,其中m条边不能直接连通,求生成树个数. 思路:参考周冬的<生成树的计数及其应用>.就是Matrix-Tree定理的应用. 对于一个无向图G,它的生成树个数等于其Kirchhoff矩阵任何一个n-1阶主子式的行列式的绝对值.