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 kN = 1e5;
vector<int> g[1 + kN];
bitset<1 + kN> vis;
pair<int, int> dfs(int u, int par) {
vis[u] = true;
pair<int, int> best{0, u};
for (int v : g[u]) {
if (v != par) {
best = max(best, dfs(v, u));
}
}
best.first += 1;
return best;
}
void testCase() {
int n, m;
cin >> n >> m;
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
int ans = 0;
for (int i = 1; i <= n; ++i) {
if (!vis[i]) {
pair<int, int> root = dfs(i, 0);
root = dfs(root.second, 0);
ans += root.first;
}
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int tests = 1;
for (int tc = 0; tc < tests; ++tc) {
testCase();
}
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... |