Police Stations CodeForces - 796D (bfs)
大意: 给定树, 有k个黑点, 初始满足条件:所有点到最近黑点距离不超过d, 求最多删除多少条边后, 使得原图仍满足条件.
所有黑点开始bfs, 贪心删边.
#include <iostream>
#include <algorithm>
#include <math.h>
#include <cstdio>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <string.h>
#include <queue>
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define hr cout<<'\n'
#define pb push_back
#define mid ((l+r)>>1)
#define lc (o<<1)
#define rc (lc|1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false);
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
void exgcd(ll a,ll b,ll &d,ll &x,ll &y){b?exgcd(b,a%b,d,y,x),y-=a/b*x:x=1,y=0,d=a;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head const int N = 4e5+10, INF = 0x3f3f3f3f;
int n, k, d, t;
int vis[N], vis2[N];
vector<pii> g[N]; int main() {
scanf("%d%d%d", &n, &k, &d);
REP(i,1,k) scanf("%d", &t), vis[t]=1;
REP(i,1,n-1) {
int u, v;
scanf("%d%d", &u, &v);
g[u].pb({v,i}),g[v].pb({u,i});
}
queue<int> q;
vector<int> ans;
REP(i,1,n) if (vis[i]) q.push(i);
while (!q.empty()) {
int u = q.front();q.pop();
for (auto e:g[u]) {
if (vis2[e.y]) continue;
if (vis[e.x]) ans.pb(e.y);
else q.push(e.x);
vis[e.x] = vis2[e.y] = 1;
}
}
printf("%d\n",int(ans.size()));
for (int t:ans) printf("%d ",t);hr;
}
Police Stations CodeForces - 796D (bfs)的更多相关文章
- 96D - Police Stations
96D - Police Stations 思路:bfs,从所有的警察局开始bfs,因为bfs的深度一样,而且题目给的树保证满足条件,所以不用考虑深度. 如果搜索到一个点a,他的下一个点b已经被搜索过 ...
- Codeforces Round #408 (Div. 2) D - Police Stations
地址:http://codeforces.com/contest/796/problem/D 题目: D. Police Stations time limit per test 2 seconds ...
- CF796D Police Stations 思维
Inzane finally found Zane with a lot of money to spare, so they together decided to establish a coun ...
- CodeForces - 796D Police Stations bfs
思路:删除尽量多的边使得所有点都能在限制距离之内到达一个警局,删除边会形成多棵子树,最多只能k棵.其实就是以每个警局为根结点,把整棵树划分为以警局为根结点的k棵树,说明要删除的边的数量就是k-1条,即 ...
- 【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个联通 ...
- CF796D Police Stations BFS+染色
题意:给定一棵树,树上有一些点是警察局,要求所有点到最近的警察局的距离不大于 $d$,求最多能删几条边 ? 题解: 考虑什么时候一条边可以被断开:这条边的两个端点被两个不同的警察局覆盖掉. 我们要设计 ...
- 796D(bfs)
题目链接: http://codeforces.com/problemset/problem/796/D 题意: 给出一颗 n 个节点树, 树枝连接的两个定点距离为 1, 树中有 k 个特殊点, 问最 ...
- Arthur and Walls CodeForces - 525D (bfs)
大意: 给定格点图, 每个'.'的连通块会扩散为矩形, 求最后图案. 一开始想得是直接并查集合并然后差分, 但实际上是不对的, 这个数据就可以hack掉. 3 3 **. .** ... 正解是bfs ...
随机推荐
- axios实现拦截器
项目中通常使用token进行用户权限认证,需要在请求的header中添加token信息进行验证,拦截返回的状态码进行跳转或重新登陆,在全局配置这些不妥,所以新建一个axios实例进行项目的配置. // ...
- Java-idea-mybatis plugin插件使用
方案一.免费插件[推荐] Free Mybatis plugin 方案二.破解插件 安装路径 File→Setting→plugin→Install plugin 搜索需要插件即可 搜索Mybati ...
- ARIMA模型总结
时间序列建模基本步骤 获取被观测系统时间序列数据: 对数据绘图,观测是否为平稳时间序列:对于非平稳时间序列要先进行d阶差分运算,化为平稳时间序列: 经过第二步处理,已经得到平稳时间序列.要对平稳时间序 ...
- ubuntu常用软件命令
解压zip软件 unzip xxx.zip -d解压到指定目录 清理磁盘空间 sudo apt-get autoremove sudo apt-get clean sudo dpkg --list ...
- linux----------linux的scp命令介绍
1.scp是有Security的文件copy,基于ssh登录. 命令基本格式: scp [OPTIONS] file_source file_target OPTIONS: -v 和大多数 linux ...
- Unity之流光效果
效果如图: shader如下: Shader "Unlit/Walk light" { Properties { _MainTex ("Base (RGB), Alpha ...
- tensorflow 的数据管理
tensorflow api操纵和管理的是numpy矩阵数据 例子: import tensorflow as tf import numpy as np vector_np = np.array([ ...
- delphi “div”、“mod”、“\”除法运算符的区别与使用方法(附带FORMAT使用方法)
Delphi中和除法相关的算术运算符有: div.mod和符号“\” 下面分别对他们的作用.操作数类型和返回值类型进行一下介绍: div:对2个整数进行除,取商,操作数需是integer类型,返回值也 ...
- Delphi中类的运行期TypeInfo信息结构说明(转载)
Delphi中类的运行期TypeInfo信息结构说明作者:刘啸CnPack开发组 http://www.cnpack.org关键字:RTTI, TypeInfo, TypeData, PropInfo ...
- 实现Runnable接口创建多线程及其优势
实现Runnable接口创建多线程: 创建一个Runnable接口的实现类RunnableImpl: 主线程中: 其中,链式编程的Thread类的静态方法currentThread方法点getName ...