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