Codeforces Round #480 (Div. 2) E. The Number Games

E. The Number Games

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

The nation of Panel holds an annual show called The Number Games, where each district in the nation will be represented by one contestant.

The nation has nn districts numbered from 11 to nn, each district has exactly one path connecting it to every other district. The number of fans of a contestant from district ii is equal to 2i2i.

This year, the president decided to reduce the costs. He wants to remove kk contestants from the games. However, the districts of the removed contestants will be furious and will not allow anyone to cross through their districts.

The president wants to ensure that all remaining contestants are from districts that can be reached from one another. He also wishes to maximize the total number of fans of the participating contestants.

Which contestants should the president remove?

Input

The first line of input contains two integers nn and kk (1≤k<n≤1061≤k<n≤106) — the number of districts in Panel, and the number of contestants the president wishes to remove, respectively.

The next n?1n?1 lines each contains two integers aa and bb (1≤a,b≤n1≤a,b≤n, a≠ba≠b), that describe a road that connects two different districts aaand bb in the nation. It is guaranteed that there is exactly one path between every two districts.

Output

Print kk space-separated integers: the numbers of the districts of which the contestants should be removed, in increasing order of district number.

Examples

input

Copy

6 32 12 64 25 62 3

output

Copy

1 3 4

input

Copy

8 42 62 77 81 23 12 47 5

output

Copy

1 3 4 5

Note

In the first sample, the maximum possible total number of fans is 22+25+26=10022+25+26=100. We can achieve it by removing the contestants of the districts 1, 3, and 4.

思路:贪心留最大的,树上倍增求出花费,若可行,暴力更新路径上所有未留下的点,否则判断下一个。

  1 #include <iostream>
  2 #include <fstream>
  3 #include <sstream>
  4 #include <cstdlib>
  5 #include <cstdio>
  6 #include <cmath>
  7 #include <string>
  8 #include <cstring>
  9 #include <algorithm>
 10 #include <queue>
 11 #include <stack>
 12 #include <vector>
 13 #include <set>
 14 #include <map>
 15 #include <list>
 16 #include <iomanip>
 17 #include <cctype>
 18 #include <cassert>
 19 #include <bitset>
 20 #include <ctime>
 21
 22 using namespace std;
 23
 24 #define pau system("pause")
 25 #define ll long long
 26 #define pii pair<int, int>
 27 #define pb push_back
 28 #define mp make_pair
 29 #define clr(a, x) memset(a, x, sizeof(a))
 30
 31 const double pi = acos(-1.0);
 32 const int INF = 0x3f3f3f3f;
 33 const int MOD = 1e9 + 7;
 34 const double EPS = 1e-9;
 35
 36 /*
 37 #include <ext/pb_ds/assoc_container.hpp>
 38 #include <ext/pb_ds/tree_policy.hpp>
 39
 40 using namespace __gnu_pbds;
 41 tree<pli, null_type, greater<pli>, rb_tree_tag, tree_order_statistics_node_update> T;
 42 */
 43
 44 int n, k, d[1000015], vis[1000015], p[1000015];
 45 vector<int> e[1000015], e1[1000015];
 46 int f[1000015][25];
 47
 48 void dfs(int x) {
 49     vis[x] = 1;
 50     for (int i = 0; i < e[x].size(); ++i) {
 51         int y = e[x][i];
 52         if (vis[y]) p[x] = y;
 53         else {
 54             e1[x].pb(y);
 55             dfs(y);
 56         }
 57     }
 58 }
 59
 60 void dfs2(int x) {
 61     f[x][0] = p[x];
 62     for (int i = 1; i <= 20; ++i) {
 63         f[x][i] = f[f[x][i - 1]][i - 1];
 64     }
 65     for (int i = 0; i < e1[x].size(); ++i) {
 66         int y = e1[x][i];
 67         dfs2(y);
 68     }
 69 }
 70 vector<int> ans;
 71 bool out[1000015];
 72 void solve() {
 73     clr(vis, 0); vis[0] = 1;
 74     int now = n;
 75     while (k) {
 76         if (vis[now]) {
 77             --now;
 78             continue;
 79         }
 80         int cnt = 1;
 81         for (int h = 0, i = now; ; ) {
 82             while (!vis[f[i][h]]) {
 83                 ++h;
 84             }
 85             while (~h && vis[f[i][h]]) {
 86                 --h;
 87             }
 88             if (~h) {
 89                 cnt += 1 << h;
 90                 i = f[i][h];
 91             } else {
 92                 break;
 93             }
 94         }
 95         if (cnt <= k) {
 96             int i = now;
 97             while (!vis[i]) {
 98                 ans.pb(i);
 99                 vis[i] = 1;
100                 i = p[i];
101             }
102             k -= cnt;
103         }
104         --now;
105     }
106     for (int i = 0; i < ans.size(); ++i) {
107         out[ans[i]] = 1;
108     }
109     for (int i = 1; i <= n; ++i) {
110         if (!out[i]) printf("%d ", i);
111     }
112 }
113 int main() {
114     scanf("%d%d", &n, &k);
115     k = n - k;
116     for (int i = 1; i < n; ++i) {
117         int a, b;
118         scanf("%d%d", &a, &b);
119         e[a].pb(b), e[b].pb(a);
120     }
121     dfs(n);
122     dfs2(n);
123     solve();
124     return 0;
125 }

