Codeforces Global Round 16 Questions

E. Buds Re-hanging


Given a multifork tree, a node is defined as a "tree bud". The condition is:

  1. Not root node
  2. Contains at least one child node
  3. All child nodes are leaf nodes.

Now you can do one thing: graft the bud node and all its children onto another node.

Question: If you can do any of the above operations, what is the minimum number of leaf nodes that will be retained in the tree?


First, make it clear that:
When a new "tree bud" node is generated after a graft, the new "tree bud" node can also be grafted.

If the minimum number of final leaf nodes is expected, then it will be effective to graft the "Tree Bud" node onto a leaf node, which will reduce the number of leaf nodes by one.
So how do you calculate all the "tree bud" nodes and how do you calculate the new "tree bud" nodes that may result from the grafting operation?
You can try to simulate the grafting operation. It is not difficult to find that a node may become a "tree bud" node when there are one or more leaf nodes in all its children. When all its children do not have a leaf node, the node becomes a leaf node because of the grafting operation.
With these two rules, it is easy to find the bud and leaf nodes.
Since any operation can be performed and all operations can be reversed, the same optimal state can be reached regardless of the state of the tree.
Let's assume that all the "tree bud" nodes are first grafted onto the root node. The number of leaf nodes present at this time is N-k-1 (Number of nodes - Number of "tree bud" nodes - Root node). Then the "tree bud" nodes are grafted onto other leaf nodes in turn to minimize the number of final leaf nodes.
There are two situations at this point:
(1) If the child node of the root node has a leaf node, then all the "tree bud" nodes can be successively grafted onto the leaf node (or its new leaf node resulting from the grafting operation), then all the "tree bud" nodes can be effectively grafted, even if the number of leaf nodes is reduced by one. The answer is N-k-1-k = N-2k-1.
(2) If the child nodes of the root node do not have leaf nodes, then only one "bud" node can be used as the starting point of grafting, that is, one "bud" node will be invalid for grafting, so the answer is N-k-1-(k-1) = N-2k.


#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pii pair<int,int>
#define io_init  ios::sync_with_stdio(false);    \
            cin.tie(0);                     \
const int maxn = 2e5+5;
const int mod = 998244353;
int t;
int n,m,k;
vector<int> vec[maxn],tmp[maxn],type;
bool vis[maxn];
unordered_set<int> buds;
int budsnum,numl;

//Build Trees
void build(int rt)
    for(int i=0;i<tmp[rt].size();++i)
        int nxt = tmp[rt][i];
//Find Tree Bud and Leaf Nodes
void findbuds(int rt)
    bool leaves = false;
    for (int i=0;i<vec[rt].size();++i) {
        int nxt =vec[rt][i];
        if (type[nxt] == 1) leaves = true;
    if (!leaves) type[rt] = 1;
    else type[rt] = 2;

int main()
        // clear
        for(int i=0;i<=n;++i)
        type.assign(n+1, -1);
        for(int i=0;i<n-1;++i)
            int u,v;
        int root_leaf = 0;
        for(int i=0;i<vec[1].size();++i)
            int nxt = vec[1][i];
        for(int i=1;i<=n;++i)
    return 0;

Subsequent topics will be added if you have time.

Welcome to corrections and comments!

Tags: C++ Algorithm data structure CodeForces

Posted on Mon, 13 Sep 2021 13:52:32 -0400 by nec9716