CodeForces 700B 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. Recently, the president signe…
[题目链接] http://codeforces.com/problemset/problem/700/B [题目大意] 给出 一棵n个节点的树, 现在在这棵树上选取2*k个点,两两配对,使得其配对的两点间距离的和最大. [题解] 求出树的加权重心,那么答案就是每个点到加权重心的距离之和,但是实际上,并不需要求出加权重心,观察树边和其两边的询问节点,可以发现一个优美的性质,每条边对答案的贡献值为min(左边的点数,右边的点数),因此,树规计算每条边的贡献值,累加和就是答案. [代码] #incl…
分析:这个题一眼看上去很难,但是正着做不行,我们换个角度:考虑每条边的贡献 因为是一棵树,所以一条边把树分成两个集合,假如左边有x个学校,右边有y个学校 贪心地想,让每条边在学校的路径上最多,所以贡献为min(x,y) 具体实现:一次dfs即可,复杂度O(N) #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <vector&…
统计每一条边的贡献,假设$u$是$v$的父节点,$(u,v)$的贡献为:$v$下面大学个数$f[v]$与$2*k-f[v]$的较小值. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #in…
链接 Codeforces 701E Connecting Universities 题意 n个点的树,给你2*K个点,分成K对,使得两两之间的距离和最大 思路 贪心,思路挺巧妙的.首先dfs一遍记录每个点的子树中(包括自己)有多少点是这K个中间的,第二遍dfs时对于每一条边,取两端包含较少的值,这样就保证树中间的点不会被取到,留下的就是相隔更远的点了.方法确实想不到啊. 代码 #include <iostream> #include <cstdio> #include <v…
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 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…
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…
题目链接:http://codeforces.com/contest/701/problem/E 题意:有n个城市构成一棵树,一个城市最多有一个学校,这n个城市一共2*k个学校,要对这2*k个学校进行连边,使得所有连出来的边的和最大,每条边边权为1. 题解:这题有一个巧妙的解法,可以记录一下每一条边的贡献(所谓贡献就是有多少对点连线会经过这条边) 也就是记录一下这条边左端点以左的所有需要连的点x和右端点的数目就是2*k-x.然后边的贡献就是两者取 最小.这样dfs一遍所有的边就行了. #incl…