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;
vector<int>adj[100001];
bool vis[100001]={false};
int again, maxdepth;
void dfs(int current, int past, int depth) {
vis[current]=true;
if(depth>maxdepth) {
maxdepth=depth;
again=current;
}
for(auto iter:adj[current]) {
if(iter != past) {
dfs(iter, current, depth+1);
}
}
}
int longest(int i) {
maxdepth=0;
dfs(i, -1, 1);
maxdepth=0;
dfs(again, -1, 1);
return maxdepth;
}
int main() {
int N,M,A,B;
cin >> N >> M;
for(int i=1; i<=M; i++) {
cin >> A >> B;
adj[A].push_back(B);
adj[B].push_back(A);
}
int sum=0;
for(int i=1; i<=N; i++) {
if(vis[i]==false) {
sum+=longest(i);
}
}
cout << sum << endl;
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... |