Just A Mistake

Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)
Total Submission(s): 168    Accepted Submission(s): 41

Problem Description
As we all know, Matt is an outstanding contestant in ACM-ICPC. Graph problems are his favorite.

Once, he came up with a simple algorithm for finding the maximal independent set in trees by mistake.

A tree is a connected undirected graph without cycles, and an independent set is subset of the vertex set which contains no adjacent vertex pairs.

Suppose that the tree contains N vertices, conveniently numbered by 1,2, . . . , N. First, Matt picks a permutation p1, p2, . . . , pN of {1, 2, 3, . . . , N } randomly and uniformly.

After picking the permutation, Matt does the following procedure.

1.Set S = .
2.Consider the vertex p1, p2, . . . , pN accordingly. For vertex pi, if and only if there is no vertex in S which is adjacent to pi, add vertex pi into S.
3.Output the set S.

Clearly the above algorithm does not always output the maximal independent set. Matt would like to know the expected size of set S instead.

 
Input
The first line contains only one integer T , which indicates the number of test cases.

For each test case, the first line contains an integer N (1 ≤ N ≤ 200), indicating the number of vertices in the graph.

Each of the following N - 1 lines contains two integers u, v (1 ≤ u, v ≤ N ) indicating an edge between u and v. You may assume that all the vertices are connected.

 
Output
For each test case, output a single line “Case #x: y”, where x is the case number (starting from 1) and y is the answer. To avoid rounding error, the answer you should output is:

(the expected size of independent set) × N! mod (109 + 7)

 
Sample Input
2
4
1 2
1 3
1 4
3
1 2
2 3
 
Sample Output
Case #1: 60
Case #2: 10

Hint

In the first sample, there are 4 vertices, so there are 4! permutations Matt may get.
Suppose the permutation Matt gets is 1 2 3 4. He will add vertex 1 into the independent set.
Suppose the permutation Matt gets is 2 1 3 4. He will add vertex 2, vertex 3 and vertex 4 into the independent set.
It is obvious that if the first element in the permutation is not vertex 1, he will get an independent set whose size is 3. Otherwise, he well get an independent set whose size is 1. Since there are 18
permutations whose first element is not vertex 1, the answer in the first sample is (3 × 18 + 1 × 6) mod (10^9 + 7) = 60.

 
Source
 
Recommend
liuyiding
签到题:
参考代码:
 #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + ;
int a[maxn]; int main()
{
int t;
scanf("%d", &t);
for(int k = ; k <= t; k++)
{
int n;
scanf("%d", &n);
for(int i = ; i <= n; i++)
scanf("%d", &a[i]);
int min_a = a[n], ans = ;
for(int i = n - ; i > ; i--)
{
if(a[i] > min_a) ans++;
else min_a = a[i];
}
printf("Case #%d: %d\n", k, ans);
}
return ;
}

  

HDU 5121 Just A Mistake的更多相关文章

  1. 动态规划(DP计数):HDU 5121 Just A Mistake

    As we all know, Matt is an outstanding contestant in ACM-ICPC. Graph problems are his favorite.Once, ...

  2. ZROI week3

    作业 poj 1091 跳蚤 容斥原理. 考虑能否跳到旁边就是卡牌的\(gcd\)是否是1,可以根据裴蜀定理证明. 考虑正着做十分的麻烦,所以倒着做,也就是用\(M^N - (不合法)\)即可. 不合 ...

  3. HDU 4293---Groups(区间DP)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=4293 Problem Description After the regional con ...

  4. hdu 3007 Buried memory 最远点对

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3007 Each person had do something foolish along with ...

  5. hdu 5625

    Clarke and chemistry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  6. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  7. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  8. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  9. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

随机推荐

  1. css的块级元素和行级元素

    块级元素 概念: 每个块级元素都是独自占一行.  元素的高度.宽度.行高和边距都是可以设置的.   元素的宽度如果不设置的话,默认为父元素的宽度(父元素宽度100%) <address>/ ...

  2. 010.Kubernetes二进制部署kube-controller-manager

    一 部署高可用kube-controller-manager 1.1 高可用kube-controller-manager介绍 本实验部署一个三实例 kube-controller-manager 的 ...

  3. Eclipse中修改tomcat的部署路径deploypath

    在eclipse上面部署web项目后,它没有将你的项目文件放到tomcat 的目录下面.而是放在了你的工作目录下面. 在tomcat上右键选择“stop” .停止eclipse内的Tomcat服务器 ...

  4. ASP.NET Core 1.0: API的输入参数

    Web API是需要接受参数的,譬如,通常用于创建数据的POST method需要接受输入数据,而用于GET method也需要接受一些可选参数,譬如:为了性能起见,控制返回数据的数量是至关重要的. ...

  5. JS简单循环遍历json数组的方法

    例如数据库里面的json字符串是这样的 1 2 3 4 5 var str = '[{"name":"宗2瓜","num":"1& ...

  6. Python 面向对象-上篇

    概述 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类和封装,让开发“更快更好更强...” 面向过程编程最易被初学 ...

  7. thinkphp6.0 开启调试模式以及Driver [Think] not supported

    thinkphp6.0 开启调试模式 首先确认自己是通过 composer 进行的下载,然后修改系统目录下的 .example.env 为 .env 文件 修改 config->app.php ...

  8. 详解Redis RDB持久化、AOF持久化

    1.持久化 1.1 持久化简介 持久化(Persistence),持久化是将程序数据在持久状态和瞬时状态间转换的机制,即把数据(如内存中的对象)保存到可永久保存的存储设备中(如磁盘). 1.2 red ...

  9. python:time模块

    (鱼c)time模块详解http://bbs.fishc.com/forum.php?mod=viewthread&tid=51326&extra=page%3D1%26filter% ...

  10. 扛把子组Scrum立会报告+燃尽图 07

    此作业要求参见https://edu.cnblogs.com/campus/nenu/2019fall/homework/8684 一.小组情况组长:迟俊文组员:宋晓丽 梁梦瑶 韩昊 刘信鹏队名:扛把 ...