-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy path115A. Party.cpp
58 lines (45 loc) · 1.04 KB
/
115A. Party.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
#include <iostream>
#include <queue>
#include <vector>
#include <memory.h>
using namespace std;
int const N = 2001;
vector<vector<int> > g;
queue<int> q;
bool vis[N];
int BFS() {
int cost = 0, size;
while(!q.empty()) {
size = q.size();
while(size--) {
int fr = q.front();
q.pop();
for(int i = 0; i < g[fr].size(); i++) {
if(vis[g[fr][i]]) continue;
vis[g[fr][i]] = true;
q.push(g[fr][i]);
}
}
cost++;
}
return cost;
}
int main() {
int n, tmp;
cin >> n;
g.resize(n);
memset(vis, false, sizeof vis);
for(int i = 0; i < n; i++) {
cin >> tmp;
if(tmp == -1) {
q.push(i);
vis[i] = true;
} else {
tmp--;
g[i].push_back(tmp);
g[tmp].push_back(i);
}
}
cout << BFS() << endl;
return 0;
}