Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement