Codeforces Round #408 (Div. 2) D
Posted 樱花落舞
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #408 (Div. 2) D相关的知识,希望对你有一定的参考价值。
Description
Inzane finally found Zane with a lot of money to spare, so they together decided to establish a country of their own.
Ruling a country is not an easy job. Thieves and terrorists are always ready to ruin the country‘s peace. To fight back, Zane and Inzane have enacted a very effective law: from each city it must be possible to reach a police station by traveling at most d kilometers along the roads.
There are n cities in the country, numbered from 1 to n, connected only by exactly n?-?1 roads. All roads are 1 kilometer long. It is initially possible to travel from a city to any other city using these roads. The country also has k police stations located in some cities. In particular, the city‘s structure satisfies the requirement enforced by the previously mentioned law. Also note that there can be multiple police stations in one city.
However, Zane feels like having as many as n?-?1 roads is unnecessary. The country is having financial issues, so it wants to minimize the road maintenance cost by shutting down as many roads as possible.
Help Zane find the maximum number of roads that can be shut down without breaking the law. Also, help him determine such roads.
The first line contains three integers n, k, and d (2?≤?n?≤?3·105, 1?≤?k?≤?3·105, 0?≤?d?≤?n?-?1) — the number of cities, the number of police stations, and the distance limitation in kilometers, respectively.
The second line contains k integers p1,?p2,?...,?pk (1?≤?pi?≤?n) — each denoting the city each police station is located in.
The i-th of the following n?-?1 lines contains two integers ui and vi (1?≤?ui,?vi?≤?n, ui?≠?vi) — the cities directly connected by the road with index i.
It is guaranteed that it is possible to travel from one city to any other city using only the roads. Also, it is possible from any city to reach a police station within d kilometers.
In the first line, print one integer s that denotes the maximum number of roads that can be shut down.
In the second line, print s distinct integers, the indices of such roads, in any order.
If there are multiple answers, print any of them.
6 2 4
1 6
1 2
2 3
3 4
4 5
5 6
1
5
6 3 2
1 5 6
1 2
1 3
1 4
1 5
5 6
2
4 5
In the first sample, if you shut down road 5, all cities can still reach a police station within k?=?4 kilometers.
In the second sample, although this is the only largest valid set of roads that can be shut down, you can print either 4 5 or 5 4 in the second line.
题意:告诉你哪些点是警察局,由于资金原因需要删除一些道路,不过需要满足其他点到警察局的距离小于d
解法:BFS,其实d是无关的,我们只需要根据每个警察局进行BFS遍历,然后哪些路没走过就输出,(为什么d无关,我也不知道)
#include<bits/stdc++.h> using namespace std; int vis[400000]; int n,k,m,d; int flag[400000]; queue<int>q; vector<pair<int,int>>p[400000]; int main() { scanf("%d%d%d",&n,&m,&d); for(int i=1;i<=m;i++) { int num; scanf("%d",&num); vis[num]=1; q.push(num); } for(int i=1;i<=n-1;i++) { int u,v; scanf("%d%d",&v,&u); p[u].push_back({v,i}); p[v].push_back({u,i}); } while(!q.empty()) { int x=q.front(); q.pop(); // cout<<x<<endl; for(int i=0;i<p[x].size();i++) { int ans=p[x][i].first; int cnt=p[x][i].second; if(vis[ans]) continue; vis[ans]=1; flag[cnt]=1; q.push(ans); } } //cout<<endl; int cot=0; for(int i=1;i<n;i++) { if(!flag[i]) { cot++; } } printf("%d\n",cot); for(int i=1;i<n;i++) { if(!flag[i]) { printf("%d ",i); } } return 0; }
以上是关于Codeforces Round #408 (Div. 2) D的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #408 (Div. 2) A
Codeforces Round #408 (Div. 2) B
Codeforces Round #408 (Div. 2)ABCD