Skip to content

Instantly share code, notes, and snippets.

@LucaDantas
Created October 5, 2021 14:44
Show Gist options
  • Save LucaDantas/98870e259c13806ff900015396cc94b9 to your computer and use it in GitHub Desktop.
Save LucaDantas/98870e259c13806ff900015396cc94b9 to your computer and use it in GitHub Desktop.
#include <cstdio>
#include <vector>
constexpr int maxn = 2e5+10;
std::vector<int> g[maxn];
int ans;
int max(int a, int b) { return a > b ? a : b; }
int dfs(int u, int p) {
int ff = 0, ss = 0;
for(int v : g[u]) if(v != p) {
int val = dfs(v, u);
if(val > ff) ss = ff, ff = val;
else if(val > ss) ss = val;
}
ans = max(ans, ff + ss);
return ff+1;
}
int main() {
int n; scanf("%d", &n);
for(int i = 1, a, b; i < n; i++)
scanf("%d %d", &a, &b), g[a].push_back(b), g[b].push_back(a);
dfs(1, 0);
printf("%d\n", ans);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment