-
Notifications
You must be signed in to change notification settings - Fork 0
/
가장_먼_노드.cpp
52 lines (43 loc) · 922 Bytes
/
가장_먼_노드.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
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int dist[20001];
void Memset() {
for (int i = 0; i < 20001; i++) {
dist[i] = -1;
}
}
int solution(int n, vector<vector<int>> edge) {
int answer = 0;
int MAX = -1;
Memset();
vector<vector<int> > v(n + 1);
for (int i = 0; i < edge.size(); i++) {
v[edge[i][0]].push_back(edge[i][1]);
v[edge[i][1]].push_back(edge[i][0]);
}
queue<int> q;
dist[1] = 0;
for (int i = 0; i < v[1].size(); i++) {
dist[v[1][i]] = dist[1] + 1;
MAX = max(MAX, dist[v[1][i]]);
q.push(v[1][i]);
}
while (!q.empty()) {
int cur = q.front();
q.pop();
for (int i = 0; i < v[cur].size(); i++) {
if (dist[v[cur][i]] == -1) {
dist[v[cur][i]] = dist[cur] + 1;
MAX = max(MAX, dist[v[cur][i]]);
q.push(v[cur][i]);
}
}
}
for (int i = 1; i <= n; i++) {
if (dist[i] == MAX) answer++;
}
return answer;
}