#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
using ll = long long;
const int inf = 2e9;
int n, m, mx, nm, dst[100100];
vector<int> g[100100];
bool used[100100];
void dfs(int v, int p = -1)
{
used[v] = 1;
dst[v] = (p == -1 ? 0 : dst[p] + 1);
if (dst[v] > mx) { mx = dst[v]; nm = v; }
for (auto u : g[v])
if (u != p) dfs(u, v);
}
int diam(int v)
{
mx = 0;
dfs(v);
mx = 0;
dfs(nm);
return mx;
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
int ans = 0, cnt = 0;
for (int i = 1; i <= n; i++)
if (!used[i])
{
cnt++;
ans += diam(i);
}
cout << ans + cnt << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
3276 KB |
Output is correct |
2 |
Correct |
11 ms |
3916 KB |
Output is correct |
3 |
Correct |
17 ms |
4512 KB |
Output is correct |
4 |
Correct |
23 ms |
5044 KB |
Output is correct |
5 |
Correct |
30 ms |
5764 KB |
Output is correct |
6 |
Correct |
36 ms |
6340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
112 ms |
10876 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2636 KB |
Output is correct |
2 |
Correct |
1 ms |
2636 KB |
Output is correct |
3 |
Correct |
1 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Incorrect |
187 ms |
3216 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
3276 KB |
Output is correct |
2 |
Correct |
11 ms |
3916 KB |
Output is correct |
3 |
Correct |
17 ms |
4512 KB |
Output is correct |
4 |
Correct |
23 ms |
5044 KB |
Output is correct |
5 |
Correct |
30 ms |
5764 KB |
Output is correct |
6 |
Correct |
36 ms |
6340 KB |
Output is correct |
7 |
Incorrect |
112 ms |
10876 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |