Created
August 13, 2019 13:12
-
-
Save lychees/80be2b033f38e8eb28a6fc43059297f8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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