Codeforces Round #364 (Div. 1) B. Connecting universities

Source: Internet
Author: User

Topic Link: Portal

The main idea: N points constitute a tree, given the k*2 point, to be divided into k groups, so that each group of points between the maximum distance.

Topic idea: Because is to seek the distance and the biggest, so we can know such a nature. If an edge is bounded, and the subtree on both sides have a given point, the edge must go through min (the given number of points on the left and the given number of points on the right).

Then the contribution of this side is the number of passes.

#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<algorithm>#include<cstring>#include<stack>#include<cctype>#include<queue>#include<string>#include<vector>#include<functional>#include<Set>#include<map>#include<climits>#defineLson Root<<1,l,mid#defineRson Root<<1|1,mid+1,r#defineFi first#defineSe Second#definePing (x, y) ((x-y) * (x-y))#defineMST (x, y) memset (x,y,sizeof (x))#defineMCP (x, y) memcpy (x,y,sizeof (y))using namespacestd;#defineGamma 0.5772156649015328606065120#defineMOD 1000000007#defineINF 0x3f3f3f3f#defineN 200005#defineMAXN 10005typedef pair<int,int>Pii;typedefLong LongLL;intN,m,k,a[n]; LL Ans;vector<int>V[n];voidDfsintXintFA) {     for(intU:v[x]) {        if(U==FA)Continue;        DFS (U,X); A[X]+=A[u]; Ans+=min (a[u],k-A[u]); }}intMain () {intI,j,group,x,y; scanf ("%d%d", &n,&k); k<<=1;  for(i=1; i<=k;++i) {scanf ("%d",&x); A[X]=1; }     for(i=1; i<n;++i) {scanf ("%d%d",&x,&y);        V[x].push_back (y);    V[y].push_back (x); } DFS (1,-1); printf ("%i64d\n", ans); return 0;}

Codeforces Round #364 (Div. 1) B. Connecting universities

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.