题目大意:给你n个点的一棵树, 每个点的权值为2^i ,让你删掉k个点使得剩下的权值和最大。

思路:这题还是比较好想的, 我们反过来考虑, 剩下一个的情况肯定是选第n个点,剩下两个

我们肯定优先考虑第n - 1 个点, 因为其他点全部加起来都没有这个点的权值大, 所以我们可以

以第n个点为根, 倍增出每个点祖先的情况, 然后从后往前贪心, 能取到就取, 不能取到就跳过。

  1. #include<bits/stdc++.h>
  2. #define LL long long
  3. #define fi first
  4. #define se second
  5. #define mk make_pair
  6. #define pii pair<int,int>
  7. #define piii pair<int, pair<int,int>>
  8.  
  9. using namespace std;
  10.  
  11. const int N=1e6 + ;
  12. const int M=1e4 + ;
  13. const int inf = 0x3f3f3f3f;
  14. const LL INF = 0x3f3f3f3f3f3f3f3f;
  15. const int mod = 1e9 + ;
  16.  
  17. int n, k, p[N][];
  18. vector<int> edge[N];
  19. bool in[N];
  20.  
  21. void dfs(int u, int fa) {
  22. p[u][] = fa;
  23. for(int i = ; i < ; i++)
  24. p[u][i] = p[p[u][i - ]][i - ];
  25. for(int v : edge[u]) {
  26. if(v == fa) continue;
  27. dfs(v, u);
  28. }
  29. }
  30.  
  31. int cal(int u, int k) {
  32. for(int i = ; i >= ; i--) {
  33. if(k >= ( << i)) {
  34. k -= << i;
  35. u = p[u][i];
  36. }
  37. }
  38. return u;
  39. }
  40.  
  41. int main() {
  42. scanf("%d%d", &n, &k);
  43. k = n - k;
  44. for(int i = ; i < n; i++) {
  45. int u, v; scanf("%d%d", &u, &v);
  46. edge[u].push_back(v);
  47. edge[v].push_back(u);
  48. }
  49. in[] = true;
  50. dfs(n, );
  51.  
  52. for(int i = n; i >= && k; i--) {
  53. int u = cal(i, k);
  54. if(in[u]) {
  55. int now = i;
  56. while(!in[now]) {
  57. in[now] = true;
  58. now = p[now][];
  59. k--;
  60. }
  61. }
  62. }
  63.  
  64. for(int i = ; i <= n; i++)
  65. if(!in[i]) printf("%d ", i);
  66. return ;
  67. }
  68. /*
  69. */

Codeforces Round #480 (Div. 2) E - The Number Games的更多相关文章

  1. Codeforces Round #427 (Div. 2) B. The number on the board

    引子: A题过于简单导致不敢提交,拖拖拉拉10多分钟还是决定交,太冲动交错了CE一发,我就知道又要错过一次涨分的机会.... B题还是过了,根据题意目测数组大小开1e5,居然蒙对,感觉用vector更 ...

  2. 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 ...

  3. 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 ...

  4. Codeforces Round #491 (Div. 2) E - Bus Number + 反思

    E - Bus Number 最近感觉打CF各种车祸.....感觉要反思一下, 上次读错题,这次想当然地以为18!肯定暴了longlong 而没有去实践, 这个题我看到就感觉是枚举每个数字的个数,但是 ...

  5. Codeforces Round #460 (Div. 2)-B. Perfect Number

    B. Perfect Number time limit per test2 seconds memory limit per test256 megabytes Problem Descriptio ...

  6. 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} ...

  7. Codeforces Round #480 (Div. 2)980C Posterized+分组类贪心

    传送门:http://codeforces.com/contest/980/problem/C 参考 题意:给定n个数字,每个数在0~256间,现在给至多连续k的数分为一组,给出字典序最小的答案. 思 ...

  8. Codeforces Round #480 (Div. 2) C - Posterized

    题目地址:http://codeforces.com/contest/980/problem/C 官方题解: 题解:一共256个像素网格,可以把这个256个分组,每个分组大小<=k.给出n个像素 ...

  9. Codeforces Round #480 (Div. 2) B. Marlin

    题目地址:http://codeforces.com/contest/980/problem/B 官方题解: 题意: 有一个城市有4行n列,n是奇数,有一个村庄在(1,1),村民在(4,n)钓鱼:还有 ...

随机推荐

  1. SpringBoot入坑指南之六:使用过滤器或拦截器

    在Web应用中,常常存在拦截全部或部分请求进行统一处理的应用场景,如权限校验.参数校验.性能监控等. 在SpringMVC框架中,我们可以通过过滤器或拦截器实现相关功能,spring-boot-sta ...

  2. NetBeans无法使用svn相关功能

    本来中NetBeans使用subversion功能只要下载过subversion客户端就可以直接使用了,我也一直是这样的,可是今天忽然的不能用了,发现问题是因为我在文件中使用subversion的比较 ...

  3. 3分钟读懂移动端rem使用方法

    1.为什么要用rem 博客很久没写了,原因很简单. 最近接手了一个项目,要同时做PC和移动端的页面,之前没接触过,但毕竟给钱的是大爷,所以还是硬着头皮上了. 移动端最麻烦的是什么? 不同分辨率适配! ...

  4. 使用 yield 减少内存消耗

    php 里面想要处理一个文本文件,有一个方法是使用 file() 函数,但是这个函数会读取文件所有内容,可能会导致占用很大内存. // 28.1 M 的文本文件, 200w 行 $file = 'st ...

  5. vim使用入门设置

    分为以下四步. 1,安装vim 2,安装git yum -y install vim git (Fedora/CentOS) /apt-get install vim git (Debian/Ubun ...

  6. 如何用javascript获取和设置css3属性

    ==================获取======================== 我想到的第一个思路 var test = document.getElementById('test'); c ...

  7. Java SSM框架之MyBatis3(五)MyBatis之ResultMap详解

    resultMap是Mybatis最强大的元素,它可以将查询到的复杂数据(比如查询到几个表中数据)映射到一个结果集当中. resultMap包含的元素: <!--column不做限制,可以为任意 ...

  8. [Alg] 尺取法

    尺取法是在线性结构中进行搜寻满足某一条件的区间的方法. 该方法保存两个索引--首索引begin.尾索引end.判断 [begin, end] 区间是否满足条件. 移动 [begin, end] 区间的 ...

  9. 安装odbc驱动

    1.下载对应的驱动 (32位/64位) http://www.oracle.com/technetwork/database/database-technologies/instant-client/ ...

  10. linux usb枚举过程分析之守护进程及其唤醒【转】

    转自:http://blog.csdn.net/xuelin273/article/details/38646765 usb热插拔,即usb设备可以实现即插即用,像U盘一样,插到电脑里就可以用,不用时 ...