SGU481 Hero of Our Time
Description
Saratov ACM ICPC teams have a tradition to come together on Halloween and recollect terrifying stories. And the most popular story among the newcomers is the story about the "Mescher Tree". A long time ago, when the famous Dmitry Mescheryakov aka Mescher was very young and he even didn't know how to write Dijkstra algorithm, he faced a difficult problem with a tree. Input file contained \(n\) — the number of vertices, and pairs of vertices, connected with an edge. Without thinking a lot (honestly, the exact reason of that mistake is unknown), he wrote the following code:
read(n);
for i := 1 to n do begin
read(u, v);
g[u, v] := true;
g[v, u] := true;
end;
Mescher successfully compiled his code, got WA on sample test and started long debugging... This story has become a true legend. So it's no surprise that Saratov ACM ICPC teams use the following definition: connected undirected graph with n vertices and n edges is called Mescheryakov Tree or, less formally, Mescher Tree. The area of application of Mescher trees is not well-studied, so we suggest you to solve one of the problems connected with such trees: given n, find the number of distinct Mescher trees with \(n\) vertices. Trees are labeled, i.e. two trees are considered distinct if and only if their adjacency matrices differ.
Input
Input contains single integer number \(n (3 \le n \le 5000)\).
Output
Output the number of Mescher trees with \(n\) vertices without leading zeroes.
Sample Input
3
Sample Output
1
这是一个比较经典的题目,即求\(n\)个点\(n\)条边的有标号无向连通图的个数。
我们设\(f(n)\)为答案,那么有$$f(n) = \frac{(n-1)! \bullet n^n}{2} \bullet \sum_{k = 3}^n \frac{1}{n^k(n-k)!}$$
证明我们可以采用算两次的方法进行证明:
设\(g(n,k)\)表示由\(k\)棵有根数组成的,节点数为\(n\)且带标号的图的个数,不妨设边的方向都是由父亲指向儿子。
再令\(h(n,k)\)表示有多少种长度为\(n-k\)有向边的序列,使得按这个顺序加边后刚好会构成由\(k\)棵根树构成的节点带有标号的图。则我们可以得到\(h(n,k)\)的一种计算方法
\]
这个式子不难得到。我们在考虑另一种方法——每次选出一条边,其中这条边由一个点连向非他自己所属有根树的根(初始每个点自己就是一棵树),每增加一条边就会减少一棵树。故有
\]
于是有$$g(n,k) \times (n-k)! = n^{n-k} \times \frac{(n-1)!}{(k-1)!}$$
解得$$g(n,k) = n^{n-k} \times \binom{n-1}{k-1}$$
之后考虑如何用\(g(n,k)\)求\(f(n)\)。由于\(n\)个点\(n\)条边的无向连通图一定有且只有\(1\)个环。我们可以枚举环的长度\(k\),然后把环看成根,就等同于环上套了\(k\)棵有根树。从\(3\)开始枚举环的长度,便有
\]
其中\(\frac{(k-1)!}{2}\)是\(k\)个数排成一个圈的本质不同排列数。
于是将$$g(n,k) = n^{n-k} \times \binom{n-1}{k-1}$$代入,便可得到开始的公式了。
由于SGU目前不可提交,所以没有代码。
SGU481 Hero of Our Time的更多相关文章
- H5游戏开发之Stick Hero
自从上次发布一个小恐龙游戏以后,到现在10天了,前后又写了3个游戏,挑了一个感觉比较有挑战的游戏和大家分享一下. 效果演示 这是我模拟一个苹果游戏<stick hero>游戏写的一个小游戏 ...
- BZOJ 1191 超级英雄 Hero 题解
BZOJ 1191 超级英雄 Hero 题解 Description 现在电视台有一种节目叫做超级英雄,大概的流程就是每位选手到台上回答主持人的几个问题,然后根据回答问题的多少获得不同数目的奖品或奖金 ...
- 2016HUAS暑假集训训练2 K - Hero
题目链接:http://acm.hust.edu.cn/vjudge/contest/121192#problem/K 这也是一道贪心题,刚开始写时以为只要对每一敌人的攻击和血的乘积进行从小到大排序即 ...
- 【入门】匈牙利算法+HNOI2006 hero超级英雄
一.关于匈牙利算法 匈牙利算法是由匈牙利数学家Edmonds提出的,用增广路径求二分图最大匹配的算法. 听起来高端,其实说白了就是: 假设不存在单相思(单身狗偷偷抹眼泪),在一个同性恋不合法的国家里( ...
- 2016HUAS_ACM暑假集训2K - Hero(英雄)
这也属于一个贪心题.关键是排序的依据. 这题排序的依据是敌人的伤害/血量(DPS/HP),不难证明,当这个比值相同时,不论先解决谁效果是相同的.思路大部分在注释里. 题目大意: 假设你的血量无限,但是 ...
- bzoj 1191: [HNOI2006]超级英雄Hero
1191: [HNOI2006]超级英雄Hero Time Limit: 10 Sec Memory Limit: 162 MB 二分图匹配... Description 现在电视台有一种节目叫做超 ...
- HDU 4901 The Romantic Hero
The Romantic Hero Time Limit: 3000MS Memory Limit: 131072KB 64bit IO Format: %I64d & %I64u D ...
- HDU4901 The Romantic Hero 计数DP
2014多校4的1005 题目:http://acm.hdu.edu.cn/showproblem.php?pid=4901 The Romantic Hero Time Limit: 6000/30 ...
- HDU 4310 Hero (贪心算法)
A - Hero Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Sta ...
随机推荐
- 어느 도시 보유 하 면 사랑 이다(事態が発生すれば、ある都市の恋はしません)【Si les villes un amour】{If have love in a city}
如果在北京拥有爱情 半夜在簋街喝啤酒 吃小龙虾 在后海的苦情歌声里 搂着你数那四合院的瓦片 如果在上海拥有爱情 去外滩手挽手 吹吹风 坐一下午 去城隍庙尝试各种小吃 嘲笑你嘴角残余的糯米糕 如果在杭州 ...
- Javascript做图片无缝平滑滚动
因需要,google得到.作者不详.多谢.我这里略作修改.只是改变了ID. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitio ...
- 自定义EL函数:小写转大写
1,定义java静态方法,FunctionDemo,有小写转大写的功能 package com.itheima.domain; public class FunctionDemo { public s ...
- PHP实现http与https转化
http://zyan.cc/post/142/ 最近在写PHP程序时,需要使浏览器在https和http之间转化,上网搜索相关信息,无奈只有最近在写PHP程序时,需要使浏览器在https和http之 ...
- [转]Windows Shell 编程 第二章 【来源:http://blog.csdn.net/wangqiulin123456/article/details/7987893】
第二章Shell的结构 “Shell 编程”的大伞之下有大量的API函数和COM接口.这个种类繁多的‘命令’集允许你用不同的方法对Windows Shell进行编程.函数和接口并不是两种提供相同功能 ...
- JavaScript高级程序设计(九):基本概念----函数
一.参数的理解 1.ECMAScript中的参数在内部是用一个数组来表示的.函数接收到的始终是这个数组,而不关心数组中包含多少个参数,即使没有参数也可以. 2.实质上,函数可以通过arguments对 ...
- OC3_MyRect
// // MyRect.h // OC3_MyRect // // Created by zhangxueming on 15/6/9. // Copyright (c) 2015年 zhangxu ...
- [leetcode] 401. Binary Watch
https://leetcode.com/contest/5/problems/binary-watch/ 这个题应该是这次最水的,这个题目就是二进制,然后是10位,最大1024,然后遍历,找二进制里 ...
- W3C盒子与IE盒子模型
盒模型: 内容(content).填充(padding).边界(margin). 边框(border) IE的content部分把 border 和 padding计算了进去 例:一个盒子的 marg ...
- 一台Ubuntu server上安装多实例MySQL
受环境所迫,在一台Ubuntu server上安装多个实例MySQL. 手动安装MySQL 环境:Ubuntu server 11.10 64bit + mysql-5.5.17-linux2.6-x ...