This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |