cf1042f Leaf Sets

Source: Internet
Author: User

CF1042F Leaf sets test instructions translation

Given a tree \ (n\) , the leaf node is divided into several sets so that the maximum distance of the point pair in the set is not more than \ (k\), and the minimum number of aggregates is obtained.

Input output Format input format:

The first line contains integers \ (n\) and \ (k\) ( \ (3 \le n \le 10^6\) )-the number of V Ertices in the tree and the maximum distance between all pair of leaves in each beautiful set.

Each of the next \ (n-1\) lines contains the integers \ (v_i\) and \ (u_i\) ( \ (1 \le v_i, u _i \le n\) )-the Description of the (i\) -th edge.

It's guaranteed that the given edges form a tree.

Output format:

Print a single integer-the minimal number of beautiful sets the split can has.

The Rokua difficulty tag is a fake series (2018.10.12).

There is a certain degree of difficulty in thinking, but not.

Consider such a property:
If there are two leaves they are the nearest leaf, then their distance to the other leaves in the tree is the deeper contribution of that leaf.

In this question, if the distance between them is not more than K, then the shallow leaves are useless, if more than, let the deep leaves themselves into a faction, shallow left the possibility of upward, this is right.

Specifically, we have an external statistical answer, and then we traverse the root of a node with a degree greater than 1.

Each sub-tree from its son to the deep leaves of the contribution of the sort, and then judge whether there is no deep leaf illegal, there is the deletion, and then spread the largest not deleted.

Code:

  #include <cstdio> #include <vector> #include <algorithm>const int n=1e6+10;int head[n],to [n<<1],next[n<<1],cnt;void Add (int u,int v) {to[++cnt]=v,next[cnt]=head[u],head[u]=cnt;}    int n,k,ans,in[n];int dfs (int now,int fa) {std::vector <int > diss;        for (int i=head[now];i;i=next[i]) {int v=to[i];        if (V==FA) continue;    Diss.push_back (Dfs (V,now));    } std::sort (Diss.begin (), Diss.end ());    int i;        For (I=diss.size () -1;i>0;i--) {if (diss[i]+diss[i-1]>k) ++ans;    else break; } return i<0?in[now]==1:diss[i]+1;}    int main () {scanf ("%d%d", &n,&k);        for (int u,v,i=1;i<n;i++) {scanf ("%d%d", &u,&v);        Add (U,v), add (V,u);    ++in[v],++in[u];            } for (int i=1;i<=n;i++) if (in[i]>1) {if (Dfs (i,0)) ans++;        Break    } printf ("%d\n", ans); return 0;}  

2018.10.12

cf1042f Leaf Sets

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.