问题 J: Shorten Diameter-------------------------------思维(图论+树的直径)

53 篇文章 0 订阅
31 篇文章 0 订阅

题目描述
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≤i≤N−1), there is an edge connecting vertices Ai and Bi.

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≤N≤2000
1≤K≤N−1
1≤Ai≤N,1≤Bi≤N
The graph defined by Ai and Bi is a tree.
输入
The input is given from Standard Input in the following format:

N K
A1 B1
A2 B2
:
AN−1 BN−1
输出
Print the minimum number of vertices that you need to remove in order to produce a good tree.
样例输入 Copy
6 2
1 2
3 2
4 2
1 6
5 6
样例输出 Copy
2
提示
The tree is shown below. Removing vertices 5 and 6 will result in a good tree with the diameter of 2.

在这里插入图片描述
题意:
给你一棵树,问你删除至少几个点,使得树的直径<=k
解析:
突破口:树的直径的中点
但我们要讨论k的奇偶性。
当k为偶数时,我们只要枚举点作为中点,如果其他点离中点的距离>k/2 则删除
当k为奇数时,我们只要枚举边作为中点,以这条边分割成两颗子树,分别枚举这条边的两个端点,如果其他点离这两个点的距离>k/2 则删除

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+1000;
vector<int> G[N];
int n,k,a,b,cnt;
int dep[N];
int ans=0x3f3f3f3f;
void dfs(int u,int fa,int val)
{
	for(auto t:G[u])
	{
		if(t==fa) continue;
		dep[t]=dep[u]+1;
		if(dep[t]>val) cnt++;
		dfs(t,u,val);
	}
}
int main()
{
	cin>>n>>k;
	for(int i=1;i<=n-1;i++)
	{
		cin>>a>>b;
		G[a].push_back(b);
		G[b].push_back(a); 
	}
	if(k%2==0)
	{
		for(int i=1;i<=n;i++)
		{
			cnt=0;
			dep[i]=0;
			dfs(i,-1,k/2);
			ans=min(ans,cnt);
		}
	}
	else
	{
		for(int i=1;i<=n;i++)//枚举边 
		{
			for(int j=0;j<G[i].size();j++)
			{
				cnt=0;
				dep[i]=0;dfs(i,G[i][j],k/2); //分别遍历一条边的两个端点 
				dep[G[i][j]]=0;dfs(G[i][j],i,k/2);
				ans=min(ans,cnt);
			}
		}
	}
	cout<<ans<<endl; 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值