题目
题目
给你一个n个顶点的树和2*k个点,可以组成k对,问k对的道路和最大是多少。
思路
考虑每条边的贡献的最大值,每条边都贡献最大也一定可以构造出这样的对,猜的。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+5;
struct Edge{int to,nex;}edge[N<<1];int head[N],tot;
inline void add(int from,int to){
edge[++tot]=(Edge){to,head[from]},head[from]=tot;
edge[++tot]=(Edge){from,head[to]},head[to]=tot;
}
int n,k,dp[N];ll ans=0;
void dfs(int x,int fa){
for(int i=head[x];i;i=edge[i].nex){
int y=edge[i].to;
if(y==fa) continue;
dfs(y,x),dp[x]+=dp[y];
ans+=min(dp[y],2*k-dp[y]);
}
}
int main(){
scanf("%d%d",&n,&k);
for(int i=1,x;i<=2*k;++i) scanf("%d",&x),dp[x]=1;
for(int i=1,x,y;i<=n-1;++i) scanf("%d%d",&x,&y),add(x,y);
dfs(1,1);
printf("%lld\n",ans);
}