#include <bits/stdc++.h>
using std::vector;
using std::array;
using std::pair;
using std::tuple;
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int N, M;
std::cin >> N >> M;
vector<vector<int>> graph(N);
while (M--) {
int a, b;
std::cin >> a >> b;
a -= 1, b -= 1;
graph[a].push_back(b);
graph[b].push_back(a);
}
vector<int> dist(N, -1);
int ans = 0;
for (int src = 0; src < N; ++src) {
if (dist[src] >= 0) continue;
vector<int> list;
{
std::queue<int> que;
const auto push = [&](const int u, const int d) {
if (dist[u] == -1) {
dist[u] = d;
list.push_back(u);
que.push(u);
}
};
push(src, 0);
while (!que.empty()) {
const int u = que.front();
que.pop();
for (const int v : graph[u]) {
push(v, dist[u] + 1);
}
}
}
int s = -1;
for (const int u : list) {
if (s == -1 or dist[s] < dist[u]) {
s = u;
}
}
for (const int u : list) {
dist[u] = -1;
}
{
std::queue<int> que;
const auto push = [&](const int u, const int d) {
if (dist[u] == -1) {
dist[u] = d;
que.push(u);
}
};
push(s, 0);
while (!que.empty()) {
const int u = que.front();
que.pop();
for (const int v : graph[u]) {
push(v, dist[u] + 1);
}
}
}
int max = -1;
for (const int u : list) {
max = std::max(max, dist[u]);
}
ans += max + 1;
}
std::cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
1444 KB |
Output is correct |
2 |
Correct |
18 ms |
2652 KB |
Output is correct |
3 |
Correct |
25 ms |
3600 KB |
Output is correct |
4 |
Correct |
50 ms |
4944 KB |
Output is correct |
5 |
Correct |
30 ms |
5912 KB |
Output is correct |
6 |
Correct |
44 ms |
6908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
6792 KB |
Output is correct |
2 |
Correct |
6 ms |
1100 KB |
Output is correct |
3 |
Correct |
16 ms |
2056 KB |
Output is correct |
4 |
Correct |
19 ms |
2764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
312 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
7 ms |
1356 KB |
Output is correct |
6 |
Correct |
10 ms |
2488 KB |
Output is correct |
7 |
Correct |
10 ms |
3508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
312 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
300 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
352 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
1444 KB |
Output is correct |
2 |
Correct |
18 ms |
2652 KB |
Output is correct |
3 |
Correct |
25 ms |
3600 KB |
Output is correct |
4 |
Correct |
50 ms |
4944 KB |
Output is correct |
5 |
Correct |
30 ms |
5912 KB |
Output is correct |
6 |
Correct |
44 ms |
6908 KB |
Output is correct |
7 |
Correct |
41 ms |
6792 KB |
Output is correct |
8 |
Correct |
6 ms |
1100 KB |
Output is correct |
9 |
Correct |
16 ms |
2056 KB |
Output is correct |
10 |
Correct |
19 ms |
2764 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
312 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
7 ms |
1356 KB |
Output is correct |
16 |
Correct |
10 ms |
2488 KB |
Output is correct |
17 |
Correct |
10 ms |
3508 KB |
Output is correct |
18 |
Correct |
1 ms |
296 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
312 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
300 KB |
Output is correct |
26 |
Correct |
0 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
352 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
66 ms |
6900 KB |
Output is correct |
30 |
Correct |
21 ms |
3896 KB |
Output is correct |
31 |
Correct |
56 ms |
6088 KB |
Output is correct |
32 |
Correct |
50 ms |
4572 KB |
Output is correct |
33 |
Correct |
65 ms |
6124 KB |
Output is correct |
34 |
Correct |
47 ms |
5052 KB |
Output is correct |
35 |
Correct |
46 ms |
6096 KB |
Output is correct |
36 |
Correct |
58 ms |
5760 KB |
Output is correct |
37 |
Correct |
43 ms |
6104 KB |
Output is correct |
38 |
Correct |
67 ms |
6352 KB |
Output is correct |
39 |
Correct |
34 ms |
6068 KB |
Output is correct |