您的位置:首页 > 其它

codeforces 700B

2016-07-25 21:04 288 查看
在给的点中选出一对对点,让每两个点的距离和最大

乱搞,做过这种关于类型的,这次算比较简单的。

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: