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;
}

Russ Allbery: Review: Radiant...

Science fiction enthusiasts and literary critics alike are turning...

Why Pet Travel Gear...

📢 As an Amazon Associate, I earn from qualifying...

Mall Together-TENOKE

Simulation gaming enthusiasts have a fresh operational challenge to...

7 Reasons This Coffee...

📢 As an Amazon Associate, I earn from qualifying...

Why Kitchen Organization Tools...

📢 As an Amazon Associate, I earn from qualifying...

Is Dorm Room Hacks...

📢 As an Amazon Associate, I earn from qualifying...

Russ Allbery: Review: Radiant Star

Science fiction enthusiasts and literary critics alike are turning their eyes toward the upcoming release of Radiant Star, the latest novel by award-winning author...

Why Pet Travel Gear Is Currently Breaking the Internet

📢 As an Amazon Associate, I earn from qualifying purchases.After putting the Pet Travel Gear through a proper 30-day real-world test, I can tell...

Mall Together-TENOKE

Simulation gaming enthusiasts have a fresh operational challenge to tackle with the arrival of Mall Together-TENOKE. This brand-new release delivers an immersive shopping mall...

7 Reasons This Coffee Bar Accessories Is Worth Every Single Penny

📢 As an Amazon Associate, I earn from qualifying purchases.There comes a point when you realise that tolerating a frustrating situation is costing you...

Why Kitchen Organization Tools Is Currently Breaking the Internet

📢 As an Amazon Associate, I earn from qualifying purchases.There comes a point when you realise that tolerating a frustrating situation is costing you...

Is Dorm Room Hacks Worth Your Money Here Is Our Full Review

📢 As an Amazon Associate, I earn from qualifying purchases.If you had told me six months ago that a single purchase would change how...

Stop Buying Cheap Alternatives This Camping Essentials Is the Real Deal

📢 As an Amazon Associate, I earn from qualifying purchases.There comes a point when you realise that tolerating a frustrating situation is costing you...

Everything You Need to Know About Outdoor Patio Tech Right Now

📢 As an Amazon Associate, I earn from qualifying purchases.I'll be honest — I was skeptical at first. My daily routine had a gap...

Why Bathroom Organization Hacks Is Currently Breaking the Internet

📢 As an Amazon Associate, I earn from qualifying purchases.I'll be honest — I was skeptical at first. My daily routine had a gap...