[POI2006]MET-Subway
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的更多相关文章
- bzoj 1517 [POI2006]Met 贪心
[POI2006]Met Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 203 Solved: 108[Submit][Status][Discus ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- 第一次作业——subway
作业源程序代码:https://github.com/R-81/subway 作业程序使用说明:通过输入命令参数求解路线(仅支持-b,-c),根据参数得出路线后,程序不会结束,此时可输入地铁路线名(例 ...
- 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 ...
- [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 ...
- Subway Icon Set – 306个像素完美的特制图标
这个图标集是306个优化的像素完美,精雕细琢的图标.为这些设备进行了优化:iOS.Windows Phone.Windows 8 and BlackBerry 10,提供 PNG, SVG, XALM ...
- ural 1272. Non-Yekaterinburg Subway
1272. Non-Yekaterinburg Subway Time limit: 1.0 secondMemory limit: 64 MB A little town started to co ...
- URAL(timus) 1272 Non-Yekaterinburg Subway(最小生成树)
Non-Yekaterinburg Subway Time limit: 1.0 secondMemory limit: 64 MB A little town started to construc ...
- 【POJ】【1635】Subway Tree Systems
树的最小表示法 给定两个有根树的dfs序,问这两棵树是否同构 题解:http://blog.sina.com.cn/s/blog_a4c6b95201017tlz.html 题目要求判断两棵树是否是同 ...
- 【BZOJ】【1520】【POI2006】Szk-Schools
网络流/费用流 比较裸的一道题 依旧是二分图模型,由源点S连向每个学校 i (1,0),「注意是连向第 i 所学校,不是连向学校的标号m[i]……唉这里WA了一次」 然后对于每所学校 i 连接 j+n ...
随机推荐
- ZBarReaderView屏幕旋转问题
转载:http://42.96.197.72/ios-zbarreaderview-interface-orientation/ 在iPad应用中,如果没有特殊情况,需要让应用支持所有屏幕方向.在iP ...
- python中WSGI是什么
uswgi学习文档 http://uwsgi-docs-cn.readthedocs.io/zh_CN/latest/WSGIquickstart.html WSGI是什么? WSGI,全称 Web ...
- zoj3662(dp)
dp还是比较好想的,但是时间还是比较坑的. 要预处理还加些优化才行 . #include <stdio.h> #include <stdlib.h> #include < ...
- OpenvSwitch自动化重新编译和安装
相信使用过OpenvSwitch的人都知道,OpenvSwitch因为要替换一部分linux内核,所以在修改OpenvSwitch源码的时候,每次都需要重新编译和安装,这个过程十分的机械和枯燥,所以写 ...
- 回顾.NET Remoting分布式开发
记得在下第一次接触.NET Remoting分布式开发是在2003年,那时候是Framework1.0初次亮相之时,Remoting分布式开发是Framework1.0其中一个亮点.经过多年的发展,在 ...
- FineReport----查询功能 的知识点
1.设置日期控件,默认当前日期 2.默认不查询 选择参数:点击查询前不显示报表内容
- I/O多路复用技术(multiplexing)是什么?
作者:知乎用户链接:https://www.zhihu.com/question/28594409/answer/52763082来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...
- json.dumps 和 json.dump的区别,load和loads的区别
json.dumps 和 json.dump的区别,load和loads的区别
- Set 接口常用子类及其特点
Set 集合中元素不可重复,是无序的(存入和取出的顺序是不一样的), Set 接口中的方法和 Collection 接口一致. 常用子类: HashSet : 内部数据结构是哈希表, 是不同步的 Li ...
- 汇编学习笔记(AT&T语法)
一个最基本的汇编程序如下所示: .section .data .section .text .globl _start _start: movl $, %eax # the number 1 is t ...