Shorten Diameter (dfs遍历)

 Shorten Diameter

时间限制: 2 Sec  内存限制: 256 MB
提交: 34  解决: 10
[提交][状态][讨论版][命题人:admin]

题目描述

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.

样例输入

6 2

1 2

3 2

4 2

1 6

5 6

样例输出

2

提示

The tree is shown below. Removing vertices 5 and 6 will result in a good tree with the diameter of 2.

一个简单的dfs遍历问题,硬生生的写了一天。题目说有n个点组成的树,要删除最少的点来得到深度为k的树,而且每次只能删除叶子节点。

反过来想,删除最少的点,就是剩下最多的点。一共2000个点,可以枚举每一个点,然后向两边延伸,维护一条最长的dfs链,最后n减去链的长度就是最后的答案。

k需要分情况考虑,若k是奇数,则需要枚举每一个点,然后向两侧延伸,跑k/2次dfs,若k是偶数,则枚举一条边,跑k/2次dfs。此处的关键在于,要向两侧跑,跑完一侧之后需要接着去跑另一侧的dfs,设置父节点,防止回头。

千言万语,都在代码里:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#include<set>
#include<cstdio>
#define ll long long
#define mset(a,x) memset(a,x,sizeof(a))

using namespace std;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}
ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}
ll Fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n;n=n*n;}return r;}
ll upd(ll x,ll v){x=x+v>=mod?x+v-mod:x+v;return x;}
struct node{
	int next,v,u,w;
}p[maxn];
int head[maxn],cnt,vis[maxn],n,k,maxx;

void add(int u,int v)
{
	p[cnt].v=v;
	p[cnt].next=head[u];
	head[u]=cnt++;
}

void dfs(int now,int fa,int step)
{
	maxx++;
	if(step==0)
	return ;
	for(int i=head[now];~i;i=p[i].next)
	{
		if(p[i].v==fa)
		continue;
		dfs(p[i].v,now,step-1);
	}
}

int main()
{
	int i,j,x,y;
	scanf("%d%d",&n,&k);
	for(i=0;i<=n;i++)
	head[i]=-1;
	for(i=0;i<n-1;i++)
	{
		scanf("%d%d",&x,&y);
		add(x,y);
		add(y,x);
	}
	int ans=0;
	if(k%2)    //k为奇数,枚举边,跑k/2的点 
	{
		for(i=1;i<=n;i++)
		{
			for(j=head[i];~j;j=p[j].next)
			{
				maxx=0;
				int t=p[j].v;
				dfs(t,i,k/2);
				dfs(i,t,k/2);
				ans=max(maxx,ans);
			}
		}
	}
	else      //k为偶数,枚举点,跑k/2的点 
	{
		for(i=1;i<=n;i++)
		{
			maxx=0;
			dfs(i,0,k/2);
			ans=max(maxx,ans);
		}
	}
	printf("%d\n",n-ans);
}
posted @ 2018-04-04 20:49  Ever_glow  阅读(124)  评论(0编辑  收藏  举报