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 MAXN = 1e5+1;
vector<int>adj[MAXN];
bool vis[MAXN];
int d = 0;
int dfs(int v,int u){
vis[v] = 1;
int d1 = 0,d2 = 0;
for(int x:adj[v]){
if(x==u)continue;
int res = dfs(x,v)+1;
if(res >= d1){
d2 = d1;
d1 = res;
}else if(res > d2)d2 = res;
}
d = max(d,d1+d2);
return d1;
}
int main()
{
int n,m;
cin>>n>>m;
for(int i=0;i<m;i++){
int a,b;
cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
int ans = 0;
for(int i=1;i<=n;i++){
if(!vis[i]){
d = 0;
dfs(i,0);
ans+=d+1;
//cout<<i<<" "<<d<<'\n';
}
}
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... |