题目:

Problem Statement

Given an undirected tree, let the distance between vertices \(u\) and \(v\) be the number of edges on the simple path from \(u\) to \(v\). The diameter of a tree is the maximum among the distances between any two vertices. We will call a tree good if and only if its diameter is at most \(K\).

You are given an undirected tree with \(N\) vertices numbered \(1\) through \(N\). For each \(i(1\leq i\leq N−1)\), there is an edge connecting vertices \(A_i\) and \(B_i\).

You want to remove zero or more vertices from the tree, so that the resulting tree is good. When a vertex is removed, all incident edges will also be removed. The resulting graph must be connected.

Find the minimum number of vertices that you need to remove in order to produce a good tree.

Constraints

  • \(2\leq N\leq 2000\)
  • \(1\leq K\leq N−1\)
  • \(1\leq A_i \leq N,1\leq B_i \leq N\)
  • The graph defined by \(A_i\) and \(B_i\) is a tree.

题解

比较简单的一道题。

考虑如果以某一点为根,那么就是删去一定深度以上的节点。

稍微分析一下可以发现这样做一定会找到最优方案。

所以可以对所有的方案取\(min\)

那么下面的问题就是删除深度大于哪个值的节点。

对于给定的\(K\)是偶数的情况,那么直接删除深度大于\(\frac{K}{2}\)的所有点即可。

如果是奇数呢?

这时候我们转换思维,不枚举以哪一个点为根了,枚举以哪条边为根。

这时候我们可以发现接下来也只要删除所有深度大于\(\frac{K-1}{2}\)的点即可.

