题目链接:传送门

题目大意:n个点构成一棵树,给定 k*2 点,要分成 k 组,使每组点之间的距离之和最大。

题目思路:因为是求距离之和最大,所以我们可以知道这样一个性质。如果以一条边为界,两边的子树均有给定的点,则这条边一定会经过 min(左边的给定点数,右边的给定点数)次。

     那么这条边的贡献就是经过的次数。

#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>
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
#define fi first
#define se second
#define ping(x,y) ((x-y)*(x-y))
#define mst(x,y) memset(x,y,sizeof(x))
#define mcp(x,y) memcpy(x,y,sizeof(y))
using namespace std;
#define gamma 0.5772156649015328606065120
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define N 200005
#define maxn 10005
typedef pair<int,int> PII;
typedef long long LL; int n,m,k,a[N];
LL ans;
vector<int>V[N];
void dfs(int x,int fa){
for(int u:V[x]){
if(u==fa)continue;
dfs(u,x);
a[x]+=a[u];
ans+=min(a[u],k-a[u]);
}
}
int main(){
int i,j,group,x,y;
scanf("%d%d",&n,&k);k<<=;
for(i=;i<=k;++i){
scanf("%d",&x);
a[x]=;
}
for(i=;i<n;++i){
scanf("%d%d",&x,&y);
V[x].push_back(y);
V[y].push_back(x);
}
dfs(,-);
printf("%I64d\n",ans);
return ;
}

Codeforces Round #364 (Div. 1)B. Connecting Universities的更多相关文章

  1. Codeforces Round #364 (Div. 2) E. Connecting Universities

    E. Connecting Universities time limit per test 3 seconds memory limit per test 256 megabytes input s ...

  2. Codeforces Round #364 (Div. 2) E. Connecting Universities (DFS)

    E. Connecting Universities time limit per test 3 seconds memory limit per test 256 megabytes input s ...

  3. Codeforces Round #364 (Div. 1) (差一个后缀自动机)

    B. Connecting Universities 大意: 给定树, 给定2*k个点, 求将2*k个点两两匹配, 每个匹配的贡献为两点的距离, 求贡献最大值 单独考虑每条边$(u,v)$的贡献即可, ...

  4. Codeforces Round #364 (Div. 1)(vp) 没什么题解就留坑待填

    我就做了前两题,第一题第一次vp就把我搞自闭跑路了,第二题第二次又把我搞自闭了 A. As Fast As Possible 细节题 #include<cstdio> #include&l ...

  5. Codeforces Round #364 (Div. 2)

    这场是午夜场,发现学长们都睡了,改主意不打了,第二天起来打的virtual contest. A题 http://codeforces.com/problemset/problem/701/A 巨水无 ...

  6. Codeforces Round #364 (Div.2) D:As Fast As Possible(模拟+推公式)

    题目链接:http://codeforces.com/contest/701/problem/D 题意: 给出n个学生和能载k个学生的车,速度分别为v1,v2,需要走一段旅程长为l,每个学生只能搭一次 ...

  7. Codeforces Round #364 (Div.2) C:They Are Everywhere(双指针/尺取法)

    题目链接: http://codeforces.com/contest/701/problem/C 题意: 给出一个长度为n的字符串,要我们找出最小的子字符串包含所有的不同字符. 分析: 1.尺取法, ...

  8. 树形dp Codeforces Round #364 (Div. 1)B

    http://codeforces.com/problemset/problem/700/B 题目大意:给你一棵树,给你k个树上的点对.找到k/2个点对,使它在树上的距离最远.问,最大距离是多少? 思 ...

  9. Codeforces Round #364 (Div. 2) B. Cells Not Under Attack

    B. Cells Not Under Attack time limit per test 2 seconds memory limit per test 256 megabytes input st ...

随机推荐

  1. 基于DDD的.NET开发框架-DDD经典分层

    DDD核心思想是由业务问题来控制解决方案的形式从以数据库为中心过渡到领域模型为中心 下面这个图是我在<领域驱动设计与模式实战>书中拍下来的,他完全诠释DDD的经典分层. 程序代码中也是响应 ...

  2. Openresty配置文件上传下载

    1. 下载包安装Openresty openresty-1.13.6.1下载地址 https://openresty.org/download/openresty-1.13.6.1.tar.gz 安装 ...

  3. 多线程-Thread、Runnable、Callbale、Future

    Thread:java使用Thread代表线程,所有的线程对象都必须是Thread类或其子类,可以通过继承Thread类来创建并启动多线程. package org.github.lujiango; ...

  4. PCIe学习笔记(15)--- TLP的ROUTING方式

    PCIE是POINT TO POINT的,不像PCI,是SHARED-BUS,总线上的数据,是被所有EP DEV看到的. 这一点与USB2.0比较类似,是广播方式的(BROADCASTING) USB ...

  5. lua 小技巧

    lua 小技巧 把常用的工具函数添加到 _G 里面,所有的文件都可以直接调用: ``` lua -- 在 a 文件中将工具函数添加到 _G: _G.IsEmptyStr = function(str) ...

  6. 转-linux下配置socks5代理

    简介: 在Linux下有各种各样的代理程序可用,象最常用的Squid,是http/https代理,也能代理ftp请求,但它实际上 是个HTTP代理程序,不是ftp代理,但它能处理ftp代理请求,就象浏 ...

  7. vi全文替换命令

    1,$s/str1/str2/g:从第一行到最后一行把str1替换成str2

  8. linux学习笔记22--命令ln

    ln是linux中又一个非常重要命令,它的功能是为某一个文件在另外一个位置建立一个同步的链接.当我们需要在不同的目录,用到相同的文件时,我们不需要在每一个需要的目录下都放一个必须相同的文件,我们只要在 ...

  9. 数据库设计(五)第一范式(1NF)?

    In our last tutorial we learned and understood how data redundancy or repetition can lead to several ...

  10. 一点一滴之DBUS

    记录几个参考链接: http://www.cnblogs.com/muxue/archive/2012/12/02/2798876.html  --- DBus 入门与应用 -- DBus 的 C 编 ...