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