题目描述

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.

输入输出格式

输入格式:

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.

输出格式:

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.

输入输出样例

输入样例#1:

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

输出样例#1:

8

1

1

3

先用高斯消元转化为上三角,后求对角线之积算出行列式绝对值

code:

  1. //Menteur_Hxy
  2. #include<cstdio>
  3. #include<iostream>
  4. #include<cstring>
  5. #include<cmath>
  6. using namespace std;
  7. #define ll long long
  8. ll rd() {
  9. ll x=0,fla=1; char c=' ';
  10. while(c<'0' || c>'9') {c=getchar();if(c=='-') fla=-fla;}
  11. while(c>='0' && c<='9') x=x*10+c-'0',c=getchar();
  12. return x*fla;
  13. }
  14. const double eps=1e-12;
  15. int T,n,m;
  16. double a[3010][3010];
  17. void gauss() {
  18. for(int i=1;i<=n;i++) {
  19. int mx=i;
  20. for(int j=i+1;j<=n;j++) mx=(a[mx][i]-a[j][i]<-eps)?i:mx;
  21. if(mx!=i) swap(a[i],a[mx]);
  22. if(!a[i][i]) {printf("0\n");return ;}
  23. for(int j=i+1;j<=n;j++) {
  24. double t=a[j][i]/a[i][i];
  25. for(int k=i;k<=n+1;k++)
  26. a[j][k]-=t*a[i][k];
  27. }
  28. }
  29. double ans=1;
  30. for(int i=1;i<=n;i++) ans=ans*a[i][i];
  31. printf("%.0f\n",fabs(ans));
  32. }
  33. int main() {
  34. T=rd();
  35. while(T--) {
  36. memset(a,0,sizeof a);
  37. n=rd()-1,m=rd();
  38. for(int i=1;i<=m;i++) {
  39. int u=rd(),v=rd();
  40. a[u][u]++,a[v][v]++;
  41. a[u][v]--,a[v][u]--;
  42. }
  43. gauss();
  44. }
  45. return 0;
  46. }

【SPOJ 104】HIGH - Highways (高斯消元)的更多相关文章

  1. SPOJ HIGH Highways ——Matrix-Tree定理 高斯消元

    [题目分析] Matrix-Tree定理+高斯消元 求矩阵行列式的值,就可以得到生成树的个数. 至于证明,可以去看Vflea King(炸树狂魔)的博客 [代码] #include <cmath ...

  2. SPOJ HIGH(生成树计数,高斯消元求行列式)

    HIGH - Highways no tags  In some countries building highways takes a lot of time... Maybe that's bec ...

  3. [spoj104][Highways] (生成树计数+矩阵树定理+高斯消元)

    In some countries building highways takes a lot of time... Maybe that's because there are many possi ...

  4. 【BZOJ-3143】游走 高斯消元 + 概率期望

    3143: [Hnoi2013]游走 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2264  Solved: 987[Submit][Status] ...

  5. 【BZOJ-3270】博物馆 高斯消元 + 概率期望

    3270: 博物馆 Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 292  Solved: 158[Submit][Status][Discuss] ...

  6. *POJ 1222 高斯消元

    EXTENDED LIGHTS OUT Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9612   Accepted: 62 ...

  7. [bzoj1013][JSOI2008][球形空间产生器sphere] (高斯消元)

    Description 有一个球形空间产生器能够在n维空间中产生一个坚硬的球体.现在,你被困在了这个n维球体中,你只知道球 面上n+1个点的坐标,你需要以最快的速度确定这个n维球体的球心坐标,以便于摧 ...

  8. hihoCoder 1196 高斯消元·二

    Description 一个黑白网格,点一次会改变这个以及与其连通的其他方格的颜色,求最少点击次数使得所有全部变成黑色. Sol 高斯消元解异或方程组. 先建立一个方程组. \(x_i\) 表示这个点 ...

  9. BZOJ 2844 albus就是要第一个出场 ——高斯消元 线性基

    [题目分析] 高斯消元求线性基. 题目本身不难,但是两种维护线性基的方法引起了我的思考. void gauss(){ k=n; F(i,1,n){ F(j,i+1,n) if (a[j]>a[i ...

随机推荐

  1. 做acm 需要学的算法

    做acm 需要学的算法 转一个搞ACM需要的掌握的算法.  要注意,ACM的竞赛性强,因此自己应该和自己的实际应用联系起来.  适合自己的才是好的,有的人不适合搞算法,喜欢系统架构,因此不要看到别人什 ...

  2. poj 3267 The Cow Lexicon (动态规划)

    The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8167   Accepted: 3845 D ...

  3. JAVAEE之-----MySQL分页技术(带搜索)

    需求: 为什么须要採用分页技术呢?在数据库中我们查询数据的时候,须要将数据返回到显示页面.数据库中含有大量数据,所有显示在一个页面过于太多,所以我们须要採用分页技术.每一页显示不同数据. 主要解决这个 ...

  4. DNS负载均衡 Nginx 负载均衡的种类

    DNS负载均衡 当一个网站有足够多的用户的时候,假如每次请求的资源都位于同一台机器上面,那么这台机器随时可能会蹦掉.处理办法就是用DNS负载均衡技术,它的原理是在DNS服务器中为同一个主机名配置多个I ...

  5. ACdream 1154 Lowbit Sum (数位DP)

    Lowbit Sum Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitSt ...

  6. luogu1447 能量采集

    题目大意 给出m,n,对于每一个整数x∈[1,m],y∈[1,n]都有一点(x,y).处理每个点所需要的能量为2*k+1,k为该点到原点经过的点的数量(不包括该点本身).求处理所有点所需要的能量和. ...

  7. luogu4180 次小生成树Tree 树上倍增

    题目:求一个无向图的严格次小生成树(即次小生成树的边权和严格小于最小生成树的边权和) 首先求出图中的最小生成树.任意加一条树外边都会导致环的出现.我们现在目标是在树外边集合B中,找到边b∈B,a∈b所 ...

  8. 循环神经网络(RNN, Recurrent Neural Networks)——无非引入了环,解决时间序列问题

    摘自:http://blog.csdn.net/heyongluoyao8/article/details/48636251 不同于传统的FNNs(Feed-forward Neural Networ ...

  9. hdoj--3339--In Action(最短路+01背包)

    In Action Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  10. 【BZOJ 2982】 combination

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2982 [算法] lucas定理 [代码] #include<bits/stdc ...