Codeforces Round #480 (Div. 2) E - The Number Games
题目大意:给你n个点的一棵树, 每个点的权值为2^i ,让你删掉k个点使得剩下的权值和最大。
思路:这题还是比较好想的, 我们反过来考虑, 剩下一个的情况肯定是选第n个点,剩下两个
我们肯定优先考虑第n - 1 个点, 因为其他点全部加起来都没有这个点的权值大, 所以我们可以
以第n个点为根, 倍增出每个点祖先的情况, 然后从后往前贪心, 能取到就取, 不能取到就跳过。
- #include<bits/stdc++.h>
- #define LL long long
- #define fi first
- #define se second
- #define mk make_pair
- #define pii pair<int,int>
- #define piii pair<int, pair<int,int>>
- using namespace std;
- const int N=1e6 + ;
- const int M=1e4 + ;
- const int inf = 0x3f3f3f3f;
- const LL INF = 0x3f3f3f3f3f3f3f3f;
- const int mod = 1e9 + ;
- int n, k, p[N][];
- vector<int> edge[N];
- bool in[N];
- void dfs(int u, int fa) {
- p[u][] = fa;
- for(int i = ; i < ; i++)
- p[u][i] = p[p[u][i - ]][i - ];
- for(int v : edge[u]) {
- if(v == fa) continue;
- dfs(v, u);
- }
- }
- int cal(int u, int k) {
- for(int i = ; i >= ; i--) {
- if(k >= ( << i)) {
- k -= << i;
- u = p[u][i];
- }
- }
- return u;
- }
- int main() {
- scanf("%d%d", &n, &k);
- k = n - k;
- for(int i = ; i < n; i++) {
- int u, v; scanf("%d%d", &u, &v);
- edge[u].push_back(v);
- edge[v].push_back(u);
- }
- in[] = true;
- dfs(n, );
- for(int i = n; i >= && k; i--) {
- int u = cal(i, k);
- if(in[u]) {
- int now = i;
- while(!in[now]) {
- in[now] = true;
- now = p[now][];
- k--;
- }
- }
- }
- for(int i = ; i <= n; i++)
- if(!in[i]) printf("%d ", i);
- return ;
- }
- /*
- */
Codeforces Round #480 (Div. 2) E - The Number Games的更多相关文章
- Codeforces Round #427 (Div. 2) B. The number on the board
引子: A题过于简单导致不敢提交,拖拖拉拉10多分钟还是决定交,太冲动交错了CE一发,我就知道又要错过一次涨分的机会.... B题还是过了,根据题意目测数组大小开1e5,居然蒙对,感觉用vector更 ...
- Codeforces Round #585 (Div. 2) B. The Number of Products(DP)
链接: https://codeforces.com/contest/1215/problem/B 题意: You are given a sequence a1,a2,-,an consisting ...
- Codeforces Round #411 div 2 D. Minimum number of steps
D. Minimum number of steps time limit per test 1 second memory limit per test 256 megabytes input st ...
- Codeforces Round #491 (Div. 2) E - Bus Number + 反思
E - Bus Number 最近感觉打CF各种车祸.....感觉要反思一下, 上次读错题,这次想当然地以为18!肯定暴了longlong 而没有去实践, 这个题我看到就感觉是枚举每个数字的个数,但是 ...
- Codeforces Round #460 (Div. 2)-B. Perfect Number
B. Perfect Number time limit per test2 seconds memory limit per test256 megabytes Problem Descriptio ...
- Codeforces Round #209 (Div. 2) C - Prime Number
传送门 题意 给出n个数及x,求 \[\frac{\sum _{i=1}^n x^{a_1+a_2+...+a_{i-1}+a_{i+1}+...a_n}}{\prod_{i=1}^n x^{a_i} ...
- Codeforces Round #480 (Div. 2)980C Posterized+分组类贪心
传送门:http://codeforces.com/contest/980/problem/C 参考 题意:给定n个数字,每个数在0~256间,现在给至多连续k的数分为一组,给出字典序最小的答案. 思 ...
- Codeforces Round #480 (Div. 2) C - Posterized
题目地址:http://codeforces.com/contest/980/problem/C 官方题解: 题解:一共256个像素网格,可以把这个256个分组,每个分组大小<=k.给出n个像素 ...
- Codeforces Round #480 (Div. 2) B. Marlin
题目地址:http://codeforces.com/contest/980/problem/B 官方题解: 题意: 有一个城市有4行n列,n是奇数,有一个村庄在(1,1),村民在(4,n)钓鱼:还有 ...
随机推荐
- SpringBoot入坑指南之六:使用过滤器或拦截器
在Web应用中,常常存在拦截全部或部分请求进行统一处理的应用场景,如权限校验.参数校验.性能监控等. 在SpringMVC框架中,我们可以通过过滤器或拦截器实现相关功能,spring-boot-sta ...
- NetBeans无法使用svn相关功能
本来中NetBeans使用subversion功能只要下载过subversion客户端就可以直接使用了,我也一直是这样的,可是今天忽然的不能用了,发现问题是因为我在文件中使用subversion的比较 ...
- 3分钟读懂移动端rem使用方法
1.为什么要用rem 博客很久没写了,原因很简单. 最近接手了一个项目,要同时做PC和移动端的页面,之前没接触过,但毕竟给钱的是大爷,所以还是硬着头皮上了. 移动端最麻烦的是什么? 不同分辨率适配! ...
- 使用 yield 减少内存消耗
php 里面想要处理一个文本文件,有一个方法是使用 file() 函数,但是这个函数会读取文件所有内容,可能会导致占用很大内存. // 28.1 M 的文本文件, 200w 行 $file = 'st ...
- vim使用入门设置
分为以下四步. 1,安装vim 2,安装git yum -y install vim git (Fedora/CentOS) /apt-get install vim git (Debian/Ubun ...
- 如何用javascript获取和设置css3属性
==================获取======================== 我想到的第一个思路 var test = document.getElementById('test'); c ...
- Java SSM框架之MyBatis3(五)MyBatis之ResultMap详解
resultMap是Mybatis最强大的元素,它可以将查询到的复杂数据(比如查询到几个表中数据)映射到一个结果集当中. resultMap包含的元素: <!--column不做限制,可以为任意 ...
- [Alg] 尺取法
尺取法是在线性结构中进行搜寻满足某一条件的区间的方法. 该方法保存两个索引--首索引begin.尾索引end.判断 [begin, end] 区间是否满足条件. 移动 [begin, end] 区间的 ...
- 安装odbc驱动
1.下载对应的驱动 (32位/64位) http://www.oracle.com/technetwork/database/database-technologies/instant-client/ ...
- linux usb枚举过程分析之守护进程及其唤醒【转】
转自:http://blog.csdn.net/xuelin273/article/details/38646765 usb热插拔,即usb设备可以实现即插即用,像U盘一样,插到电脑里就可以用,不用时 ...