原文地址:https://www.cnblogs.com/BIGTOM/p/9024135.html

时间: 2024-11-06 07:24:39

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

Codeforces Round #480 (Div. 2) C 贪心 D 数字、思维 E 树上倍增

Codeforces Round #480 (Div. 2) C. Posterized 题意: 给出 n 个数,都是区间 [0,255] 内的数,要你把 [0,255] 划分成多个长度 <=k 的不重叠的子区间.每个数必须包含在一个子区间内,且这个数的价值是这个子区间的左端点.要你输出这 n 数的价值,且这 n 个价值字典序要最小. tags: 首先很明显字典序最小,那对于第 i 个数 p[i] 定它的区间时,左端点肯定要尽可能小.所以我们直接枚举区间 [ p[i]-k+1, p[i] ] 定

Codeforces Round #411 (Div. 2)D. Minimum number of steps(贪心)

传送门 Description We have a string of letters 'a' and 'b'. We want to perform some operations on it. On each step we choose one of substrings "ab" in the string and replace it with the string "bba". If we have no "ab" as a subs

Codeforces Round #480 (Div. 2)

这是<2100参加div2 only制度的第一场 然而3大号都打不了(都>2200) 随便抓了只黑名小号来打 A 如果珍珠个数为0或串长约数即可 00:03:18 B 一开始脑子糊涂了 以为有的时候不行 还wa了一发 事实上 左下到右上等价于右上到左下 沿竖着的对称轴对称放障碍即可 00:16:38 C 贪心 00:27:11 D 把每个数除去所有平方因子 相同的一组 从n个点开始向右扫一遍统计即可 注意0放入任何其他组不不影响 00:37:20 E 我们考虑留下哪些数 k<n n号节点

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

E - Bus Number 最近感觉打CF各种车祸.....感觉要反思一下, 上次读错题,这次想当然地以为18!肯定暴了longlong 而没有去实践, 这个题我看到就感觉是枚举每个数字的个数,但是我觉得算得时候会爆longlong 就想用大数,但是我去看别人交的全部都是C++,就感觉是不是有别的方法, 想了半天感觉时间来不及了就强行上了个java,结果时间来不及... 以后写题首先要读清楚题目,其次不要想当然,要去实践!!!!!!!!!!! 真的很烦. import java.math.Bi

Codeforces Round #460 (Div. 2) B Perfect Number(二分+数位dp)

题目传送门 B. Perfect Number time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output We consider a positive integer perfect, if and only if the sum of its digits is exactly 1010. Given a positive integ

Codeforces Round #585 (Div. 2) B. The Number of Products

题目地址:http://codeforces.com/contest/1215/problem/B 题意:给你一个值都不为零的数组,分别找出有多少个连续的子串乘积小于零,大于零. 我的思路:先找出为正数的基础子串x个,即单个正数或两个负数连起来的子串算一个,可算得...算了一直wrong在样例7.还没想出来那错了,就不写了. 别人家的思路:从首开始找负数字串,即有一个负数后,在下一个负数前这里面的都是负数子串.而其他的就为正数子串,正数子串初始化应为1,所以得的正数子串最后应加上1.两个负数作为

【构造】Codeforces Round #480 (Div. 2) B. Marlin

题意:给你一个4*n的网格,保证n为奇数,让你在其中放k个障碍物,不能放在边界的格子上,使得从左上角走到右下角的最短路的方案数,恰好等于从左下角走到右上角的最短路的方案数. k为偶数时,以纵向为对称轴进行摆放即可. k为奇数且小于等于n-2时,如下图横向对称摆放: .........................#####.............. k等于n时,如下图: ..............#.........#..###########.............. k大于n时,在上

Codeforces Round 480 Div 2 光荣掉分记

痛 痛苦 痛苦啊. 越接近黄名想的越多了啊…… 都说了不要在意rating这破玩意了…… 没出E就算了,策略问题. 居然还FST了: FST个D就算了: FST个A算个**啊. 紧张的时候总会写出一些垃圾代码. 痛苦啊. 原文地址:https://www.cnblogs.com/cxhscst2/p/9017306.html

Codeforces Round #209 (Div. 2)——Prime Number

MySQL使用的是插件式存储引擎. 主要包括存储引擎有:MyISAM,Innodb,NDB Cluster,Maria,Falcon,Memory,Archive,Merge,Federated. 其中最为广泛的是MyISAM 和Innodb两种存储引擎,所以接下来对它们做简单介绍. MyISAM 存储引擎简介 MyISAM 存储引擎的表存储在数据库中,每一个表都被存放为三个以表名命名的物理文件. 1.(.frm文件)任何存储引擎都不可缺少的存放表结构定义信息的文件 2.(.MYD文件)存放表数