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;
const int INF = 1e9;
int n;
vector<bool> visited;
vector<vector<int>> adj;
vector<int> dist;
queue<int> q;
pair<int, int> diameter(int start) {
q.push(start);
vector<bool> v(n+1);
dist[start] = 0;
int a1 = 0, a2 = 0;
while (!q.empty()) {
int cur = q.front();
q.pop();
if (dist[cur] > a1) {
a1 = dist[cur];
a2 = cur;
}
visited[cur] = true;
for (auto e : adj[cur]) {
if (!v[e]) {
dist[e] = dist[cur]+1;
q.push(e);
v[e] = true;
}
}
}
return {a1, a2};
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
int m;
cin >> n >> m;
adj.resize(n+1);
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
visited.resize(n+1);
dist.resize(n+1, INF);
int ans = 0;
for (int i = 1; i <= n; i++) {
if (!visited[i]) {
int x = diameter(i).second;
ans += diameter(x).first;
ans++;
}
}
cout << ans << '\n';
return 0;
}
# | 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... |