题目大意:给你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的更多相关文章

  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. 【题解】 bzoj2115: [Wc2011] Xor (线性基+dfs)

    bzoj2115,戳我戳我 Solution: 看得题解(逃,我太菜了,想不出这种做法 那么丢个链接 Attention: 板子别写错了 又写错了这次 \(long long\)是左移63位,多了会溢 ...

  2. 面试题:get和post的本质区别

    前言:相信小伙伴们面试时候一定都遇到过这个问题,即使没有遇到过,至少也听说过,网上资料一大片,大概每个人都能说出来一些.但是总感觉面试装逼不成功,所以就翻阅了部分资料,进一步整理了下. 一般当我们提到 ...

  3. bzoj千题计划260:bzoj2940: [Poi2000]条纹

    http://www.lydsy.com/JudgeOnline/problem.php?id=2940 SG 博弈入门推荐张一飞的<由感性认识到理性认识 ——透析一类搏弈游戏的解答过程> ...

  4. prefab内容分析

    写在前面: 当前使用的unity版本:5.3.7p4. 如果打开prefab文件是乱码: 把editer的asset Srialization改为Force Text即可. 一.什么是Prefab P ...

  5. 【学习笔记】AspectJ笔记

    AspectJ的概念 是一种静态编译期增强性AOP的实现 在编译过程中修改代码加入相关逻辑,无需程序员动手 AspectJ具体用法 下载安装AspectJ,启动jar文件,安装到JDK目录,添加pat ...

  6. H5 Day2 练习

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. JavaScript编写风格指南 (一)

    //参考<编写可维护的Javascript>  一:缩进// 第一行的层级由4个空格组成,避免使用制表符tab进行缩进 //好的写法if (true) {    doSomething() ...

  8. JavaScript Cookies取值

    http://www.w3school.com.cn/js/js_cookies.asp

  9. python的__get__、__set__、__delete__(1)

    内容:    描述符引导        摘要        定义和介绍        描述符协议        调用描述符        样例        Properties        函数和 ...

  10. PyQT5 No module named ‘PyQt5.QtWebEngineWidgets’

    PyQT5查找不到模块QtWebEngineWidgets pip install pyqt5==5.10.1 或 安装64位的Pyhon解释器