-
Notifications
You must be signed in to change notification settings - Fork 0
/
19538.cpp
69 lines (57 loc) · 1.1 KB
/
19538.cpp
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
#include <iostream>
#include <queue>
#include <map>
using namespace std;
int nodeNum[200001];
int answer[200001];
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int N;
cin >> N;
map<int, vector<int>> m;
queue<int> q;
for (int i = 1; i <= N; ++i) {
int a;
while (true) {
cin >> a;
if (a == 0) {
break;
}
else {
m[i].push_back(a);
}
}
answer[i] = -1;
}
int M;
cin >> M;
for (int i = 0; i < M; ++i) {
int a;
cin >> a;
q.push(a);
answer[a] = 0;
}
for (int i = 1; i <= N; ++i) {
nodeNum[i] = m[i].size();
}
while (!q.empty()) {
int current = q.front(); // 1 // q 1 6
q.pop();
for (int i = 0; i < m[current].size(); ++i) {
int curValue = m[current][i]; // 2
int curValueNeiborSize = m[curValue].size();
nodeNum[curValue] -= 1;
int cnt = answer[current] + 1;
if (m[curValue].size() - nodeNum[curValue] >= (m[curValue].size() + 1) / 2 && answer[curValue] == -1) {
q.push(curValue);
answer[curValue] = cnt;
}
}
}
for (int i = 1; i <= N; ++i) {
cout << answer[i] << " ";
}
return 0;
}