博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Shorten Diameter
阅读量:4615 次
发布时间:2019-06-09

本文共 2889 字,大约阅读时间需要 9 分钟。

Shorten Diameter


Time limit : 2sec / Stack limit : 256MB / Memory limit : 256MB

Score : 600 points

Problem Statement

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≦iN−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≦KN−1
  • 1≦AiN,1≦BiN
  • The graph defined by Ai and Bi is a tree.

Input

The input is given from Standard Input in the following format:

N KA1 B1A2 B2:AN−1 BN−1

Output

Print the minimum number of vertices that you need to remove in order to produce a good tree.


Sample Input 1

6 21 23 24 21 65 6

Sample Output 1

2

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

ctree.png

Sample Input 2

6 51 23 24 21 65 6

Sample Output 2

0

Since the given tree is already good, you do not need to remove any vertex.

分析:虽然没做出来,不过看了陈高远大牛的代码还是可以yy一下的。

可以先求出任意两点间的距离(dfs),然后考虑最优情况是左右子树平衡的时候(有2种情况),遍历至最优情况即可

代码:

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define rep(i,m,n) for(i=m;i<=n;i++)#define rsp(it,s) for(set
::iterator it=s.begin();it!=s.end();it++)#define vi vector
#define pii pair
#define mod 1000000007#define inf 0x3f3f3f3f#define pb push_back#define mp make_pair#define fi first#define se second#define ll long long#define pi acos(-1.0)const int maxn=2e3+10;const int dis[4][2]={ { 0,1},{-1,0},{ 0,-1},{ 1,0}};using namespace std;using namespace __gnu_cxx;ll gcd(ll p,ll q){ return q==0?p:gcd(q,p%q);}ll qpow(ll p,ll q){ll f=1;while(q){ if(q&1)f=f*p;p=p*p;q>>=1;}return f;}int n,m,a[maxn][maxn],mi,cnt;vi p[maxn];pii q[maxn];void dfs(int root,int pre,int now,int s){ a[root][now]=s; for(int x:p[now]) { if(x!=pre)dfs(root,now,x,s+1); }}int main(){ int i,j,k,t; mi=inf; scanf("%d%d",&n,&k); rep(i,1,n-1){ scanf("%d%d",&j,&t); p[j].pb(t),p[t].pb(j); q[i].fi=j,q[i].se=t; } rep(i,1,n)dfs(i,-1,i,0); rep(i,1,n) { cnt=0; rep(j,1,n) if(2*a[i][j]>k)cnt++; mi=min(mi,cnt); } rep(i,1,n-1) { cnt=0; rep(j,1,n) if(2*min(a[j][q[i].fi],a[j][q[i].se])+1>k) cnt++; mi=min(mi,cnt); } printf("%d\n",mi); //system ("pause"); return 0;}

 

转载于:https://www.cnblogs.com/dyzll/p/5678614.html

你可能感兴趣的文章
LeetCode 876. Middle of the Linked List
查看>>
作业一
查看>>
joj1023
查看>>
动画原理——旋转
查看>>
Finding LCM LightOJ - 1215 (水题)
查看>>
python生成器
查看>>
PowerDesigner Constraint name uniqueness 错误
查看>>
系统子系统_GPRS子系统流程图
查看>>
为什么 NSLog 不支持 Swift 对象(转)
查看>>
Ubuntu 下搭建SVN服务器
查看>>
css3转换
查看>>
将字符串中不同字符的个数打印出来
查看>>
java第三次上机
查看>>
android Javah生成JNI头文件
查看>>
npm创建react项目
查看>>
关于u32中查找和定位最后到bit Number of 1 Bits
查看>>
sql数据库查询
查看>>
云计算技能图谱
查看>>
委托、Lambda表达式和事件
查看>>
typecho模板制作代码收集
查看>>