【题目链接】:http://codeforces.com/contest/796/problem/D

【题意】



在一棵树上,保证每个点在距离d之内都有一个警察局;

让你删掉最多的边,使得剩下的森林仍然满足上述约束;

【题解】



从每个警察局开始进行bfs;

这样每个路线第一次到达的点肯定是最短路;

所以bfs的时候遇到没访问过的点,就记录它被访问,即它被这个状态一开始的警察局所管辖(控制);

则可以想象每个警察局都同时往外扩张;

则每个警察局管的范围就确定是那些了;

则那些点肯定是到那个管辖它的警察局最近的;

这样就能确定最后哪些边是要保留下来的了;

删掉剩余的边就好;

(每个点开始找最短路是哪条边的,那条边就保留下来);

警察局不用分配给它边.



【Number Of WA】



0



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)
#define ms(x,y) memset(x,y,sizeof x) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 3e5+1000; int n,k,d,dis[N];
int p[N];
vector <pii> G[N];
queue <int> dl;
bool bo[N]; int main()
{
//freopen("F:\\rush.txt","r",stdin);
ms(dis,255);
rei(n),rei(k),rei(d);
rep1(i,1,k)
rei(p[i]),dl.push(p[i]),dis[p[i]] = 0;
rep1(i,1,n-1)
{
int x,y;
rei(x),rei(y);
G[x].ps(mp(y,i)),G[y].ps(mp(x,i));
}
while (!dl.empty())
{
int x = dl.front();
dl.pop();
for (pii temp:G[x])
{
int y = temp.fi;
if (dis[y]==-1)
{
dis[y] = dis[x]+1;
dl.push(y);
}
}
}
int cnt = 0;
rep1(i,1,n)
if (dis[i]!=0)
{
int mi = 21e8,id = -1;
for (pii temp:G[i])
{
int y = temp.fi;
if (dis[y]<mi)
{
mi = dis[y],id = temp.se;
}
}
cnt++;
bo[id] = true;
}
printf("%d\n",n-1-cnt);
rep1(i,1,n-1)
if (!bo[i])
{
printf("%d ",i);
}
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}

【codeforces 796D】Police Stations的更多相关文章

  1. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  2. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  3. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  4. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  5. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  6. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

  7. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

  8. 【Codeforces 670C】 Cinema

    [题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...

  9. 【codeforces 515D】Drazil and Tiles

    [题目链接]:http://codeforces.com/contest/515/problem/D [题意] 给你一个n*m的格子; 然后让你用1*2的长方形去填格子的空缺; 如果有填满的方案且方案 ...

随机推荐

  1. CodePlus 2017 12 月赛

    这场比赛跟个zz一样 div1卡在了同余方程上 心态崩了去做div2 然后被T1搞崩了 T1: 大模拟 比较像配平方程式 思路: 但是未知物质每种元素系数不能≥10 且不能为空 (如CO2+?=CO2 ...

  2. POJ3420 递推+矩阵快速幂

    POJ3420 很有趣的覆盖问题 递归推导如下: f[n] = f[n-1] + 4*f[n-2] + 2 * [ f[n-3] + f[n-5] + f[n-7] +.... ] + 3 *  [ ...

  3. 洛谷 P1312 [ NOIP 2011 ] Mayan游戏 —— 搜索+模拟

    题目:https://www.luogu.org/problemnew/show/P1312 还是不擅长这种题,所以参考了一下TJ: 其实也很好搜,按字典序,先搜右移,再搜左移: 不交换相同颜色的两个 ...

  4. it人必进的几大网站

    1.chinaunix网址:http://www.chinaunix.net/简介:中国最大的linux/unix技术社区. 2.itpub网址:http://www.itpub.net/ 简介:有名 ...

  5. MySQL 字符编码问题详细解释

    http://www.codesoil.net/tag/charset Character Set Problem in PHP + MySQL4.1+ 和许多人一样,我也是在转移blog时才发现这个 ...

  6. CSS小代码汇总整理

    /**实现斑马线的表格*/table.flexme tbody tr:nth-child(2n){background-color:#D6E7FC;} /*返回偶数序的子元素*/table.flexm ...

  7. 【转载】Sybase数据库服务器端安装

    sybase数据库的安装分为服务器端和客户端,本文先介绍一下服务器端的安装. 1.和其他程序一样,双击setup.exe.   2.出现欢迎界面,直接点击next即可.   3.下面选择相应国家的协议 ...

  8. 【WIP】Ruby CSV文件操作

    创建: 2017/09/30                                                                                       ...

  9. thinkphp结合云之讯做短信验证码

    thinkphp结合云之讯做短信验证码先去云之讯注册账号 网址http://www.ucpaas.com/ 注册云之讯平台账号,即可免费获得10元测试费用测试够用啦 解压附件到 ThinkPHP\Li ...

  10. TypeScript `infer` 关键字

    考察如下类型: type PromiseType<T> = (args: any[]) => Promise<T>; 那么对于符合上面类型的一个方法,如何得知其 Prom ...