#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, m, a, b, dist[N], ans;
bool vis[N];
vector<int> tree[N];
int dfs(int v, int p){
vis[v] = true;
dist[v] = dist[p] + 1;
int mx = v, cur;
for(auto e : tree[v]){
if(e != p){
cur = dfs(e, v);
if(dist[cur] > dist[mx]) mx = cur;
}
}
return mx;
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
cin >> n >> m;
for(int i = 0; i < m; i++){
cin >> a >> b;
tree[a].push_back(b);
tree[b].push_back(a);
}
for(int i = 1; i <= n; i++){
if(!vis[i]){
a = dfs(i, 0);
b = dfs(a, 0);
ans += dist[b];
}
}
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
3548 KB |
Output is correct |
2 |
Correct |
14 ms |
4188 KB |
Output is correct |
3 |
Correct |
22 ms |
4996 KB |
Output is correct |
4 |
Correct |
26 ms |
5900 KB |
Output is correct |
5 |
Correct |
33 ms |
6732 KB |
Output is correct |
6 |
Correct |
41 ms |
7492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
13780 KB |
Output is correct |
2 |
Correct |
5 ms |
3200 KB |
Output is correct |
3 |
Correct |
7 ms |
3796 KB |
Output is correct |
4 |
Correct |
8 ms |
4372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
5 ms |
3284 KB |
Output is correct |
6 |
Correct |
9 ms |
4000 KB |
Output is correct |
7 |
Correct |
10 ms |
4692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2776 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2772 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
3548 KB |
Output is correct |
2 |
Correct |
14 ms |
4188 KB |
Output is correct |
3 |
Correct |
22 ms |
4996 KB |
Output is correct |
4 |
Correct |
26 ms |
5900 KB |
Output is correct |
5 |
Correct |
33 ms |
6732 KB |
Output is correct |
6 |
Correct |
41 ms |
7492 KB |
Output is correct |
7 |
Correct |
32 ms |
13780 KB |
Output is correct |
8 |
Correct |
5 ms |
3200 KB |
Output is correct |
9 |
Correct |
7 ms |
3796 KB |
Output is correct |
10 |
Correct |
8 ms |
4372 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
5 ms |
3284 KB |
Output is correct |
16 |
Correct |
9 ms |
4000 KB |
Output is correct |
17 |
Correct |
10 ms |
4692 KB |
Output is correct |
18 |
Correct |
2 ms |
2776 KB |
Output is correct |
19 |
Correct |
2 ms |
2644 KB |
Output is correct |
20 |
Correct |
2 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2644 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
2 ms |
2644 KB |
Output is correct |
24 |
Correct |
2 ms |
2644 KB |
Output is correct |
25 |
Correct |
2 ms |
2644 KB |
Output is correct |
26 |
Correct |
2 ms |
2772 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
2 ms |
2644 KB |
Output is correct |
29 |
Correct |
50 ms |
7468 KB |
Output is correct |
30 |
Correct |
11 ms |
4240 KB |
Output is correct |
31 |
Correct |
25 ms |
7012 KB |
Output is correct |
32 |
Correct |
18 ms |
4972 KB |
Output is correct |
33 |
Correct |
33 ms |
7256 KB |
Output is correct |
34 |
Correct |
25 ms |
5708 KB |
Output is correct |
35 |
Correct |
37 ms |
7080 KB |
Output is correct |
36 |
Correct |
34 ms |
6272 KB |
Output is correct |
37 |
Correct |
35 ms |
7264 KB |
Output is correct |
38 |
Correct |
43 ms |
6748 KB |
Output is correct |
39 |
Correct |
35 ms |
7116 KB |
Output is correct |