We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 17b9d16 commit 924a581Copy full SHA for 924a581
khj20006/202511/19 PGM LV3 가장 먼 노드.md
@@ -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