0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-08 13:58:48 +00:00

B4016 树的直径

https://www.luogu.com.cn/record/169658881
This commit is contained in:
Baoshuo Ren 2024-07-31 12:56:10 +08:00
parent 96f8d117e2
commit 059a68435b
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

49
Luogu/B4016/B4016.cpp Normal file
View File

@ -0,0 +1,49 @@
#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 1e5 + 5;
int n, dep[N];
std::vector<int> g[N];
void dfs(int u, int fa) {
dep[u] = dep[fa] + 1;
for (int v : g[u]) {
if (v == fa) continue;
dfs(v, u);
}
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 1, u, v; i < n; i++) {
cin >> u >> v;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
dfs(1, 0);
int u = std::max_element(dep + 1, dep + n + 1) - dep;
std::fill(std::begin(dep), std::end(dep), 0);
dfs(u, 0);
cout << *std::max_element(dep + 1, dep + n + 1) - 1 << endl;
return 0;
}