E. The Number Games
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 n
.
This year, the president decided to reduce the costs. He wants to remove k
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?
The first line of input contains two integers n
) — the number of districts in Panel, and the number of contestants the president wishes to remove, respectively.
The next n−1
lines each contains two integers a and b (1≤a,b≤n, a≠b), that describe a road that connects two different districts a and bin the nation. It is guaranteed that there is exactly one path between every two districts.
Print k
space-separated integers: the numbers of the districts of which the contestants should be removed, in increasing order of district number.
6 3 2 1 2 6 4 2 5 6 2 3
1 3 4
8 4 2 6 2 7 7 8 1 2 3 1 2 4 7 5
1 3 4 5
In the first sample, the maximum possible total number of fans is 22+25+26=100
. We can achieve it by removing the contestants of the districts 1, 3, and 4.
题意:给出一颗N个节点的树,第i个节点的权值为2^i, 现在你必须删除K个节点(K < N),并且保证剩下的点都联通的情况下,使得剩下的点权值最大。
题解:我们要以N当根,N必须保留,这点很重要,然后贪心的从N-1 ~ 1这些点判断这个点i是否能保留,如果这个点能保留,则他到根N的这些点都必须被保留,这里需要倍增预处理,用个变量记录当前图还需要保留几个点,贪心即可。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1E6 + 7;
vector<int>g[maxn];
int par[maxn];
int up[20][maxn];
bool was[maxn];
void dfs(int u, int fa)
{
par[u] = up[0][u] = fa;
for(int i = 1;i < 20;i ++) up[i][u] = up[i-1][ up[i-1][u] ];
for(int to:g[u]) if(to != fa) dfs(to, u);
}
int main()
{
int n, k;
scanf("%d %d", &n, &k);
was[n] = 1;
for(int u, v, i = 1;i < n;i ++) {
scanf("%d %d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
dfs(n, n);
int ost = n - 1 - k;
for(int i = n - 1;i >= 1;i --) {
if(was[i]) continue;
int v = i, len = 0;
for(int j = 19;j >= 0;j --) {
if(!was[ up[j][v] ]) {
v = up[j][v];
len += 1 << j;
}
}
len ++;
if(len <= ost) {
v = i;
ost -= len;
while(true) {
if(was[v]) break;
was[v] = 1;
v = par[v];
}
}
}
for(int i = 1;i <= n; i++) if(!was[i]) printf("%d ",i);
printf("\n");
return 0;
}