P3000 [USACO10DEC]牛的健美操Cow Calisthenics

                          [USACO10DEC]牛的健美操Cow Calisthenics

题目描述

Farmer John continues his never-ending quest to keep the cows fit by having them exercise on various cow paths that run through the pastures. These cow paths can be represented as a set of vertices connected with bidirectional edges so that each pair of vertices has exactly one simple path between them. In the abstract, their layout bears a remarkable resemblance to a tree. Surprisingly, each edge (as it winds its way through the pastures) has the same length.

For any given set of cow paths, the canny cows calculate the longest possible distance between any pair of vertices on the set of cowpaths and call it the pathlength. If they think this pathlength is too large, they simply refuse to exercise at all.

Farmer John has mapped the paths and found V (2 <= V <= 100,000) vertices, conveniently numbered from 1..V. In order to make shorter cowpaths, he can block the path between any two vertices, thus creating more sets of cow paths while reducing the pathlength of both cowpath sets.

Starting from a single completely connected set of paths (which have the properties of a tree), FJ can block S (1 <= S <= V-1) paths, creating S+1 sets of paths. Your goal is to compute the best paths he can create so that the largest pathlength of all those sets is minimized.

Farmer John has a list of all V-1 edges in his tree, each described by the two vertices A_i (1 <= A_i <= V) and B_i (1 <= B_i <= V; A_i != B_i) that it connects.

Consider this rather linear cowpath set (a tree with 7 vertices):

1---2---3---4---5---6---7

If FJ can block two paths, he might choose them to make a map like this:

1---2 | 3---4 | 5---6---7 where the longest pathlength is 2, which would be the answer in this case. He can do no better than this.

TIME LIMIT: 2 seconds

MEMORY LIMIT: 32 MB

Farmer John为了保持奶牛们的健康,让可怜的奶牛们不停在牧场之间 的小路上奔跑。这些奶牛的路径集合可以被表示成一个点集和一些连接 两个顶点的双向路,使得每对点之间恰好有一条简单路径。简单的说来, 这些点的布局就是一棵树,且每条边等长,都为1。 对于给定的一个奶牛路径集合,精明的奶牛们会计算出任意点对路径的最大值, 我们称之为这个路径集合的直径。如果直径太大,奶牛们就会拒绝锻炼。 Farmer John把每个点标记为1..V (2 <= V <= 100,000)。为了获得更加短 的直径,他可以选择封锁一些已经存在的道路,这样就可以得到更多的路径集合, 从而减小一些路径集合的直径。 我们从一棵树开始,FJ可以选择封锁S (1 <= S <= V-1)条双向路,从而获得 S+1个路径集合。你要做的是计算出最佳的封锁方案,使得他得到的所有路径集合 直径的最大值尽可能小。 Farmer John告诉你所有V-1条双向道路,每条表述为:顶点A_i (1 <= A_i <= V) 和 B_i (1 <= B_i <= V; A_i!= B_i)连接。

输入输出格式

输入格式:

 

  • Line 1: Two space separated integers: V and S

  • Lines 2..V: Two space separated integers: A_i and B_i

 

输出格式:

 

  • Line 1: A single integer that is the best maximum pathlength FJ can achieve with S blocks

 

输入输出样例

输入样例#1:
7 2 
6 7 
3 4 
6 5 
1 2 
3 2 
4 5 
输出样例#1:
2 

最大值最小 我们首先想到二分 由于砍得边是有限的
所以我们可以二分树的最大直径
每次贪心检验
维护一个d[i] 表示第i个节点的子树下未砍掉的最长链
对d排序
如果最长链+次长链大于二分值 那么就砍掉最长链 否则 就把这条连加入到他父亲节点的d中

 1 #include <cstdio>
 2 #include <cctype>
 3 #include <vector>
 4 #include <algorithm>
 5 
 6 const int MAXN=100010;
 7 
 8 int n,m,cnt,mid;
 9 
10 int d[MAXN],f[MAXN];
11 
12 std::vector<int> Graph[MAXN];
13 
14 inline void read(int&x) {
15     int f=1;register char c=getchar();
16     for(x=0;!isdigit(c);c=='-'&&(f=-1),c=getchar());
17     for(;isdigit(c);x=x*10+c-48,c=getchar());
18     x=x*f;
19 }
20 
21 void DFS(int u,int fa) {
22     for(int i=0;i<Graph[u].size();++i) {
23         int v=Graph[u][i];
24         if(v==fa) continue;
25         DFS(v,u);
26     }
27     int tot=0;f[u]=1;
28     for(int i=0;i<Graph[u].size();++i) {
29         int v=Graph[u][i];
30         if(v==fa) continue;
31         d[++tot]=f[v];
32     }
33     std::sort(d+1,d+1+tot);
34     for(int i=tot;i;--i) {
35         if(d[i]+d[i-1]>mid) ++cnt;
36         else {f[u]+=d[i];return;}
37     }
38     return;
39 }
40 
41 inline bool check() {
42     cnt=0;
43     DFS(1,0);
44     return cnt<=m;
45 }
46 
47 int hh() {
48     read(n);read(m);
49     for(int x,y,i=1;i<n;++i) {
50         read(x);read(y);
51         Graph[x].push_back(y);
52         Graph[y].push_back(x);
53     }
54     DFS(1,0);
55     int l=0,r=MAXN+1;
56     while(l+1<r) {
57         mid=(l+r)>>1;
58         if(check()) r=mid;
59         else l=mid;
60     }
61     printf("%d\n",r);
62     return 0;
63 }
64 
65 int sb=hh();
66 int main(int argc,char**argv) {;}
代码

 




posted @ 2017-09-19 17:06  拿叉插猹哈  阅读(285)  评论(0编辑  收藏  举报