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;
}
ACC 001 C - Shorten Diameter 图论的更多相关文章
- AtCoder Grand Contest 001 C Shorten Diameter 树的直径知识
链接:http://agc001.contest.atcoder.jp/tasks/agc001_c 题解(官方): We use the following well-known fact abou ...
- Shorten Diameter
Shorten Diameter Time limit : 2sec / Stack limit : 256MB / Memory limit : 256MB Score : 600 points P ...
- AGC001 C - Shorten Diameter【枚举】
一开始没看到要保证最后是树--所以一定要从叶子开始删 枚举重心,如果k是偶数,那么按当前重心提起来deep大于k/2的全都要切掉,这样枚举重心然后取min即可 奇数的话就是枚举直径中间的边,然后从两边 ...
- AtCoderACGC001C Shorten Diameter
Description: 给定一个\(n\)个点的树,要求删去最少的点使树的致直径不超过k Solution: 如果\(k\)为偶数,对于最终状态一定是以每一个点为根任何点的深度不能超过\(k/2\) ...
- AtCoder Grand Contest
一句话题解 QwQ主要是因为这篇文章写的有点长……有时候要找某一个题可能不是很好找,所以写了这个东西. 具体的题意.题解和代码可以再往下翻._(:з」∠)_ AGC 001 C:枚举中点/中边. D: ...
- RE:从零开始的AGC被虐(到)生活(不能自理)
RE:从零开始的AGC被虐(到)生活(不能自理) 「一直注视着你,似近似远,总是触碰不到.」 --来自风平浪静的明天 AtCoder Grand Contest 001 B: Mysterious L ...
- A*G#C001
AGC001 A BBQ Easy 贪心. https://agc001.contest.atcoder.jp/submissions/7856034 B Mysterious Light 很nb这个 ...
- 【AtCoder】AGC001
AGC001 A - BBQ Easy 从第\(2n - 1\)个隔一个加一下加到1即可 #include <bits/stdc++.h> #define fi first #define ...
- AGC001[BCDE] 题解
A没意思 F太难 所以大概近期的AGC题解都是BCDE的 然后特殊情况再说 开始刷AGC的原因就是计数太差 没有脑子 好几个学长都推荐的AGC所以就开始刷了 = = 大概两天三篇的速度?[可能也就最开 ...
随机推荐
- python之路 正则表达式,模块导入的方法,hashlib加密
一.正则表达式re python中re模块提供了正则表达式相关操作 字符: . 匹配除换行符以外的任意字符 \w 匹配字母或数字或下划线或汉字 \s 匹配任意的空白符 \d 匹配数字 \b 匹配单词的 ...
- 使用BUCK进行iOS项目打包
关于BUCK BUCK是Facebook开源的快速打包工具,可以用于多种语言及平台的项目打包,例如:C.C++.Java.iOS.Android等等.用于大型的iOS.Android项目,可以显著提升 ...
- linux中搭建docker
1.通过 vagrant ssh登录虚拟机 2.在虚拟机中通过 yum 命令安装docker 3.通过docker -v检查docker是否安装成功 4.开启docker加速器 curl -sSL h ...
- Shell编程之运算
一.变量的数值计算 1.算术运算符 常用的运算符号 常用的运算命令 (1)双小括号 基本语法 1)利用"(())"进行简单运算 [root@codis-178 ~]# echo $ ...
- PHP用*隐藏中文问题
中文截取 function substr_cut($user_name){ $strlen= mb_strlen($user_name, 'utf-8'); $firstStr= mb_substr( ...
- ARC管理内存(一)
相关概念 栈 当程序执行某个方法(或函数)时,会从内存中名为栈(stack)的区域分配一块内存空间,这块内存空间称为帧(frame).帧负责保存程序在方法内声明的变量的值.在方法内声明的变量称为局部变 ...
- USB引脚及定义
USB 2.0 USB接口定义: USB引脚定义: 针脚 名称 说明 接线颜色 1 VCC +5V电压 红色 2 D- 数据线负极 白色 3 D+ 数据线正极 绿色 4 GND 接地 黑色 Min ...
- leetcode刷题2:两数相加add_two_numbers
题目:两数相加 (难度:中等) 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字. 将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以 ...
- Bootstrap3组件--2
目录 1. 分页 2. 标签 3. 徽章 4. 巨幕 5. 页头 6. 缩略图 7. 警告框 8. 进度条 9. 列表组 10. 面板 11.Well 1. 分页 <!doctype html& ...
- HDFS数据复本存放
复本怎么放Hadoop的默认布局策略是在运行客户端的节点上放第一个复本(如果客户端运行在容器之外,就随机选择一个节点,不过系统会避免挑选那些存储太满或太忙的节点).第二个复本放在与第一个不通且随机另外 ...