Lang:G++
Edit12345678910111213141516171819202122232425262728293031#include <bits/stdc++.h>#include <stdio.h>#include <stdlib.h>#include <math.h>#include <string.h>using std::vector;using std::max;vector<int> G[100001];int ans = 0;int dfs(int node, int depth) {int deepest = 0, second_deepest = 0;if (G[node].size() == 0) {return depth;}for (int i = 0, l = G[node].size(); i < l; ++i) {int child_depth = dfs(G[node][i], depth + 1);if (child_depth > deepest) {second_deepest = deepest;deepest = child_depth;} else if (child_depth > second_deepest) {second_deepest = child_depth;}}ans = max(ans, deepest + second_deepest - depth);return deepest;}