Codeforces Global Round 16 Questions

E. Buds Re-hanging

Topic:

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?

Questions:

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.

Ideas:
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.

Code:

#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);                     \
            cout.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)
{
    vis[rt]=true;
    for(int i=0;i<tmp[rt].size();++i)
    {
        int nxt = tmp[rt][i];
        if(!vis[nxt])
        {
            vec[rt].push_back(nxt);
            build(nxt);
        }
    }
}
//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];
        findbuds(nxt);
        if (type[nxt] == 1) leaves = true;
    }
    if (!leaves) type[rt] = 1;
    else type[rt] = 2;
}

int main()
{
    io_init;
    cin>>t;
    while(t--)
    {   
        cin>>n;
        // clear
        for(int i=0;i<=n;++i)
        {
            vis[i]=false;
            vec[i]=vector<int>();
            tmp[i]=vector<int>();
        }
        type.assign(n+1, -1);
        budsnum=0;
        for(int i=0;i<n-1;++i)
        {
            int u,v;
            cin>>u>>v;
            tmp[u].push_back(v);
            tmp[v].push_back(u);
        }
        build(1);
        findbuds(1);
        type[1]=0;
        int root_leaf = 0;
        for(int i=0;i<vec[1].size();++i)
        {
            int nxt = vec[1][i];
            if(type[nxt]==1)
                root_leaf=1;
        }
        for(int i=1;i<=n;++i)
        {
            budsnum+=(type[i]==2);
        }
        cout<<n-2*budsnum-root_leaf<<"\n";
    }
    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