96D - Police Stations
思路:bfs,从所有的警察局开始bfs,因为bfs的深度一样,而且题目给的树保证满足条件,所以不用考虑深度。
如果搜索到一个点a,他的下一个点b已经被搜索过了,而且a到b这条路没有被走过,那么这条路可以被砍掉。
不能用dfs,这样可能导致某些点搜索不到,反例读者自己找。
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a)) const int N=3e5+;
bool vis[N]={false};
bool vs[N]={false};
vector<int>g[N];
vector<int>edge[N];
vector<int>ans;
int n,k,d,a,u,v;
queue<int>q; void bfs()
{
int now;
int nxt;
while(!q.empty())
{
now=q.front();
q.pop();
for(int i=;i<g[now].size();i++)
{
if(!vis[g[now][i]])vis[g[now][i]]=true,vs[edge[now][i]]=true,q.push(g[now][i]);
else if(!vs[edge[now][i]])ans.pb(edge[now][i]);
}
}
} int main()
{
ios::sync_with_stdio(false);
cin.tie();
cin>>n>>k>>d;
for(int i=;i<k;i++)cin>>a,vis[a]=true;
for(int i=;i<n-;i++)cin>>u>>v,g[u].pb(v),g[v].pb(u),edge[v].pb(i+),edge[u].pb(i+); for(int i=;i<=n;i++)if(vis[i])q.push(i);
bfs();
sort(ans.begin(),ans.end());
ans.erase(unique(ans.begin(),ans.end()),ans.end());
cout<<ans.size()<<'\n';
for(int i=;i<ans.size();i++)cout<<ans[i]<<' ';
cout<<'\n';
return ;
}
96D - Police Stations的更多相关文章
- 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条,即 ...
- Police Stations CodeForces - 796D (bfs)
大意: 给定树, 有k个黑点, 初始满足条件:所有点到最近黑点距离不超过d, 求最多删除多少条边后, 使得原图仍满足条件. 所有黑点开始bfs, 贪心删边. #include <iostream ...
- Codeforces Round #408 (Div. 2) D. Police Stations(最小生成树+构造)
传送门 题意 n个点有n-1条边相连,其中有k个特殊点,要求: 删去尽可能多的边使得剩余的点距特殊点的距离不超过d 输出删去的边数和index 分析 比赛的时候想不清楚,看了别人的题解 一道将1个联通 ...
- 【codeforces 796D】Police Stations
[题目链接]:http://codeforces.com/contest/796/problem/D [题意] 在一棵树上,保证每个点在距离d之内都有一个警察局; 让你删掉最多的边,使得剩下的森林仍然 ...
- CF796D Police Stations BFS+染色
题意:给定一棵树,树上有一些点是警察局,要求所有点到最近的警察局的距离不大于 $d$,求最多能删几条边 ? 题解: 考虑什么时候一条边可以被断开:这条边的两个端点被两个不同的警察局覆盖掉. 我们要设计 ...
- ZOJ 2699 Police Cities
Police Cities Time Limit: 10 Seconds Memory Limit: 32768 KB Once upon the time there lived a ki ...
- Codeforces Round #408 (Div. 2)
C. Bank Hacking 题目大意:给出一棵n个节点的树,每个节点有一个权值,删掉一个点的代价为当前这个点的权值,并且会使其相邻点和距离为2且中间隔着未被删除的点的点权值加1,现在选一个点开始删 ...
随机推荐
- c++移动构造函数
写在前面 C++中有“左值”.“右值”的概念,C++11以后,又有了“左值”.“纯右值”.“将亡值”的概念.关于这些概念,许多资料上都有介绍,本文在拾人牙慧的基础上又加入了一些自己的一些理解,同时提出 ...
- Leetcode: Construct Binary Tree from Preorder and Inorder Transversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that ...
- DW课堂练习 用所学的知识去制作一个 (邮箱的注册页面)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- php深入学习
关于PHP程序员解决问题的能力 http://rango.swoole.com/archives/340 深入理解PHP内核 by xuhong大牛 http://www.php-internals. ...
- tfs项目解绑及svn上传
1.tfs解绑 file--源代码管理——tfs解绑 2.svn将本地的文件夹上传到server 右击--import--url--新建文件夹
- python使用WSGI接口实现简单网页
Python Web 介绍 Python的Web服务器分为服务器程序和应用程序.服务器程序负责接收客户端的请求发送给应用程序,应用程序负责处理请求返回给服务器程序.为了方便应用程序的开发,我们把常用的 ...
- 主成分分析(PCA)学习笔记
这两天学习了吴恩达老师机器学习中的主成分分析法(Principal Component Analysis, PCA),PCA是一种常用的降维方法.这里对PCA算法做一个小笔记,并利用python完成对 ...
- thinkphp相关
thinkphp相关1.thinkphp调试sql方法:echo M("table_name")->getLastSql(); 2. 条件查询设置多个条件参数的写法:(1). ...
- 线程属性pthread_attr_t简介
本文编辑整理自: http://hi.baidu.com/7828058/blog/item/256e16decd1a385e94ee3784.html http://www.ibm.com/deve ...
- Bootstrap+AngularJS对话框实例
<script type="text/javascript" src="/assets/JS/plugins/jquery.min.js">< ...