#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 + 1;
}
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;
for (int i = 1; i <= n; i++)
if (!used[i]) ans += diam(i);
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
3432 KB |
Output is correct |
2 |
Correct |
12 ms |
4296 KB |
Output is correct |
3 |
Correct |
17 ms |
4992 KB |
Output is correct |
4 |
Correct |
26 ms |
5824 KB |
Output is correct |
5 |
Correct |
31 ms |
6604 KB |
Output is correct |
6 |
Correct |
39 ms |
7448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
108 ms |
12100 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2680 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
1 ms |
2636 KB |
Output is correct |
5 |
Incorrect |
194 ms |
3276 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2636 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
3432 KB |
Output is correct |
2 |
Correct |
12 ms |
4296 KB |
Output is correct |
3 |
Correct |
17 ms |
4992 KB |
Output is correct |
4 |
Correct |
26 ms |
5824 KB |
Output is correct |
5 |
Correct |
31 ms |
6604 KB |
Output is correct |
6 |
Correct |
39 ms |
7448 KB |
Output is correct |
7 |
Incorrect |
108 ms |
12100 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |