Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- vector<vector<int>> g;
- vector<bool> used;
- pair<int, int> dfs(int v) {
- used[v] = true;
- pair<int, int> best = {v, 0};
- for (const auto& u : g[v]) {
- if (!used[u]) {
- auto res = dfs(u);
- if (res.second > best.second) best = res;
- }
- }
- ++best.second;
- return best;
- }
- int main() {
- int n;
- cin >> n;
- g.assign(n, vector<int>());
- used.assign(n, false);
- for (int i = 0; i < n - 1; ++i) {
- int a, b;
- cin >> a >> b;
- --a, --b;
- g[a].push_back(b);
- g[b].push_back(a);
- }
- auto res1 = dfs(0);
- used.assign(n, false);
- auto res2 = dfs(res1.first);
- cout << res2.second - 1<< "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement