cf 700 B Connecting Universities】的更多相关文章

题意:现在给以一棵$n$个结点的树,并给你$2k$个结点,现在要求你把这些节点互相配对,使得互相配对的节点之间的距离(路径上经过边的数目)之和最大.数据范围$1 \leq n \leq 200000, 2k \leq n$. 分析:贪心选择距离最大.次大...的结点对?貌似不对.暴力枚举所有可能?对但不可行.考虑节点对之间的距离实际上就是它们到LCA距离之和.因此单独考虑每个结点,它对答案的贡献实际上就是它到与其匹配节点的LCA的距离,这个距离必然不超过它到根的距离.如果我们有一种方法使得每个结…
Descrition 给你一颗\(n\le 2*10^5\)个点的树, 有\(2*k(2k\le n)\)座大学座落在点上 (任二大学不在同一个点) 求一种两两匹配的方案, 使得距离和最大 即\[maximize~\{~\sum_{each~pair~(x,y)} dis(x,y)~\}~\] Solution 1 (1) 化简一下我们相当于要最小化 两两lca的深度和 我们先把这2k所大学按dfn序从小到大排好, 把前k个称为A部分, 后k个称为B部分 (2) 所有匹配均为\(A-B\)匹配…
E. Connecting Universities time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Treeland is a country in which there are n towns connected by n - 1 two-way road such that it's possible to get f…
Connecting Universities Treeland is a country in which there are n towns connected by n - 1 two-way road such that it's possible to get from any town to any other town. In Treeland there are 2k universities which are located in different towns. Recen…
E. Connecting Universities time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Treeland is a country in which there are n towns connected by n - 1 two-way road such that it's possible to get f…
题目链接: E. Connecting Universities time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Treeland is a country in which there are n towns connected by n - 1 two-way road such that it's possible to…
链接 Codeforces 701E Connecting Universities 题意 n个点的树,给你2*K个点,分成K对,使得两两之间的距离和最大 思路 贪心,思路挺巧妙的.首先dfs一遍记录每个点的子树中(包括自己)有多少点是这K个中间的,第二遍dfs时对于每一条边,取两端包含较少的值,这样就保证树中间的点不会被取到,留下的就是相隔更远的点了.方法确实想不到啊. 代码 #include <iostream> #include <cstdio> #include <v…
[题目链接] http://codeforces.com/problemset/problem/700/B [题目大意] 给出 一棵n个节点的树, 现在在这棵树上选取2*k个点,两两配对,使得其配对的两点间距离的和最大. [题解] 求出树的加权重心,那么答案就是每个点到加权重心的距离之和,但是实际上,并不需要求出加权重心,观察树边和其两边的询问节点,可以发现一个优美的性质,每条边对答案的贡献值为min(左边的点数,右边的点数),因此,树规计算每条边的贡献值,累加和就是答案. [代码] #incl…
Treeland is a country in which there are n towns connected by n - 1 two-way road such that it's possible to get from any town to any other town. In Treeland there are 2k universities which are located in different towns. Recently, the president signe…
Treeland is a country in which there are n towns connected by n - 1 two-way road such that it's possible to get from any town to any other town. In Treeland there are 2k universities which are located in different towns. Recently, the president signe…