#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int n;
vector<bool> visited;
vector<vector<int>> adj;
pair<int, int> diameter(int start) {
vector<int> dist(n+1, INF);
queue<int> q;
q.push(start);
vector<bool> v(n+1);
dist[start] = 0;
int a1 = 0, a2 = 0;
while (!q.empty()) {
int cur = q.front();
q.pop();
if (dist[cur] > a1) {
a1 = dist[cur];
a2 = cur;
}
if (v[cur]) continue;
visited[cur] = true;
v[cur] = true;
for (auto e : adj[cur]) {
if (dist[cur]+1 < dist[e]) {
dist[e] = dist[cur]+1;
q.push(e);
}
}
}
return {a1, a2};
}
int main() {
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);
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 |
8 ms |
1624 KB |
Output is correct |
2 |
Correct |
15 ms |
2732 KB |
Output is correct |
3 |
Correct |
22 ms |
3984 KB |
Output is correct |
4 |
Correct |
31 ms |
5204 KB |
Output is correct |
5 |
Correct |
55 ms |
6412 KB |
Output is correct |
6 |
Correct |
46 ms |
7624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
7372 KB |
Output is correct |
2 |
Correct |
96 ms |
1368 KB |
Output is correct |
3 |
Correct |
402 ms |
2264 KB |
Output is correct |
4 |
Correct |
858 ms |
3220 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 |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
32 ms |
1552 KB |
Output is correct |
6 |
Correct |
118 ms |
2652 KB |
Output is correct |
7 |
Correct |
261 ms |
3700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
1 ms |
484 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
1624 KB |
Output is correct |
2 |
Correct |
15 ms |
2732 KB |
Output is correct |
3 |
Correct |
22 ms |
3984 KB |
Output is correct |
4 |
Correct |
31 ms |
5204 KB |
Output is correct |
5 |
Correct |
55 ms |
6412 KB |
Output is correct |
6 |
Correct |
46 ms |
7624 KB |
Output is correct |
7 |
Correct |
46 ms |
7372 KB |
Output is correct |
8 |
Correct |
96 ms |
1368 KB |
Output is correct |
9 |
Correct |
402 ms |
2264 KB |
Output is correct |
10 |
Correct |
858 ms |
3220 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 |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
32 ms |
1552 KB |
Output is correct |
16 |
Correct |
118 ms |
2652 KB |
Output is correct |
17 |
Correct |
261 ms |
3700 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
1 ms |
484 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
55 ms |
7620 KB |
Output is correct |
30 |
Execution timed out |
1047 ms |
4184 KB |
Time limit exceeded |
31 |
Halted |
0 ms |
0 KB |
- |