题目链接:
3 seconds
256 megabytes
standard input
standard output
Treeland is a country in which there are n towns connected by n - 1 two-way road such that it's possible to get from any town to any other town.
In Treeland there are 2k universities which are located in different towns.
Recently, the president signed the decree to connect universities by high-speed network.The Ministry of Education understood the decree in its own way and decided that it was enough to connect each university with another one by using a cable. Formally, the decree will be done!
To have the maximum sum in the budget, the Ministry decided to divide universities into pairs so that the total length of the required cable will be maximum. In other words, the total distance between universities in k pairs should be as large as possible.
Help the Ministry to find the maximum total distance. Of course, each university should be present in only one pair. Consider that all roads have the same length which is equal to 1.
The first line of the input contains two integers n and k (2 ≤ n ≤ 200 000, 1 ≤ k ≤ n / 2) — the number of towns in Treeland and the number of university pairs. Consider that towns are numbered from 1 to n.
The second line contains 2k distinct integers u1, u2, ..., u2k (1 ≤ ui ≤ n) — indices of towns in which universities are located.
The next n - 1 line contains the description of roads. Each line contains the pair of integers xj and yj (1 ≤ xj, yj ≤ n), which means that the j-th road connects towns xj and yj. All of them are two-way roads. You can move from any town to any other using only these roads.
Print the maximum possible sum of distances in the division of universities into k pairs.
7 2
1 5 6 2
1 3
3 2
4 5
3 7
4 3
4 6
6
9 3
3 2 1 6 5 9
8 9
3 2
2 7
3 4
7 6
4 5
2 1
2 8
9
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=2e5+10;
const int maxn=500+10;
const double eps=1e-14; int vis[N],a[N],head[N],cnt,ans,siz,son[N],n,k;
LL ansdis=0; struct Edge
{
int from,to,next,val;
}edge[2*N];
inline void add_edge(int s,int e)
{
edge[cnt].from=s;
edge[cnt].to=e;
edge[cnt].next=head[s];
head[s]=cnt++;
} void dfs(int cur,int fa)
{
son[cur]=vis[cur];
int temp=0;
for(int i=head[cur];i!=-1;i=edge[i].next)
{
int fr=edge[i].to;
if(fr==fa)continue;
dfs(fr,cur);
son[cur]+=son[fr];
temp=max(temp,son[fr]);
}
temp=max(temp,2*k-son[cur]);
if(temp<siz||temp==siz&&cur<ans)
{
siz=temp;
ans=cur;
}
return ;
}
void dfs1(int cur,int fa,LL dis)
{
if(vis[cur])ansdis=ansdis+dis;
for(int i=head[cur];i!=-1;i=edge[i].next)
{
int fr=edge[i].to;
if(fr==fa)continue;
dfs1(fr,cur,dis+1);
}
return ;
}
inline void Init()
{
mst(head,-1);
cnt=0;
siz=inf;
}
int main()
{
read(n);read(k);
Init();
For(i,1,2*k)read(a[i]),vis[a[i]]=1;
For(i,1,n-1)
{
int u,v;
read(u);read(v);
add_edge(u,v);
add_edge(v,u);
}
dfs(1,0);
dfs1(ans,0,0);
cout<<ansdis<<endl;
return 0;
}