ACC 001 C - Shorten Diameter 图论

题目:

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;
}

转载于:https://www.cnblogs.com/Skyminer/p/7001744.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值