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

题目描述
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; 
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章