codeforces 700B
在给的点中选出一对对点,让每两个点的距离和最大
乱搞,做过这种关于类型的,这次算比较简单的。
#include <cstdio> #include <cstring> #include <algorithm> #include <cstdlib> #include <iostream> #include <vector> #include <cmath> #include <map> #include <string> #include <stack> #include <queue> using namespace std; vector<int>edge[200100]; int vis[200100]; int num[200100]; int n,k; long long ans; void dfs1(int root,int father) { if (vis[root]) num[root]=1; for (int i=0;i<edge[root].size();i++) { int v=edge[root][i]; if (v==father) continue; dfs1(v,root); num[root]+=num[v]; } } void dfs2(int root,int father) { for (int i=0;i<edge[root].size();i++) { int v=edge[root][i]; if (v==father) continue; ans+=min(num[v],2*k-num[v]); dfs2(v,root); } } int main() { scanf ("%d%d",&n,&k); int temp=2*k; while (temp--) { int p; scanf ("%d",&p); vis[p]=1; } n--; while (n--) { int t1,t2; scanf ("%d%d",&t1,&t2); edge[t1].push_back(t2); edge[t2].push_back(t1); } dfs1(1,0); dfs2(1,0); printf ("%I64d\n",ans); return 0; }