Skip to content

Instantly share code, notes, and snippets.

@lychees
Created August 13, 2019 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lychees/80be2b033f38e8eb28a6fc43059297f8 to your computer and use it in GitHub Desktop.
Save lychees/80be2b033f38e8eb28a6fc43059297f8 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
using namespace std;
const int N = int(1e4) + 9;
vector<int> adj[N];
int n, d;
int dfs(int u = 1, int p = -1) {
int d1 = 0, d2 = 0;
for (auto v: adj[u]) {
if (v == p) continue;
int d = dfs(v, u) + 1;
if (d > d1) d2 = d1, d1 = d;
else if (d > d2) d2 = d;
}
d = max(d, d1 + d2);
return d1;
}
int main() {
cin >> n;
for (int i=0;i<n-1;++i) {
int a, b; cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs();
cout << d << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment