#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
const int maxn = 2e5 + 5;
const int oo = 1e9 + 7;
const ll inf = 1e18;
int n, m;
int dis[maxn], ans, vs[maxn];
int vertex;
vector<int> adj[maxn];
void bfs(int u) {
for (int i = 1; i <= n; i++)
dis[i] = oo;
dis[u] = 0;
queue<int> q;
q.push(u);
while (!q.empty()) {
int v = q.front();
q.pop();
vs[v] = 1;
for (auto j : adj[v]) {
if (dis[j] > dis[v] + 1) {
dis[j] = dis[v] + 1;
ans = max(ans, dis[j]);
vertex = j;
q.push(j);
}
}
}
}
int solve(int u) {
ans = 0;
bfs(u);
bfs(vertex);
return ans + 1;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
int res = 0;
for (int i = 1; i <= n; i++) {
if (!vs[i])
res += solve(i);
}
cout << res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5968 KB |
Output is correct |
2 |
Correct |
10 ms |
6736 KB |
Output is correct |
3 |
Correct |
15 ms |
7788 KB |
Output is correct |
4 |
Correct |
26 ms |
8528 KB |
Output is correct |
5 |
Correct |
22 ms |
9440 KB |
Output is correct |
6 |
Correct |
26 ms |
10352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
74 ms |
10064 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
2 ms |
4964 KB |
Output is correct |
3 |
Correct |
2 ms |
4944 KB |
Output is correct |
4 |
Correct |
2 ms |
4944 KB |
Output is correct |
5 |
Incorrect |
237 ms |
5712 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5200 KB |
Output is correct |
2 |
Incorrect |
2 ms |
4944 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5968 KB |
Output is correct |
2 |
Correct |
10 ms |
6736 KB |
Output is correct |
3 |
Correct |
15 ms |
7788 KB |
Output is correct |
4 |
Correct |
26 ms |
8528 KB |
Output is correct |
5 |
Correct |
22 ms |
9440 KB |
Output is correct |
6 |
Correct |
26 ms |
10352 KB |
Output is correct |
7 |
Incorrect |
74 ms |
10064 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |