首页 技术 正文
技术 2022年11月14日
0 收藏 431 点赞 2,777 浏览 2330 个字

链接:http://agc001.contest.atcoder.jp/tasks/agc001_c

题解(官方):

We use 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.

The proof of this fact is not very hard. See the picture below. The blue
vertices are the endpoints of the diameters, and the red vertex (or edge) is in
the middle of the diameter. This red vertex is the center of the tree; if there
is a vertex v such that dist(v, red) > D/2, the distance between v and one of
blue points will be more than D (because the distance between the red point
and each blue point is D/2). The proof for odd case is similar.

Now the problem can be solved in the following way (we only describe the
solution for the even case, but the odd case is similar). Choose a vertex x in
the tree (this will be the center after removal of vertices) and count the number
of vertices y such that dist(x, y) > D/2. If we remove all such y, the diameter
of the remaining graph will be at most D. Thus, we can try all N vertices as x
and the answer is the minimum count of such y. The solution works in O(N^2).

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef long long LL;
const int N = 2e3+;
const int INF = 0x3f3f3f3f;
const LL mod = 1e9+;
typedef pair<int ,int >pii;
struct Edge{
int v,next;
}edge[N<<];
int head[N],tot,n,k;
void add(int u,int v){
edge[tot].v=v;
edge[tot].next=head[u];
head[u]=tot++;
}
bool vis[N];
int d[N];
void bfs(int s,int f){
queue<int>q;
while(!q.empty())q.pop();
d[s]=;vis[s]=true;
q.push(s);
while(!q.empty()){
int u=q.front();
q.pop();
for(int i = head[u];~i;i=edge[i].next){
int v=edge[i].v;
if(vis[v]||v==f)continue;
d[v]=d[u]+;
vis[v]=true;
q.push(v);
}
}
}
int solveodd(int u,int v){
memset(d,INF,sizeof(d));
memset(vis,,sizeof(vis));
bfs(u,v);bfs(v,u);
int ret=;
for(int i=;i<=n;++i)
if(d[i]>k)++ret;
return ret;
}
int solveeven(int u){
memset(d,INF,sizeof(d));
memset(vis,,sizeof(vis));
bfs(u,);
int ret=;
for(int i=;i<=n;++i)
if(d[i]>k)++ret;
return ret;
}
int main(){
scanf("%d%d",&n,&k);
memset(head,-,sizeof(head));
for(int i=;i<=n-;++i){
int u,v;
scanf("%d%d",&u,&v);
add(u,v);add(v,u);
}
int ret=INF;
if(k&){
k>>=;
for(int i=;i<tot;i+=)
ret=min(ret,solveodd(edge[i].v,edge[i+].v));
}
else{
k>>=;
for(int i=;i<=n;++i)
ret=min(ret,solveeven(i));
}
printf("%d\n",ret);
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905