#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 1;
vector <int> adj[maxn];
bool vis[maxn];
int node = 0, max_dis = -1;
void dfs(int s, int p, int d) {
if (d > max_dis) {
node = s;
max_dis = d;
}
vis[s] = true;
for (auto i : adj[s]) {
if (i == p)
continue;
dfs(i, s, d+1);
}
}
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
vector <pair <int, int>> endpoints;
for (int i = 1; i <= n; i++) {
if (vis[i])
continue;
max_dis = -1;
endpoints.emplace_back();
dfs(i, i, 0);
endpoints.back().first = node;
max_dis = -1;
dfs(node, node, 0);
endpoints.back().second = node;
}
for (int i = 0; i + 1 < (int)endpoints.size(); i++) {
int a = endpoints[i].second, b = endpoints[i+1].first;
adj[a].push_back(b);
adj[b].push_back(a);
}
max_dis = -1;
dfs(endpoints.back().second, 0, 0);
cout << max_dis+1 << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
3408 KB |
Output is correct |
2 |
Correct |
18 ms |
4176 KB |
Output is correct |
3 |
Correct |
43 ms |
4960 KB |
Output is correct |
4 |
Correct |
36 ms |
5712 KB |
Output is correct |
5 |
Correct |
42 ms |
6472 KB |
Output is correct |
6 |
Correct |
49 ms |
7240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
11852 KB |
Output is correct |
2 |
Correct |
6 ms |
4176 KB |
Output is correct |
3 |
Correct |
10 ms |
5588 KB |
Output is correct |
4 |
Correct |
15 ms |
7124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2640 KB |
Output is correct |
2 |
Correct |
1 ms |
2640 KB |
Output is correct |
3 |
Correct |
1 ms |
2812 KB |
Output is correct |
4 |
Correct |
1 ms |
2640 KB |
Output is correct |
5 |
Correct |
6 ms |
3408 KB |
Output is correct |
6 |
Correct |
10 ms |
4464 KB |
Output is correct |
7 |
Correct |
14 ms |
5200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2640 KB |
Output is correct |
2 |
Correct |
1 ms |
2796 KB |
Output is correct |
3 |
Correct |
1 ms |
2640 KB |
Output is correct |
4 |
Correct |
1 ms |
2640 KB |
Output is correct |
5 |
Correct |
1 ms |
2640 KB |
Output is correct |
6 |
Correct |
1 ms |
2640 KB |
Output is correct |
7 |
Correct |
1 ms |
2800 KB |
Output is correct |
8 |
Correct |
1 ms |
2808 KB |
Output is correct |
9 |
Correct |
2 ms |
2640 KB |
Output is correct |
10 |
Correct |
2 ms |
2640 KB |
Output is correct |
11 |
Correct |
2 ms |
2640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
3408 KB |
Output is correct |
2 |
Correct |
18 ms |
4176 KB |
Output is correct |
3 |
Correct |
43 ms |
4960 KB |
Output is correct |
4 |
Correct |
36 ms |
5712 KB |
Output is correct |
5 |
Correct |
42 ms |
6472 KB |
Output is correct |
6 |
Correct |
49 ms |
7240 KB |
Output is correct |
7 |
Correct |
52 ms |
11852 KB |
Output is correct |
8 |
Correct |
6 ms |
4176 KB |
Output is correct |
9 |
Correct |
10 ms |
5588 KB |
Output is correct |
10 |
Correct |
15 ms |
7124 KB |
Output is correct |
11 |
Correct |
2 ms |
2640 KB |
Output is correct |
12 |
Correct |
1 ms |
2640 KB |
Output is correct |
13 |
Correct |
1 ms |
2812 KB |
Output is correct |
14 |
Correct |
1 ms |
2640 KB |
Output is correct |
15 |
Correct |
6 ms |
3408 KB |
Output is correct |
16 |
Correct |
10 ms |
4464 KB |
Output is correct |
17 |
Correct |
14 ms |
5200 KB |
Output is correct |
18 |
Correct |
2 ms |
2640 KB |
Output is correct |
19 |
Correct |
1 ms |
2796 KB |
Output is correct |
20 |
Correct |
1 ms |
2640 KB |
Output is correct |
21 |
Correct |
1 ms |
2640 KB |
Output is correct |
22 |
Correct |
1 ms |
2640 KB |
Output is correct |
23 |
Correct |
1 ms |
2640 KB |
Output is correct |
24 |
Correct |
1 ms |
2800 KB |
Output is correct |
25 |
Correct |
1 ms |
2808 KB |
Output is correct |
26 |
Correct |
2 ms |
2640 KB |
Output is correct |
27 |
Correct |
2 ms |
2640 KB |
Output is correct |
28 |
Correct |
2 ms |
2640 KB |
Output is correct |
29 |
Correct |
46 ms |
7256 KB |
Output is correct |
30 |
Correct |
32 ms |
11464 KB |
Output is correct |
31 |
Correct |
45 ms |
11624 KB |
Output is correct |
32 |
Correct |
28 ms |
11292 KB |
Output is correct |
33 |
Correct |
45 ms |
10240 KB |
Output is correct |
34 |
Correct |
33 ms |
10752 KB |
Output is correct |
35 |
Correct |
43 ms |
11268 KB |
Output is correct |
36 |
Correct |
42 ms |
9212 KB |
Output is correct |
37 |
Correct |
43 ms |
10184 KB |
Output is correct |
38 |
Correct |
47 ms |
7884 KB |
Output is correct |
39 |
Correct |
45 ms |
10996 KB |
Output is correct |