Skip to content

Commit 61d0aee

Browse files
authored
Merge pull request #1450 from AlgorithmWithGod/khj20006
[20251119] PGM / LV3 / 가장 먼 노드 / 권혁준
2 parents 968ee4e + 924a581 commit 61d0aee

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
```cpp
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
int solution(int n, vector<vector<int>> edge) {
6+
const int INF = 123456;
7+
8+
vector<vector<int>> graph(n+1);
9+
for(auto e : edge) {
10+
int a = e[0], b = e[1];
11+
graph[a].push_back(b);
12+
graph[b].push_back(a);
13+
}
14+
15+
queue<pair<int, int>> q;
16+
vector<int> dist(n+1, INF);
17+
q.emplace(1,0);
18+
dist[1] = 0;
19+
int mx = 0;
20+
while(!q.empty()) {
21+
auto [n,d] = q.front(); q.pop();
22+
mx = max(mx, d);
23+
for(int i:graph[n]) if(dist[i] == INF) {
24+
dist[i] = d+1;
25+
q.emplace(i, dist[i]);
26+
}
27+
}
28+
29+
int ans = 0;
30+
for(int i=1;i<=n;i++) if(dist[i] == mx) ans++;
31+
32+
return ans;
33+
}
34+
```

0 commit comments

Comments
 (0)