https://i1.wp.com/eblogarithm.com/wp-content/uploads/2017/04/Codeforces-Round-408-Div-2-problem-D-Police-Stations-Solution-in-C1.png?fit=640%2C360

Codeforces Round #408 (Div. 2), problem: (D) Police Stations Solution in C

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <numeric>
#include <climits>

using namespace std;

int main(int argc, char **argv)
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int N, K, D;
cin >> N >> K >> D;
vector<int> P(N, N);
vector<vector<int>> E(N);
map<pair<int, int>, int> M;
vector<bool> R(N);
queue<pair<int, int>> Q;
for (int i = 0; i < K; i++) {
int a;
cin >> a;
a–;
if (!P[a])
continue;
P[a] = 0;
Q.push({a, -1});
}
for (int i = 1; i < N; i++) {
int u, v;
cin >> u >> v;
u–;
v–;
E[u].push_back(v);
E[v].push_back(u);
M[make_pair(min(u, v), max(u, v))] = i;
}
while (!Q.empty()) {
auto q = Q.front();
Q.pop();
auto a = q.first;
for (auto b: E[a]) {
if (b == q.second)
continue;
if (P[b] > P[a] + 1) {
P[b] = P[a] + 1;
Q.push({b, a});
}
else {
int r = M[make_pair(min(a, b), max(a, b))];
R[r] = true;
}
}
}
int b = 0;
for (int i = 1; i < N; i++)
if (R[i])
b++;
cout << b << endl;
for (int i = 1; i < N; i++)
if (R[i])
cout << i << ‘ ‘;
cout << endl;
return 0;
}

(Visited 38 times, 1 visits today)



There are no comments

Add yours

Leave a Reply

%d bloggers like this: