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 N = 1e5 + 5;
int n, m, a, b, dist[N], ans;
bool vis[N];
vector<int> tree[N];
int dfs(int v, int p){
vis[v] = true;
dist[v] = dist[p] + 1;
int mx = v, cur;
for(auto e : tree[v]){
if(e != p){
cur = dfs(e, v);
if(dist[cur] > dist[mx]) mx = cur;
}
}
return mx;
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
cin >> n >> m;
for(int i = 0; i < m; i++){
cin >> a >> b;
tree[a].push_back(b);
tree[b].push_back(a);
}
for(int i = 1; i <= n; i++){
if(!vis[i]){
a = dfs(i, 0);
b = dfs(a, 0);
ans += dist[b];
}
}
cout << ans;
}
# | 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... |