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 ...
随机推荐
- nyoj 412 Same binary weight ()
Same binary weight 时间限制:300 ms | 内存限制:65535 KB 难度:3 描述 The binary weight of a positive integer ...
- lqb 基础练习 01字串 (itoa)
基础练习 01字串 时间限制:1.0s 内存限制:256.0MB 问题描述 对于长度为5位的一个01串,每一位都可能是0或1,一共有32种可能.它们的前几个是: 00000 00001 0 ...
- 队列+BFS(附vector初试)
优先队列的使用: include<queue>//关联头文件 struct node{ int x,y; friend bool operator < (node d1,node d ...
- ubuntu触摸板双指滑动,页面滚动方向
setting——mouse & Touchpad——Natural scrolling 跟我的另一台本子一样了-
- 安卓手机运行fedora
安卓手机使用容器运行其他linux,一般两种: 1. termux + rootfs.img + proot,依赖api>=21,不必root但受限. 2. linuxdeploy + proo ...
- .NET高级特性-Emit(1)
在这个大数据/云计算/人工智能研发普及的时代,Python的崛起以及Javascript的前后端的侵略,程序员与企业似乎越来越青睐动态语言所带来的便捷性与高效性,即使静态语言在性能,错误检查等方面的优 ...
- img标签不能直接作为body的子元素
前几天在一本教材上看到关于HTML标签嵌套规则一节的时候,看到这么一句话,“把图像作为body元素的子元素直接插入到页面中,这样是不妥的,一是结构嵌套有误,二是图像控制不方便.”后面还给了一段代码演示 ...
- centos 生成网卡UUID
在Linux或CentOS中,可以通过如下命令获取网卡的uuid信息: uuidgen 网卡名07d07031-eb0f-4691-8606-befb46645433 查看网卡UUID nmcli c ...
- zip的压缩和解压命令
以下命令均在/home目录下操作cd /home #进入/home目录 1.把/home目录下面的data目录压缩为data.zip zip -r data.zip data #压缩data目录 ...
- ubuntu 16.04源码编译和配置caffe详细教程 | Install and Configure Caffe on ubuntu 16.04
本文首发于个人博客https://kezunlin.me/post/b90033a9/,欢迎阅读! Install and Configure Caffe on ubuntu 16.04 Series ...