AtCoder Grand Contest 001 C

来源:互联网 发布:php一般用什么开发工具 编辑:程序博客网 时间:2024/05/29 03:57

想到边上去了、但是没有推出一定存在题解说的两种情况


C.Weuse the following well-known fact about trees.
Let T be a tree, and let D be the diameter of the tree.


• If D is even, there exists an vertex v of T such that for each vertex w in
T, the distance between w and v is at most D/2.


• If D is odd, there exists an edge e of T such that for each vertex w in T,
the distance between w and one of the endpoints of e is at most (D −1)/2.
Here v and e are called centers of the tree.


#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <ctime>#include <iostream>#include <algorithm>#include <sstream>#include <string>#include <vector>#include <queue>#include <stack>#include <map>#include <set>#include <utility>#include <bitset>using namespace std;#define LL long long#define pb push_back#define mk make_pair#define pill pair<int, int>#define fi first#define se second#define mst(a, b)memset(a, b, sizeof a)#define REP(i, x, n)for(int i = x; i <= n; ++i)const int MOD = 1e9 + 7;const int qq = 2e3 + 10;const LL INF = 1e9 + 10;int n, k;vector<int> G[qq];int dis[qq][qq];void Dfs(int rt, int u, int fa, int dep) {dis[rt][u] = dep;int sz = (int)G[u].size();for(int i = 0; i < sz; ++i) {int v = G[u][i];if(v == fa)continue;Dfs(rt, v, u, dep + 1);}}pill pl[qq];int main(){scanf("%d%d", &n, &k);for(int a, b, i = 1; i < n; ++i) {scanf("%d%d", &a, &b);G[a].pb(b), G[b].pb(a);pl[i] = mk(a, b);}for(int i = 1; i <= n; ++i) {Dfs(i, i, -1, 0);}int minx = n;for(int i = 1; i <= n; ++i) {int cnt = 0;for(int j = 1; j <= n; ++j) {if(dis[j][i] * 2 > k)cnt++;}minx = min(minx, cnt);}for(int i = 1; i <= n - 1; ++i) {int cnt = 0;for(int j = 1; j <= n; ++j) {if(2 * min(dis[j][pl[i].fi], dis[j][pl[i].se]) + 1 > k)cnt++;}minx = min(minx, cnt);}printf("%d\n", minx);return 0;}


原创粉丝点击