zoukankan      html  css  js  c++  java
  • 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 (1k<n1061≤k<n≤106) — the number of districts in Panel, and the number of contestants the president wishes to remove, respectively.

    The next n1n−1 lines each contains two integers aa and bb (1a,bn1≤a,b≤n, aba≠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 3
    2 1
    2 6
    4 2
    5 6
    2 3
    output
    Copy
    1 3 4
    input
    Copy
    8 4
    2 6
    2 7
    7 8
    1 2
    3 1
    2 4
    7 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 }
    View Code
  • 相关阅读:
    C#实现简单的委托异步调用
    jquery each遍历节点使用
    js操作document文档元素 节点交换交换
    js闭包使用
    .net下各个数据类型所占用的字节
    html file控件选择文件后立即预览 js实现
    叉积运用-判断凸多边形
    高精度算法-大数加法
    Dfs学习经验(纸上运行理解DFS)【两道题】
    a标签点击后更改颜色
  • 原文地址:https://www.cnblogs.com/BIGTOM/p/9024135.html
Copyright © 2011-2022 走看看