链接 Codeforces 701E Connecting Universities 题意 n个点的树,给你2*K个点,分成K对,使得两两之间的距离和最大 思路 贪心,思路挺巧妙的.首先dfs一遍记录每个点的子树中(包括自己)有多少点是这K个中间的,第二遍dfs时对于每一条边,取两端包含较少的值,这样就保证树中间的点不会被取到,留下的就是相隔更远的点了.方法确实想不到啊. 代码 #include <iostream> #include <cstdio> #include <v…
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…
分析:这个题一眼看上去很难,但是正着做不行,我们换个角度:考虑每条边的贡献 因为是一棵树,所以一条边把树分成两个集合,假如左边有x个学校,右边有y个学校 贪心地想,让每条边在学校的路径上最多,所以贡献为min(x,y) 具体实现:一次dfs即可,复杂度O(N) #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <vector&…
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…
题意:给出一棵树上的2*k个节点,给他们配对,使得他们之间的距离和最大. 思路:一条边的两侧如果有一侧没有给定的节点就不会被经过…… 如果有1个节点就会被经过1次…… 如果两侧分别有x,y个给定节点就会被经过min(x,y)次 因为要使总路程最大就是让每一条路被走过最多的次数 肯定是两侧各取一个 剩下的只能在某侧内部解决 所以按此统计即可 答案很大 用INT64 ..]of longint; n,k,tot,i:longint; ans:int64; procedure add(a,b:long…
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…
题意:现在给以一棵$n$个结点的树,并给你$2k$个结点,现在要求你把这些节点互相配对,使得互相配对的节点之间的距离(路径上经过边的数目)之和最大.数据范围$1 \leq n \leq 200000, 2k \leq n$. 分析:贪心选择距离最大.次大...的结点对?貌似不对.暴力枚举所有可能?对但不可行.考虑节点对之间的距离实际上就是它们到LCA距离之和.因此单独考虑每个结点,它对答案的贡献实际上就是它到与其匹配节点的LCA的距离,这个距离必然不超过它到根的距离.如果我们有一种方法使得每个结…