#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int n;
vector<bool> visited;
vector<vector<int>> adj;
vector<int> dist;
queue<int> q;
pair<int, int> diameter(int start) {
q.push(start);
vector<bool> v(n+1);
dist[start] = 0;
int a1 = 0, a2 = 0;
v[start] = true;
while (!q.empty()) {
int cur = q.front();
q.pop();
if (dist[cur] > a1) {
a1 = dist[cur];
a2 = cur;
}
visited[cur] = true;
for (auto e : adj[cur]) {
if (!v[e]) {
dist[e] = dist[cur]+1;
q.push(e);
v[e] = true;
}
}
}
return {a1, a2};
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
int m;
cin >> n >> m;
adj.resize(n+1);
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
visited.resize(n+1);
dist.resize(n+1, INF);
int ans = 0;
for (int i = 1; i <= n; i++) {
if (!visited[i]) {
int x = diameter(i).second;
ans += diameter(x).first;
ans++;
}
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1372 KB |
Output is correct |
2 |
Correct |
8 ms |
2396 KB |
Output is correct |
3 |
Correct |
12 ms |
3420 KB |
Output is correct |
4 |
Correct |
18 ms |
4432 KB |
Output is correct |
5 |
Correct |
19 ms |
5372 KB |
Output is correct |
6 |
Correct |
26 ms |
6428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
6232 KB |
Output is correct |
2 |
Correct |
4 ms |
1116 KB |
Output is correct |
3 |
Correct |
8 ms |
2140 KB |
Output is correct |
4 |
Correct |
12 ms |
2908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
1372 KB |
Output is correct |
6 |
Correct |
5 ms |
2648 KB |
Output is correct |
7 |
Correct |
9 ms |
3416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1372 KB |
Output is correct |
2 |
Correct |
8 ms |
2396 KB |
Output is correct |
3 |
Correct |
12 ms |
3420 KB |
Output is correct |
4 |
Correct |
18 ms |
4432 KB |
Output is correct |
5 |
Correct |
19 ms |
5372 KB |
Output is correct |
6 |
Correct |
26 ms |
6428 KB |
Output is correct |
7 |
Correct |
18 ms |
6232 KB |
Output is correct |
8 |
Correct |
4 ms |
1116 KB |
Output is correct |
9 |
Correct |
8 ms |
2140 KB |
Output is correct |
10 |
Correct |
12 ms |
2908 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
3 ms |
1372 KB |
Output is correct |
16 |
Correct |
5 ms |
2648 KB |
Output is correct |
17 |
Correct |
9 ms |
3416 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
29 ms |
6480 KB |
Output is correct |
30 |
Correct |
42 ms |
3932 KB |
Output is correct |
31 |
Correct |
42 ms |
7120 KB |
Output is correct |
32 |
Correct |
36 ms |
4956 KB |
Output is correct |
33 |
Correct |
25 ms |
7260 KB |
Output is correct |
34 |
Correct |
35 ms |
5976 KB |
Output is correct |
35 |
Correct |
28 ms |
7004 KB |
Output is correct |
36 |
Correct |
32 ms |
6236 KB |
Output is correct |
37 |
Correct |
24 ms |
7256 KB |
Output is correct |
38 |
Correct |
33 ms |
6736 KB |
Output is correct |
39 |
Correct |
26 ms |
7256 KB |
Output is correct |