HDU 5121 Just A Mistake
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
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.
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.
(the expected size of independent set) × N! mod (109 + 7)
4
1 2
1 3
1 4
3
1 2
2 3
Case #2: 10
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.
#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的更多相关文章
- 动态规划(DP计数):HDU 5121 Just A Mistake
As we all know, Matt is an outstanding contestant in ACM-ICPC. Graph problems are his favorite.Once, ...
- ZROI week3
作业 poj 1091 跳蚤 容斥原理. 考虑能否跳到旁边就是卡牌的\(gcd\)是否是1,可以根据裴蜀定理证明. 考虑正着做十分的麻烦,所以倒着做,也就是用\(M^N - (不合法)\)即可. 不合 ...
- HDU 4293---Groups(区间DP)
题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=4293 Problem Description After the regional con ...
- hdu 3007 Buried memory 最远点对
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3007 Each person had do something foolish along with ...
- hdu 5625
Clarke and chemistry Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
随机推荐
- Springboot中的缓存Cache和CacheManager原理介绍
背景理解 什么是缓存,为什么要用缓存 程序运行中,在内存保持一定时间不变的数据就是缓存.简单到写一个Map,里面放着一些key,value数据,就已经是个缓存了 所以缓存并不是什么高大上的技术,只是个 ...
- 深入理解计算机系统 第三章 程序的机器级表示 part2
这周由于时间和精力有限,只读一小节:3.4.4 压入和弹出栈数据 栈是一种特殊的数据结构,遵循“后进先出”的原则,可以用数组实现,总是从数组的一端插入和删除元素,这一端被称为栈顶. 栈有两个常用指令 ...
- lqb 入门训练 Fibonacci数列 (循环 PS:提柜要栈溢出)
入门训练 Fibonacci数列 时间限制:1.0s 内存限制:256.0MB 问题描述 Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1. 当n比较大时, ...
- 工作日志,error parsing query: unable to find time zone
工作日志,error parsing query: unable to find time zone 坑 Windows 系统使用influxdb数据库,在执行查询语句时提示 ERR: error p ...
- ubuntu server 1604 关机和重启
命令有很多,记住以下两三个就够了 重启: sudo reboot (这个短,易记) sudo shutdown -r now 统一的shutdown形式 关机:sudo shutdown -P no ...
- deepin安装pip
sudo apt install python3-venv python3-pip 升级最新版 pip3 install --upgrade pip 更新完以后就报错网上的解决办法没有好使的 退回版 ...
- [Odoo12基础教程]之第零篇-win中odoo12环境搭建
所需材料 1.python3.7 2.pycharm社区版及以上 3.postgresSQL10 下载链接:https://www.enterprisedb.com/thank-you-downloa ...
- node后台初始配置(2)
一.node-app结构 创建成功node-app项目后,会自动生成一些文件一般初始的结构如下图 在bin文件夹里面只有一个文件www var port = normalizePort(pr ...
- linux 相关零碎知识整理
1.启动bash shell 大部分linux系统启动用户命令行接口(cli)环境时使用默认的bash shell,在bash shell启动时,它将自动执行位于用户主目录下的.bashrc中的命令. ...
- C#学习笔记03--循环和一维数组
一.循环(重点) 什么时候用循环? 想让一段代码执行多次, 这段代码可能不一样但是一定有一个规律. 1.while 循环 格式: while(循环条件) { 循环执行的代码; } 循环的机制: 当 ...