CodeForces 980 E The Number Games
The Number Games
题意:一共有n个点, n-1条路,第i个点有2^i个人, 然后需要删除k个点,现在需要删除k个点之后,剩下的图为连通图, 并且剩下的人数最多。
题解:我们可以发现如果删除最大的那个点 比 删除其他的所有点损失的人数还要多, 所以我们要贪心的留下标号大的点。我们将删除点的问题转变成保留(n-k)个点的问题,然后先保留标号为n的点,然后以n位根节点dfs建树。
然后再从大到小考虑, 如果要保留第i个点, 第i个点往上走, 走到已经保留的点途经的城市数是不是大于或者等于现在还能保留的城市数目。 如果是就将路上的所有的点都保留下来, 就处理下一个点, 如果数目不够, 就处理下一个点。
因为如果每次往上走k步很容易TLE, 所以我们用倍增法优化一下往上走的过程。然后处理完所有的点得到答案了。
代码:
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout); 4 #define LL long long 5 #define ULL unsigned LL 6 #define fi first 7 #define se second 8 #define pb push_back 9 #define lson l,m,rt<<1 10 #define rson m+1,r,rt<<1|1 11 #define max3(a,b,c) max(a,max(b,c)) 12 #define min3(a,b,c) min(a,min(b,c)) 13 typedef pair<int,int> pll; 14 const int INF = 0x3f3f3f3f; 15 const LL mod = 1e9+7; 16 const int N = 1e6+10; 17 vector<int> son[N]; 18 int vis[N]; 19 int anc[N][21]; 20 bool can[N]; 21 void dfs(int o,int u){ 22 for(int i = 0; i < son[u].size(); i++){ 23 int v = son[u][i]; 24 if(o == v) continue; 25 anc[v][0] = u; 26 for(int i = 1; i <= 20; i++) anc[v][i] = anc[anc[v][i-1]][i-1]; 27 dfs(u,v); 28 } 29 } 30 bool check(int x, int k){ 31 for(int i = 20; i >= 0; i--){ 32 if((k >> i)&1) x = anc[x][i]; 33 } 34 return can[x]; 35 } 36 int main(){ 37 int n, u, v, k; 38 scanf("%d%d", &n, &k); 39 for(int i = 1; i < n; i++){ 40 scanf("%d%d", &u, &v); 41 son[u].pb(v); 42 son[v].pb(u); 43 } 44 for(int i = 0; i <= 20; i++) anc[n][i] = n; 45 dfs(n,n); 46 k = n - k - 1; 47 can[n] = 1; 48 for(int i = n - 1; i >= 1; i--){ 49 if(check(i,k)){ 50 int t = i; 51 while(!can[t]){ 52 can[t] = 1; 53 t = anc[t][0]; 54 k--; 55 } 56 } 57 } 58 for(int i = 1; i <= n; i++) 59 if(!can[i]) printf("%d ", i); 60 return 0; 61 }
View Code
转载于:https://www.cnblogs.com/MingSD/p/9022166.html
CodeForces 980 E The Number Games相关推荐
- CodeForces - 416A Guess a number
A. Guess a number! time limit per test1 second memory limit per test256 megabytes A TV show called & ...
- Codeforces 455B A Lot of Games(字典树+博弈)
题目连接: Codeforces 455B A Lot of Games 题目大意:给定n,表示字符串集合.给定k,表示进行了k次游戏,然后是n个字符串.每局开始,字符串为空串,然后两人轮流在末尾追加 ...
- Codeforces 724 G Xor-matic Number of the Graph 线性基+DFS
G. Xor-matic Number of the Graph http://codeforces.com/problemset/problem/724/G 题意:给你一张无向图.定义一个无序三元组 ...
- CodeForces - 1560F2 Nearest Beautiful Number (hard version)(二分+数位dp)
题目链接:点击查看 题目大意:给出一个十进制数字 nnn 和一个约束 kkk,问大于等于 nnn 且满足不同的数位个数小于等于 kkk 的最小的数字是多少 题目分析:自己写的贪心太丑了,就不放上来丢人 ...
- CodeForces 389A Fox and Number Game
链接:http://codeforces.com/problemset/problem/389/A Fox and Number Game time limit per test:1 second m ...
- Codeforces 1070A Find a Number(BFS) 2018-2019 ICPC, NEERC, Southern Subregional Contest Problem A
Description You are given two positive integers ddd and sss. Find minimal positive integer nnn which ...
- 【Codeforces 1426 F】Number of Subsequences,字符串计数DP
problem F. Number of Subsequences time limit per test1 second memory limit per test256 megabytes inp ...
- 【Codeforces】835B The number on the board (贪心)
把所有字符串上的数字加起来,看是否超过k,没有超过k的话,把字符串sort,从第一位开始,将字符变成9,直到sum大于等于k为止. #include <iostream> #include ...
- CodeForces 518A - Chewbaсca and Number(思路)
题意:给你两个长度相同的字符串,输出一个长度与它俩相同,且字典序位于两者之间(不可与其中一者相同)的字符串,若不存在则输出"No such string".(输入保证字典序第一个严 ...
- CodeForces 392C Yet Another Number Sequence 矩阵快速幂
题意: \(F_n\)为斐波那契数列,\(F_1=1,F_2=2\). 给定一个\(k\),定义数列\(A_i=F_i \cdot i^k\). 求\(A_1+A_2+ \cdots + A_n\). ...
最新文章
- AutoFac Ioc依赖注入容器
- 著名IT公司名字来由
- mapinfo在线地图插件_地图数据获取|2、CAD地图获取
- 【Android 逆向】启动 DEX 字节码中的 Activity 组件 ( 在 PathClassLoader 和 BootClassLoader 之间插入 DexClassLoader )
- 泊松分布的分布函数_10分钟了解泊松分布
- vector父类类型可以存放子类吗_拼夕夕三轮面经:被问到反射和泛型的bug,你踏空了吗?...
- 惠普136nw打印机清零_惠普136nw打印机清零_HP惠普打印机清零大全
- matlab实现最速下降法
- pantum打印机驱动安装m6506_奔图m6506nw驱动下载
- 上传文件,路径为C:\fakepath\的问题(待跟进)
- 最新版银灿如意烧AIBurn V2.0.0.1(支持IS903B/IS902E/IS916主控)
- python读二进制文件
- (原创推荐文章)kerberos服务器端与客户端
- Echarts显示行政区域图并上色
- Python人脸微笑识别2-----Ubuntu16.04基于Tensorflow卷积神经网络模型训练的Python3+Dlib+Opencv实现摄像头人脸微笑检测
- JS解决因循环绑定click事件失效
- qq邮箱993服务器地址,ios邮箱绑定qq邮箱提示993服务器连接超时
- java对图片进行压缩处理
- ffmpeg源码分析-parse_optgroup
- Ruby学习-安装、升级Ruby菜鸟教程(Linux环境下)
热门文章
- SpringBoot中级篇-SpringBatch+Oracle配置
- oracle如何修改initial参数,oracle初始化参数设置
- 如何在小程序wxml文件中编写js代码
- redis依赖包_解决痛点:二方包稳定性测试实践
- 解决虚拟机内服务器卡顿,不流畅问题
- 加服务器虚拟机软件,服务器虚拟机软件
- Echarts单条折线可拖拽
- Yii2修改默认控制器
- html页面 request,HTML DOM requestFullscreen() 方法
- 编译ffmpeg:no match insn: lbu $r16,0($r5)