-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path19538.cpp
More file actions
64 lines (54 loc) · 1.43 KB
/
19538.cpp
File metadata and controls
64 lines (54 loc) · 1.43 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
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
using namespace std;
#define MAXN 200050
int N,M,ans[MAXN];
vector<int> adj[MAXN];
bool checked[MAXN];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin >> N;
for(int i=1 ; i<=N ; i++){
while(1){
int a; cin >> a;
if(!a) break;
adj[i].push_back(a);
}
}
fill_n(ans,N+5, -1);
queue<pair<int,int>> q;
cin >> M;
for(int i=0 ; i<M ; i++){
int a; cin >> a;
q.push({a, 0});
ans[a] = 0;
checked[a] = true;
}
while(!q.empty()){
pair<int,int> top = q.front();
q.pop();
vector<int> to_check;
for(auto v: adj[top.first]){
if(checked[v]) continue;
if(ans[v] == -1){
int surr = 0;
for(int i=0 ; i<adj[v].size() ; i++){
if(checked[adj[v][i]] &&ans[adj[v][i]] != -1 && ans[adj[v][i]] <= top.second) surr += 1;
}
if(surr >= (adj[v].size()+1)/2){
q.push({v, top.second+1});
ans[v] = top.second+1;
to_check.push_back(v);
}
}
}
for(int i=0 ; i<to_check.size() ; i++){
checked[to_check[i]] = true;
}
}
for(int i=1 ; i<=N ; i++) cout << ans[i] << " ";
}