#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;
vector<int> g[100100];
bool used[100100];
void dfs(int v, int p, int de)
{
used[v] = 1;
if (de > mx) { mx = de; nm = v; }
for (auto u : g[v])
if (u != p) dfs(u, v, de + 1);
}
int diam(int v)
{
mx = 0;
dfs(v, -1, 0);
mx = 0;
dfs(nm, -1, 0);
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 |
9 ms |
3148 KB |
Output is correct |
2 |
Correct |
15 ms |
3696 KB |
Output is correct |
3 |
Correct |
23 ms |
4268 KB |
Output is correct |
4 |
Correct |
27 ms |
4880 KB |
Output is correct |
5 |
Correct |
32 ms |
5440 KB |
Output is correct |
6 |
Correct |
40 ms |
5896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
124 ms |
10564 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
3 ms |
2636 KB |
Output is correct |
5 |
Incorrect |
214 ms |
3184 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
3148 KB |
Output is correct |
2 |
Correct |
15 ms |
3696 KB |
Output is correct |
3 |
Correct |
23 ms |
4268 KB |
Output is correct |
4 |
Correct |
27 ms |
4880 KB |
Output is correct |
5 |
Correct |
32 ms |
5440 KB |
Output is correct |
6 |
Correct |
40 ms |
5896 KB |
Output is correct |
7 |
Incorrect |
124 ms |
10564 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |