CodeForces - 796D Police Stations bfs
思路:删除尽量多的边使得所有点都能在限制距离之内到达一个警局,删除边会形成多棵子树,最多只能k棵。其实就是以每个警局为根结点,把整棵树划分为以警局为根结点的k棵树,说明要删除的边的数量就是k-1条,即删除的边的条数是一定的。剩下就是为每个节点找根结点,考虑从所有警局出发得到到每个点的最短距离,则当前节点u,一定是从u->v,如果d[v] <= lim则这条边一定会保留。
AC代码
#include <cstdio>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int>
typedef long long LL;
const int maxn = 3e5 + 5;
int vis[maxn], d[maxn];
int n, k, lim;
map<PI, int>ha;
vector<int>G[maxn];
queue<int>q;
int main() {
while(scanf("%d%d%d", &n, &k, &lim) == 3) {
ha.clear();
while(!q.empty()) q.pop();
for(int i = 1; i <= n; ++i) G[i].clear();
memset(d, -1, sizeof(d));
int pos;
for(int i = 0; i < k; ++i) {
scanf("%d", &pos);
d[pos] = 0;
q.push(pos);
}
int u, v;
for(int i = 0; i < n-1; ++i) {
scanf("%d%d", &u, &v);
ha[make_pair(u, v)] = i+1;
ha[make_pair(v, u)] = i+1;
G[u].push_back(v);
G[v].push_back(u);
}
memset(vis, 0, sizeof(vis));
int cnt = 0; //要保留的边的数量
while(!q.empty()) {
int u = q.front(); q.pop();
for(int i = 0; i < G[u].size(); ++i) {
int v = G[u][i];
if(d[v] == -1) {
d[v] = d[u] + 1;
q.push(v);
if(d[v] <= lim) {
int id = ha[make_pair(u, v)];
vis[id] = 1;
++cnt;
}
}
}
}
printf("%d\n", n-cnt-1);
//printf("%d\n", k-1);
for(int i = 1; i <= n-1; ++i) {
if(!vis[i]) printf("%d ", i);
}
printf("\n");
}
return 0;
}
如有不当之处欢迎指出!
CodeForces - 796D Police Stations bfs的更多相关文章
- CF796D Police Stations BFS+染色
题意:给定一棵树,树上有一些点是警察局,要求所有点到最近的警察局的距离不大于 $d$,求最多能删几条边 ? 题解: 考虑什么时候一条边可以被断开:这条边的两个端点被两个不同的警察局覆盖掉. 我们要设计 ...
- Codeforces Round #408 (Div. 2) D - Police Stations
地址:http://codeforces.com/contest/796/problem/D 题目: D. Police Stations time limit per test 2 seconds ...
- 96D - Police Stations
96D - Police Stations 思路:bfs,从所有的警察局开始bfs,因为bfs的深度一样,而且题目给的树保证满足条件,所以不用考虑深度. 如果搜索到一个点a,他的下一个点b已经被搜索过 ...
- CF796D Police Stations 思维
Inzane finally found Zane with a lot of money to spare, so they together decided to establish a coun ...
- Police Stations CodeForces - 796D (bfs)
大意: 给定树, 有k个黑点, 初始满足条件:所有点到最近黑点距离不超过d, 求最多删除多少条边后, 使得原图仍满足条件. 所有黑点开始bfs, 贪心删边. #include <iostream ...
- 【codeforces 796D】Police Stations
[题目链接]:http://codeforces.com/contest/796/problem/D [题意] 在一棵树上,保证每个点在距离d之内都有一个警察局; 让你删掉最多的边,使得剩下的森林仍然 ...
- Codeforces Round #408 (Div. 2) D. Police Stations(最小生成树+构造)
传送门 题意 n个点有n-1条边相连,其中有k个特殊点,要求: 删去尽可能多的边使得剩余的点距特殊点的距离不超过d 输出删去的边数和index 分析 比赛的时候想不清楚,看了别人的题解 一道将1个联通 ...
- Codeforces 343E Pumping Stations
Description 题面 题目大意:求一个排列 \(P\),使得 \(\sum_{i=1}^{n-1}maxflow(P_i,P_{i+1})\) 尽量大 Solution 构造出最小割树,那么第 ...
- Vladik and Favorite Game CodeForces - 811D (思维+BFS+模拟+交互题)
D. Vladik and Favorite Game time limit per test 2 seconds memory limit per test 256 megabytes input ...
随机推荐
- python_如何对迭代器进行切片操作
案例: 对于某个文件,我只想读取到其中100~200行之间的内容,是否可以通过切片的方式进行读取? 我想: f = open() f[100:200] 可行? 如何解决这个问题? 方法1: 全部读取到 ...
- python 中 reversed()函数
一个列表a: a=[1,2,3,4,5,6,7] 一个对象b: b=reversed(a) 输出: print(b) <list_reverseiterator object at 0x0000 ...
- tp5中设置指定的log日志,可单独建立文件夹和文件名
1:在D:\www\tp5\thinkphp\library\think\Log.php中添加下列代码.可在runtime文件夹下建立tlogs文件夹(可自定义). /** * [payLog 支付日 ...
- DirectX SDK (June 2010)安装错误S1023的一个解决方法
在安装DXSDK_Jun10.exe时一个常见的安装失败的代号是S1023,一般出现这种错误的原因是系统中已经安装了Visual Studio 2010及以上的版本. 在[控制面板]中找到这两个: 如 ...
- TensorFlow-相关 API(学习笔记 )
1.tf.nn.conv2d conv2d( input, filter, strides, padding, use_cudnn_on_gpu=True, data_format='NHWC', n ...
- 统一网络控制器Func
一.简介 二.安装 三.测试 一.简介 什么是Func? Func是由红帽子公司以Fedora平台构建的统一网络控制器,是为解决集群管理.监控问题而设计开发的系统管理基础框架.它是一个能有效简化多服务 ...
- 树莓派小车By 树莓派爱好者ITJoker(通过python socket通信实现树莓派视频小车)(一)
本文由树莓派爱好者ITJoker 编辑,转载请注明出处.本人也有新浪博客同样是树莓派爱好者ITJoker 所需材料:树莓派2B或者2B以上,L2985n驱动板,若干排线,电池及电池盒,usb无线网卡( ...
- javascript中的BOM对象
1.window对象 所有的浏览器都支持window对象 概念上讲,一个html文档对应一个window对象 功能上讲,控制浏览器窗口 使用上讲,window对象不需要创建对象,直接使用 2.wind ...
- 2017年总结的前端文章——CSS盒模型详解
CSS的盒模型是CSS的基础,同时也是难点,这个问题经常在面试中会被问到,属于经典问题了.很多博客里讲得也很模糊不清,于是,我在这里重新整理一下. 可以认为每个html标签都是一个方块,然后这个方块又 ...
- Annotation基础知识
1.Annotation介绍 Annontation是Java5开始引入的新特征.中文名称一般叫注解. Annontation像一种修饰符一样,应用于包.类型.构造方法.方法.成员变量.参数及本地变量 ...