@Shorten Diameter@
@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≦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.
Input
The input is given from Standard Input in the following format:
N K
A1 B1
A2 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 2
1 2
3 2
4 2
1 6
5 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.
Sample Input 2
6 5
1 2
3 2
4 2
1 6
5 6
Sample Output 2
0
Since the given tree is already good, you do not need to remove any vertex.
@Translation@
给你一棵包含 N 个点的树。N <= 2000。
进行若干次操作,每次操作删除一个叶子。
求最少次的操作,使得剩下的树的直径 <= K。
@Solution@
首先可以用反证法证明这样一个结论:如果一棵树有多条直径,所有的直径共中点(如果是长度为奇数则共中间一条边)。
因此:我们可以枚举最后剩下的树的中点,删去深度 >= K/2 的结点统计答案。
总时间复杂度为O(n^2)。
注意分类讨论直径为奇数或偶数。
@Code@
思维题/结论题,利用了树直径的一些性质qwq。
这个博客感觉好像有点儿掺水。。。
#include<queue>
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN = 2000;
struct edge{
int to;
edge *nxt;
}edges[MAXN*2 + 5], *adj[MAXN + 5], *ecnt = &edges[0];
void addedge(int u, int v) {
edge *p = (++ecnt);
p->to = v, p->nxt = adj[u], adj[u] = p;
p = (++ecnt);
p->to = u, p->nxt = adj[v], adj[v] = p;
}
queue<int>que;
int dis[MAXN + 5], vis[MAXN + 5], N, K;
int solve() {
int ret = 0;
while( !que.empty() ) {
int f = que.front(); que.pop();
if( dis[f] > K/2 ) ret++;
for(edge *p=adj[f];p!=NULL;p=p->nxt) {
if( !vis[p->to] ) {
vis[p->to] = true;
dis[p->to] = dis[f] + 1;
que.push(p->to);
}
}
}
return ret;
}
int main() {
scanf("%d%d", &N, &K);
for(int i=1;i<N;i++) {
int A, B;
scanf("%d%d", &A, &B);
addedge(A, B);
}
int ans = MAXN + 5;
if( K % 2 == 0 ) {
for(int i=1;i<=N;i++) {
for(int j=1;j<=N;j++) {
vis[j] = false;
dis[j] = 0;
}
vis[i] = true; que.push(i);
ans = min(ans, solve());
}
}
else {
for(int i=1;i<=N;i++) {
for(edge *p=adj[i];p!=NULL;p=p->nxt) {
if( i > p->to ) continue;
for(int j=1;j<=N;j++) {
vis[j] = false;
dis[j] = 0;
}
vis[i] = true; que.push(i);
vis[p->to] = true; que.push(p->to);
ans = min(ans, solve());
}
}
}
printf("%d\n", ans);
}
@END@
就是这样,新的一天里,也请多多关照哦(ノω<。)ノ))☆.。