-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeforces_Police_Stations.cpp
More file actions
70 lines (57 loc) · 1.66 KB
/
Copy pathCodeforces_Police_Stations.cpp
File metadata and controls
70 lines (57 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
void solve(){
int cities,stationCnt,distanceMax;
cin>>cities>>stationCnt>>distanceMax;
vector<bool> visited(cities+1,false);
vector<int> parent(cities+1,-1);
queue<int> q;
for(int i=0; i<stationCnt; i++){
int policeStation;
cin>>policeStation;
q.push(policeStation);
parent[policeStation]=0;
}
vector<set<pair<int,int>>> adjList(cities+1);
for(int i=1; i<cities; i++){
int city1,city2;
cin>>city1>>city2;
adjList[city1].insert({city2,i});
adjList[city2].insert({city1,i});
}
vector<int> roads(cities+1,0);
while(!q.empty()){
int current=q.front();
q.pop();
if(visited[current]) continue;
visited[current]=true;
for(auto neighbour: adjList[current]){
if(neighbour.first!=parent[current]){
if(visited[neighbour.first])
roads[neighbour.second]=1;
else{
q.push(neighbour.first);
if(parent[neighbour.first]==-1)
parent[neighbour.first]=current;
}
}
}
}
int removedRoadCnt=0;
for(int i=1; i<=cities; i++)
if(roads[i]) removedRoadCnt++;
cout<<removedRoadCnt<<endl;
for(int i=1; i<=cities; i++)
if(roads[i]) cout<<i<<" ";
cout<<endl;
}
signed main(){
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
optimize();
solve();
return 0;
}