Codeforces Round #364 (Div. 1)B. Connecting Universities
题目链接:传送门
题目大意: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的更多相关文章
- 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 ...
- 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 ...
- Codeforces Round #364 (Div. 1) (差一个后缀自动机)
B. Connecting Universities 大意: 给定树, 给定2*k个点, 求将2*k个点两两匹配, 每个匹配的贡献为两点的距离, 求贡献最大值 单独考虑每条边$(u,v)$的贡献即可, ...
- Codeforces Round #364 (Div. 1)(vp) 没什么题解就留坑待填
我就做了前两题,第一题第一次vp就把我搞自闭跑路了,第二题第二次又把我搞自闭了 A. As Fast As Possible 细节题 #include<cstdio> #include&l ...
- Codeforces Round #364 (Div. 2)
这场是午夜场,发现学长们都睡了,改主意不打了,第二天起来打的virtual contest. A题 http://codeforces.com/problemset/problem/701/A 巨水无 ...
- Codeforces Round #364 (Div.2) D:As Fast As Possible(模拟+推公式)
题目链接:http://codeforces.com/contest/701/problem/D 题意: 给出n个学生和能载k个学生的车,速度分别为v1,v2,需要走一段旅程长为l,每个学生只能搭一次 ...
- Codeforces Round #364 (Div.2) C:They Are Everywhere(双指针/尺取法)
题目链接: http://codeforces.com/contest/701/problem/C 题意: 给出一个长度为n的字符串,要我们找出最小的子字符串包含所有的不同字符. 分析: 1.尺取法, ...
- 树形dp Codeforces Round #364 (Div. 1)B
http://codeforces.com/problemset/problem/700/B 题目大意:给你一棵树,给你k个树上的点对.找到k/2个点对,使它在树上的距离最远.问,最大距离是多少? 思 ...
- 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 ...
随机推荐
- 利用docker创建支持centos的ssh镜像
创建docker镜像需要基础镜像,目前官方已提供了多种基础镜像,参见: https://hub.docker.com/explore/ 要想创建支持centos的ssh镜像,就需要以centos镜像为 ...
- CSDN开源夏令营 百度数据可视化实践 ECharts(8)
(1)前言 首先谢谢林峰老师,继续接着第七篇提到的内容.CSS布局确实非常累,感觉不好看了就的调整,总的看起来的高大上嘛.好了废话不再多说.今天主要就先解说一个页面的内容,对于CSS布局后面讲会具体的 ...
- sklearn中xgboost模块中plot_importance函数(特征重要性)
# -*- coding: utf-8 -*- """ ######################################################### ...
- php_memcahed 安装
1.下载服务端memcached软件 32bit:下载 memcached-win32-1.4.4-14.zip(直接下)里面包含6个文件,将解压后的文件夹随便放在什么位置(例如:D:\wamp_wi ...
- php里面的编码问题
1 获取当前字符串的编码 $encode = mb_detect_encoding($str, array("ASCII",'UTF-8',"GB2312",& ...
- 如果你需要从不同的服务器(不同域名)上获取数据就需要使用跨域 HTTP 请求
Response.AppendHeader("Access-Control-Allow-Origin", "*")Response.AppendHeader(& ...
- win 7 下vim的使用
1.gVim74.exe ftp://ftp.vim.org/pub/vim/pc/gvim74.exe 2.vimcdoc-1.9.0-setup.exe 中文说明文档 http://211.147 ...
- [sj系统] phabricator系统升级
sj是公司产品bug管理 wiki一体化系统 ,很刁. 安装: http://www.cnblogs.com/chorulex/p/5381558.html 升级: https://phabricat ...
- python3 解析xml
转载:http://www.jb51.net/article/79494.htm 这篇文章主要为大家详细介绍了深入解读Python解析XML的几种方式,以ElementTree模块为例,演示具体使用方 ...
- mysql only_full_group_by问题
我的mysql出现了only_full_group_by问题,网上一堆处理方案! 主要两种 一种修改配置表my.ini 另一种通过指令,屏蔽当前链接的only_full_group_by报错!我想永久 ...