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)钓鱼:还有 ...
随机推荐
- 【bzoj2194】快速傅立叶之二 FFT
题意:给定序列a,b,求序列c,\(c(k)=\sum_{i=k}^{n-1}a(i)b(i-k)\) Solution: \[ c(k)=\sum_{i=k}^{n-1}a(i)b(i-k)\\ c ...
- HGOI20180822 五校联考卷
T1 [题目意思]给出下列程序片段,预测程序运行结果 输入文件为T(T<=200)组数据,每组数据有个n(n<=1014) 输出文件为T行,每行一个数据,表示fun(n)的值 simple ...
- RK哈希(Rabin_Karp 哈希)
Rabin_Karp 哈希通过比较hash值是否相等来比较每个字符串是否相等有概率出错(很小)字符串x1,x2,x3……xk基底e;模数mo;hash=(xk*e^0+xk-1*e^1+......+ ...
- ASP.NET MVC —— Model之一模型模板
http://www.cnblogs.com/lzhp/archive/2013/03/25/2981650.html Mvc model系列文章主要分为三部分:Model Templates,Mod ...
- spectrogram函数做短时傅里叶分析
整理自:http://blog.sina.com.cn/s/blog_6163bdeb0102dwfw.html 今天偶人发现原来matlab自带了短时傅里叶变换的分析函数,老版本的matlab是sp ...
- hdu 6166 Senior Pan
http://acm.hdu.edu.cn/showproblem.php?pid=6166 题意: 给出一张无向图,给定k个特殊点 求这k个特殊点两两之间的最短路 二进制分组 枚举一位二进制位 这一 ...
- Linux命令(一)grep查询
grep -n as test1.txt -n : 显示行号 -v: 显示没有搜索字符的一行 -i:忽视大小写 搜索字符串 模式查找
- Spring 学习02
一.上节内容回顾 1 spring的概念 (1)核心:ioc和aop (2)spring一站式框架 2 spring的bean管理(xml) (1)bean实例化 (2)注入属性 (3)注入对象属性 ...
- MongoDB-3.4搭建副本集
搭建副本集 1:首先创建3台虚拟机作为配置环境 IP1:192.168.101.175 IP2:192.168.101.176 IP3:192.168.101.177 2.下载MongoDB 3.4版 ...
- [转载]Frontend Knowledge Structure
https://github.com/JacksonTian/fks http://code.csdn.net/news/2819224 本文为大家整理了一系列关于JavaScript的常用工具,包括 ...