题意:
告诉你一棵树, 告诉你某些结点为警察局, 其余的为普通点 , 普通点到每个警察局的距离都小于等于d,现在要求删除尽可能多的边,使得每个点到警察局的距离依然小于等于d,输出数量以及哪些边?
思路:
直接从警察局开始bfs , 访问的不在访问,这样每个访问到的边 就是要保留的边,没访问到的就是要删除的。
#include <bits/stdc++.h>
#define Siz(x) (int)x.size()
using namespace std;
const int maxn = 3e5 + 10;
int n, k, d;
queue<int>q;
int vis[maxn];
int way[maxn];
map<pair<int,int> , int>mp;
vector<int>g[maxn];
int main(){
int x,y;
scanf("%d%d",&x, &y);
return 0 * printf("%d\n",x+y);
scanf("%d %d %d",&n, &k, &d);
for (int i = 0; i < k; ++i){
int x;
scanf("%d",&x);
vis[x] = 1;
q.push(x);
}
for (int i = 1; i < n; ++i){
int u,v;
scanf("%d %d",&u, &v);
g[u].push_back(v);
g[v].push_back(u);
mp[make_pair(u,v)] = i;
mp[make_pair(v,u)] = i;
}
while(!q.empty()){
int u = q.front(); q.pop();
for (int i = 0; i < Siz(g[u]); ++i){
int v = g[u][i];
if (!vis[v]){
vis[v] = 1;
q.push(v);
way[mp[make_pair(u,v)] ] = 1;
way[mp[make_pair(v,u)] ] = 1;
}
}
}
int ans = 0;
for (int i = 1; i < n; ++i){
if (!way[i])++ans;
}
printf("%d\n",ans);
for (int i = 1; i < n; ++i){
if (!way[i]){
printf("%d ", i);
}
}
putchar('\n');
return 0;
}
D. Police Stations
time limit per test
memory limit per test
input
output
Inzane finally found Zane with a lot of money to spare, so they together decided to establish a country of their own.
d
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
n - 1
Help Zane find the maximum number of roads that can be shut down without breaking the law. Also, help him determine such roads.
Input
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.
k integers p1, p2, ..., pk (1 ≤ pi ≤ n) — each denoting the city each police station is located in.
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.
d
Output
s
s
If there are multiple answers, print any of them.
Examples
input
6 2 4
1 6
1 2
2 3
3 4
4 5
5 6
output
1 5
input
6 3 2
1 5 6
1 2
1 3
1 4
1 5
5 6
output
2 4 5
Note
5, all cities can still reach a police station within k = 4
4 5 or 5 4