【codeforces 796D】Police Stations
【题目链接】: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的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
- 【Codeforces 670C】 Cinema
[题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...
- 【codeforces 515D】Drazil and Tiles
[题目链接]:http://codeforces.com/contest/515/problem/D [题意] 给你一个n*m的格子; 然后让你用1*2的长方形去填格子的空缺; 如果有填满的方案且方案 ...
随机推荐
- js的类库
prototype.js https://github.com/sstephenson/prototype moment js https://github.com/moment/moment thr ...
- Codeforces--400A--Inna and Choose Options(模拟水题)
Inna and Choose Options Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:26 ...
- AAC的AudioSpecificConfig细节
AAC格式里有个复杂的AudioSpecificConfig, 在FLV格式里称为AAC sequence header.在正式播放ADTS AAC数据包之前,需要用AudioSpecificConf ...
- 【转载】HashMap底层实现原理及面试问题
①HashMap的工作原理 HashMap基于hashing原理,我们通过put()和get()方法储存和获取对象.当我们将键值对传递给put()方法时,它调用键对象的hashCode()方法来计算h ...
- [Swift通天遁地]五、高级扩展-(4)快速生成Invert、Mix、Tint、Shade颜色及调整饱和度阶
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- 微信小程序后台获取用户的opeid
1.微信小程序后台获取登录用户的openid,首先微信小程序将code传给后台服务器 wx.login({ success: function (res) { var code = res.code ...
- ACM_新七步诗(深搜)
新七步诗 Time Limit: 2000/1000ms (Java/Others) Problem Description: 突然的一天,小锴做了一个梦,梦见自己来到了三国,而自己也成了梦寐以求的帅 ...
- 本地mongochef连接其他计算机上的数据库认证失败解决方法
关闭防火墙或者在信任程序列表添加运行目录下的mongod.exe即可
- C# linq学习【转】
在说LINQ之前必须先说说几个重要的C#语言特性 一:与LINQ有关的语言特性 1.隐式类型 (1)源起 在隐式类型出现之前, 我们在声明一个变量的时候, 总是要为一个变量指定他的类型 甚至在fore ...
- 《Java编程的逻辑》第四部分 文件