复杂度\(O(n^2)\)

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(int &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
#define rg register int
#define rep(i,a,b) for(rg i=(a);i<=(b);++i)
#define per(i,a,b) for(rg i=(a);i>=(b);--i)
const int maxn = 2048;
struct Edge{
int to,next;
}G[maxn<<1];
int head[maxn],cnt;
void add(int u,int v){
G[++cnt].to = v;
G[cnt].next = head[u];
head[u] = cnt;
}
#define v G[i].to
int dep[maxn];
void dfs(int u,int f){
for(rg i = head[u];i;i=G[i].next){
if(v == f) continue;
dep[v] = dep[u] + 1;
dfs(v,u);
}
}
#undef v
struct Node{
int u,v;
}e[maxn];
int main(){
int n,K;read(n);read(K);
int u,v;
rep(i,2,n){
read(u);read(v);
e[i].u = u;e[i].v = v;
add(u,v);add(v,u);
}
int ans = 0x3f3f3f3f;
if((K&1) == 0){
int res = 0;
rep(i,1,n){
res = dep[i] = 0;dfs(i,i);
rep(j,1,n) if(dep[j] > (K >> 1)) ++ res;
ans = min(ans,res);
}
}else{
int res = 0;
rep(i,2,n){
dep[e[i].u] = dep[e[i].v] = res = 0;
dfs(e[i].u,e[i].v);dfs(e[i].v,e[i].u);
rep(j,1,n){
if(dep[j] > (K >> 1)) ++ res;
}ans = min(ans,res);
}
}printf("%d\n",ans);
return 0;
}

ACC 001 C - Shorten Diameter 图论的更多相关文章

  1. AtCoder Grand Contest 001 C Shorten Diameter 树的直径知识

    链接:http://agc001.contest.atcoder.jp/tasks/agc001_c 题解(官方): We use the following well-known fact abou ...

  2. Shorten Diameter

    Shorten Diameter Time limit : 2sec / Stack limit : 256MB / Memory limit : 256MB Score : 600 points P ...

  3. AGC001 C - Shorten Diameter【枚举】

    一开始没看到要保证最后是树--所以一定要从叶子开始删 枚举重心,如果k是偶数,那么按当前重心提起来deep大于k/2的全都要切掉,这样枚举重心然后取min即可 奇数的话就是枚举直径中间的边,然后从两边 ...

  4. AtCoderACGC001C Shorten Diameter

    Description: 给定一个\(n\)个点的树,要求删去最少的点使树的致直径不超过k Solution: 如果\(k\)为偶数,对于最终状态一定是以每一个点为根任何点的深度不能超过\(k/2\) ...

  5. AtCoder Grand Contest

    一句话题解 QwQ主要是因为这篇文章写的有点长……有时候要找某一个题可能不是很好找,所以写了这个东西. 具体的题意.题解和代码可以再往下翻._(:з」∠)_ AGC 001 C:枚举中点/中边. D: ...

  6. RE:从零开始的AGC被虐(到)生活(不能自理)

    RE:从零开始的AGC被虐(到)生活(不能自理) 「一直注视着你,似近似远,总是触碰不到.」 --来自风平浪静的明天 AtCoder Grand Contest 001 B: Mysterious L ...

  7. A*G#C001

    AGC001 A BBQ Easy 贪心. https://agc001.contest.atcoder.jp/submissions/7856034 B Mysterious Light 很nb这个 ...

  8. 【AtCoder】AGC001

    AGC001 A - BBQ Easy 从第\(2n - 1\)个隔一个加一下加到1即可 #include <bits/stdc++.h> #define fi first #define ...

  9. AGC001[BCDE] 题解

    A没意思 F太难 所以大概近期的AGC题解都是BCDE的 然后特殊情况再说 开始刷AGC的原因就是计数太差 没有脑子 好几个学长都推荐的AGC所以就开始刷了 = = 大概两天三篇的速度?[可能也就最开 ...

随机推荐

  1. MySql安装成功后命令行进行必要的配置

    1.1 首次用命令行登录 用zip方式安装成功mysql,并通过net start mysql 命令正常启动mysql服务后,打开dos命令行窗口,输入“mysql -uroot -p”或“mysql ...

  2. 微服务架构~BFF和网关是如何演化出来的

    介绍 BFF(Backend for Frontend)和网关Gateway是微服务架构中的两个重要概念,这两个概念相对比较新,有些开发人员甚至是架构师都不甚理解. 本文用假想的公司案例+图示的方式, ...

  3. JS以指定格式获取当前日期

    //获取当前时间,格式YYYY-MM-DD function getNowFormatDate() { var date = new Date(); var seperator1 = "-& ...

  4. 编写自已的第一个MapReduce程序

    从进入系统学习到现在,貌似我们还没有真正开始动手写程序,估计有些立志成为Hadoop攻城狮的小伙伴们已经有些急了.环境已经搭好,小讲也有些按捺不住了.今天,小讲就和大家一起来动手编写我们的第一个Map ...

  5. Kubernetes 命令补全

    yum install -y bash-completionsource /usr/share/bash-completion/bash_completionsource <(kubectl c ...

  6. JAVA Excel导入导出

    --------------------------------------------方式一(新)-------------------------------------------------- ...

  7. mapreduce 实现数子排序

    设计思路: 使用mapreduce的默认排序,按照key值进行排序的,如果key为封装int的IntWritable类型,那么MapReduce按照数字大小对key排序,如果key为封装为String ...

  8. IOS 被拒 关于 iPhone running iOS 10.3.1 on Wi-Fi connected to an IPv6 network.

    问题: Guideline 2.1 - Performance Thank you for your resubmission. However, we discovered one or more ...

  9. 求两个有序序列合并成新有序序列的中位数,求第k小数

    此算法涉及一个重要数学结论:如果A[k/2-1]<B[k/2-1],那么A[0]~A[k/2-1]一定在第k小的数的序列当中,可以用反证法证明. 算法思想如下: 1,假设A长度为m,B长度为n, ...

  10. DB2导入数据时乱码问题

    1.由于导入import导入数据时乱码,一直找不到解决办法,于是就用load导入 LOAD后,发现某些表检查挂起( 原因码为 "1",所以不允许操作 SQLSTATE=57016  ...