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>
#define REP(i,a,b) for (auto i = (a); i <= (b); ++i)
#define SIZE(x) int(x.size())
#define debug(x) cerr << #x << " is " << x << endl
#define el '\n'
using namespace std; using ll = long long;
const int N = 1e5+3;
int n, m;
vector<int> adj[N];
vector<int> components;
bool visited[N];
void dfs(int s) {
if (visited[s]) return;
visited[s] = true;
for (auto u : adj[s]) {
dfs(u);
}
}
void dfs1(int &x, int &l, int s = 1, int e = 0, int d = 0) {
if (l < d) {
x = s;
l = d;
}
for (auto u : adj[s]) if (u != e) {
dfs1(x,l,u,s,d+1);
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
REP(i,1,m) {
int u, v; cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
REP(i,1,n) {
if (not visited[i]) {
components.push_back(i);
dfs(i);
}
}
int answer = 0;
for (auto s : components) {
int x = s, l = 0;
dfs1(x,l,s);
dfs1(x,l = 0,x);
answer += l+1;
}
cout << answer;
}
# | 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... |