博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
生成树计数
阅读量:5275 次
发布时间:2019-06-14

本文共 3093 字,大约阅读时间需要 10 分钟。

发现一个总结的很好的台湾同学的生成树学习笔记:

不知道这个台湾同学是谁,但是好像极其厉害,附博客:

还有这个同学编纂的<台湾师范大学ACM算法入门教程(繁体)>

终于知道楼教主为什么去facebook了,因为他连续2011,2012两年获得Facebook Hacker Cup第三。

SPOJ-104:无向图生成树计数模板题

104. Highways

Problem code: HIGH

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:44 53 44 22 31 21 32 12 11 03 31 22 33 1Sample output:8113 附生成树计数模板:
/*SPOJ submission 7361111 (C++ 4.3.2) plaintext list. Status: AC, problem HIGH, contest SPOJ. By ldg859 (ldg), 2012-07-23 10:35:07.*/ #include
#include
#include
#include
using namespace std;#define zero(x) ((x>0? x:-x)<1e-15)int const MAXN = 100;double a[MAXN][MAXN];double b[MAXN][MAXN];int g[53][53];int n,m;double det(double a[MAXN][MAXN],int n) { int i, j, k, sign = 0; double ret = 1, t; for (i = 0; i < n; i++) for (j = 0; j < n; j++) b[i][j] = a[i][j]; for (i = 0; i < n; i++) { if (zero(b[i][i])) { for (j = i + 1; j < n; j++) if (!zero(b[j][i])) break; if (j == n) return 0; for (k = i; k < n; k++) t = b[i][k], b[i][k] = b[j][k], b[j][k] = t; sign++; } ret *= b[i][i]; for (k = i + 1; k < n; k++) b[i][k] /= b[i][i]; for (j = i + 1; j < n; j++) for (k = i + 1; k < n; k++) b[j][k] -= b[j][i] * b[i][k]; } if (sign & 1) ret = -ret; return ret;}void build(){ while (m--) { int a, b; scanf("%d%d", &a, &b); g[a-1][b-1]=g[b-1][a-1]=1; }}int main() { int cas; scanf("%d", &cas); while (cas--) { scanf("%d%d", &n, &m); memset(g,0,sizeof(g)); build(); memset(a,0,sizeof(a)); for (int i=0;i

 

转载于:https://www.cnblogs.com/markliu/archive/2012/07/23/2605198.html

你可能感兴趣的文章
ios不响应presentModalViewController界面的处理
查看>>
Virtualization基础
查看>>
P2344 奶牛抗议
查看>>
人工智能实战_第三次作业_陈泽寅
查看>>
让开发也懂前端
查看>>
asp.net中退出登陆的相关问题(解决后退或直接粘贴地址进入网页的问题)
查看>>
Java实战之04JavaWeb-02Request和Response
查看>>
[转]Blue Prism Architecture
查看>>
彻底的卸载SQL Server2005
查看>>
C# Json格式转换成List集合
查看>>
最大化平均值 (二分搜索法)
查看>>
讲一下python的背景知识
查看>>
jdbc 驱动设置
查看>>
windows 编程 —— 消息与参数(定时器、初始化消息、改变大小)
查看>>
ES6基础知识清单
查看>>
Java线程池ThreadPoolExecutor使用和分析
查看>>
Power of Two
查看>>
批量隐藏注释
查看>>
过滤选择器——可见性过滤选择器
查看>>
testing
查看>>