Description

给出一棵N个结点的树,选择L条路径,覆盖这些路径上的结点,使得被覆盖到的结点数最多。

Input

第一行两个正整数N、L(2 <= N <= 1,000,000, 0 <= L <= N)。下面有N-1行,每行两个正整数A和B(1 <= A, B <= N),表示一条边(A,B)。

Output

一个整数,表示最多能覆盖到多少结点。

Sample Input

17 3

1 2

3 2

2 4

5 2

5 6

5 8

7 8

9 8

5 10

10 13

13 14

10 12

12 11

15 17

15 16

15 10

Sample Output

13

Solution

这个题么,还是很容易想的吧。首先,我们随便画出一棵树来,然后YY一下,发现对于所有的叶子节点,我们最多选到\(min(2\times l ,sum_{leaves})\)个节点。那么,思考一下我们是不是可以把这个结论推广到这棵树的每一层上,事实证明,完全没有问题。那么,如何给这棵树分层呢?比较好的方法是从这棵树的所有叶子节点来一遍拓扑排序,记录一下就可以了。

Code

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#define re register
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define MAXN 1000001
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ms(arr) memset(arr, 0, sizeof(arr))
const int inf = 0x3f3f3f3f;
struct po
{
int nxt,to;
}edge[MAXN*2];
int head[MAXN],du[MAXN],cur[MAXN],n,m,stack[MAXN],vis[MAXN],l,cnt[MAXN],ans,num;
inline void add_edge(int from,int to)
{
edge[++num].nxt=head[from];
edge[num].to=to;
head[from]=num;
}
inline void add(int from,int to)
{
add_edge(from,to);
add_edge(to,from);
}
inline int read()
{
int x=0,c=1;
char ch=' ';
while((ch>'9'||ch<'0')&&ch!='-')ch=getchar();
while(ch=='-') c*=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-'0',ch=getchar();
return x*c;
}
queue<int> q;
inline void top()
{
for(re int i=1;i<=n;i++) if(du[i]==1) ++cnt[cur[i]=1],vis[i]=1,q.push(i);
while(!q.empty()){
int u=q.front();
q.pop();
for(re int i=head[u];i;i=edge[i].nxt){
int v=edge[i].to;
if(vis[v]) continue;
du[v]--;
if(du[v]==1) {
vis[v]=1;
++cnt[cur[v]=cur[u]+1];
q.push(v);
}
}
}
return;
}
int main()
{
n=read();l=read();
for(re int i=1;i<n;i++){
int x=read(),y=read();
du[x]++;du[y]++;
add(x,y);
}
top();
for(re int i=1;cnt[i];i++){
ans+=min(l<<1,cnt[i]);
}
cout<<ans;
return 0;
}

[POI2006]MET-Subway的更多相关文章

  1. bzoj 1517 [POI2006]Met 贪心

    [POI2006]Met Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 203  Solved: 108[Submit][Status][Discus ...

  2. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  3. 第一次作业——subway

    作业源程序代码:https://github.com/R-81/subway 作业程序使用说明:通过输入命令参数求解路线(仅支持-b,-c),根据参数得出路线后,程序不会结束,此时可输入地铁路线名(例 ...

  4. 2016 Multi-University Training Contest 1 J.Subway

    Subway Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Su ...

  5. [Android Tips] 23. How to fail/stop Gradle task immediately if some conditions are not met

    throw new GradleException("conditions are not met") 参考 How to fail/stop task immediately i ...

  6. Subway Icon Set – 306个像素完美的特制图标

    这个图标集是306个优化的像素完美,精雕细琢的图标.为这些设备进行了优化:iOS.Windows Phone.Windows 8 and BlackBerry 10,提供 PNG, SVG, XALM ...

  7. ural 1272. Non-Yekaterinburg Subway

    1272. Non-Yekaterinburg Subway Time limit: 1.0 secondMemory limit: 64 MB A little town started to co ...

  8. URAL(timus) 1272 Non-Yekaterinburg Subway(最小生成树)

    Non-Yekaterinburg Subway Time limit: 1.0 secondMemory limit: 64 MB A little town started to construc ...

  9. 【POJ】【1635】Subway Tree Systems

    树的最小表示法 给定两个有根树的dfs序,问这两棵树是否同构 题解:http://blog.sina.com.cn/s/blog_a4c6b95201017tlz.html 题目要求判断两棵树是否是同 ...

  10. 【BZOJ】【1520】【POI2006】Szk-Schools

    网络流/费用流 比较裸的一道题 依旧是二分图模型,由源点S连向每个学校 i (1,0),「注意是连向第 i 所学校,不是连向学校的标号m[i]……唉这里WA了一次」 然后对于每所学校 i 连接 j+n ...

随机推荐

  1. 69、ViewPagerIndicator+ViewPager实现Tab

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&q ...

  2. zoj3662(dp)

    dp还是比较好想的,但是时间还是比较坑的. 要预处理还加些优化才行 . #include <stdio.h> #include <stdlib.h> #include < ...

  3. 自己根据java的LinkedList源码编写的一个简单的LinkedList实现

    自己实现了一个简单的LinkedList /** * Create by andy on 2018-07-03 11:44 * 根据 {@link java.util.LinkedList}源码 写了 ...

  4. 【BZOJ2259】[Oibh]新型计算机 最短路

    [BZOJ2259][Oibh]新型计算机 Description Tim正在摆弄着他设计的“计算机”,他认为这台计算机原理很独特,因此利用它可以解决许多难题. 但是,有一个难题他却解决不了,是这台计 ...

  5. Leetcode-Resotre IP Addresses

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  6. Blog上一页和下一页的功能

    博客上一页和下一页的功能 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...

  7. unix_timestamp 和 from_unixtime 时间戳函数 区别

    1.unix_timestamp 将时间转化为时间戳.(date 类型数据转换成 timestamp 形式整数) 没传时间参数则取当前时间的时间戳 mysql> select unix_time ...

  8. WebService 入门

    1. 远程调用技术 2. WebService 概述 WebService 是使用 Http 发送 SOAP 协议数据的一种远程调用技术; WebService 需要开发客户端; WebService ...

  9. python2 打印菱形

    程序分析:先把图形分成两部分来看待,前四行一个规律,后三行一个规律,利用双重for循环,第一层控制行,第二层控制列. 程序源代码: #!/usr/bin/python # -*- coding: UT ...

  10. git 从远程仓库指定分支clone代码到本地

    不指定分支 git clone + clone 地址 # 例如 git clone https://amc-msra.visualstudio.com/xxx/_xx/xxxxxx 指定分支 git ...