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的更多相关文章

  1. 生成树的计数(基尔霍夫矩阵):BZOJ 1002 [FJOI2007]轮状病毒

    1002: [FJOI2007]轮状病毒 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 3928  Solved: 2154[Submit][Statu ...

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

    基尔霍夫矩阵 https://blog.csdn.net/w4149/article/details/77387045 https://blog.csdn.net/qq_29963431/articl ...

  3. BZOJ 4031 HEOI2015 小Z的房间 基尔霍夫矩阵+行列式+高斯消元 (附带行列式小结)

    原题链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4031 Description 你突然有了一个大房子,房子里面有一些房间.事实上,你的房子可 ...

  4. BZOJ 1002: [FJOI2007]轮状病毒【生成树的计数与基尔霍夫矩阵简单讲解+高精度】

    1002: [FJOI2007]轮状病毒 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 5577  Solved: 3031[Submit][Statu ...

  5. 疯子的算法总结(九) 图论中的矩阵应用 Part 2 矩阵树 基尔霍夫矩阵定理 生成树计数 Matrix-Tree

    定理: 1.设G为无向图,设矩阵D为图G的度矩阵,设C为图G的邻接矩阵. 2.对于矩阵D,D[i][j]当 i!=j 时,是一条边,对于一条边而言无度可言为0,当i==j时表示一点,代表点i的度. 即 ...

  6. 【BZOJ】1002:轮状病毒(基尔霍夫矩阵【附公式推导】或打表)

    Description 轮状病毒有很多变种,所有轮状病毒的变种都是从一个轮状基产生的.一个N轮状基由圆环上N个不同的基原子和圆心处一个核原子构成的,2个原子之间的边表示这2个原子之间的信息通道.如下图 ...

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

    基本思路: 1.先观察规律,写写画画未果 2.写程序暴力打表找规律,找出规律 1-15的答案:1    5    16    45    121 320 841     2205   5776 151 ...

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

    1002: [FJOI2007]轮状病毒 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2234  Solved: 1227[Submit][Statu ...

  9. BZOJ1002 FJOI2007 轮状病毒 【基尔霍夫矩阵+高精度】

    BZOJ1002 FJOI2007 轮状病毒 Description 轮状病毒有很多变种,所有轮状病毒的变种都是从一个轮状基产生的.一个N轮状基由圆环上N个不同的基原子和圆心处一个核原子构成的,2个原 ...

随机推荐

  1. J2EE 读取文件路径

    在J2ee中实现java类读取webcontent/web-inf/config.xml的实现代码 ,其中../config.xml相对于classes的路径 java.net.URL url = t ...

  2. SpringSecurity 在MVC 中的简单使用(翻译的,稍加改动)

    Spring Security允许开发人员轻松地将安全功能集成到J2EE Web应用程序中,它通过Servlet过滤器实现“用户自定义”安全检查. 在本教程中,我们将向您展示如何在Spring MVC ...

  3. 谁是谁的first-child

    看过CSS伪类选择器之后,心想也就如此嘛,:first-child选择元素的第一个子元素,有什么难的,可一到实践中,还是到处碰壁啊. <body> <ul class="f ...

  4. Encapsulation.

    Access control is often referred to as implementation hiding. Wrapping data and methods within class ...

  5. copssh加bitvise

    只是简单记录下自己在成功使用的方案: 目的:为了突破公司对网站和qq的限制 具备的条件:一台云服务器.Copssh_4.1.0.bitvise ssh client 4.62.公司电脑客户端 一.首先 ...

  6. webViewDidFinishLoad 执行多次的问题

    在做网页加载进度条的时候,发现UIWebViewDelegate中webViewDidFinishLoad方法会执行多次: - (void)webViewDidStartLoad:(UIWebView ...

  7. css3基础教程十六变形与动画animation

    前面我们讲过的变形与动画一般都是通过鼠标的单击.获得焦点,被点击或对元素进行一定改变后以后触发效果的,那么有没有像Flash一样自动播放的动画效果呢?答案当然是肯定的,这就是我们今天要讲到的anima ...

  8. xml 个人练习2

    package cn.gdpe.xml; import java.io.File;import java.io.FileInputStream;import java.io.IOException;i ...

  9. tr命令的使用

    tr是translate的简写,亦即翻译,但是遗憾的是,它不能翻译句子,只能翻译单个字符. 1 tr的工作原理是什么? 先记住一点,tr命令不接受指定的文件参数,而只是对标准输入进行翻译.好了,记住这 ...

  10. yzoi1109&&viojs1042最小步数的一点看法——回文数

    Description - 问题描述 有一天,雄霸传授本人风神腿法第一式:捕风捉影..............的步法(弟子一:堂主,你大喘气呀.风:你给我闭嘴.)捕风捉影的关键是换气(换不好就会大喘气 ...