From cdc9464526e968ef102c1a4fb8d2f8bcd7a2b2df Mon Sep 17 00:00:00 2001 From: Zengtudor Date: Wed, 13 Nov 2024 23:32:56 +0800 Subject: [PATCH] update --- src/B4016/B4016.cpp | 63 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/B4016/B4016.cpp diff --git a/src/B4016/B4016.cpp b/src/B4016/B4016.cpp new file mode 100644 index 0000000..84a99f8 --- /dev/null +++ b/src/B4016/B4016.cpp @@ -0,0 +1,63 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +using ll = int64_t; + +int main(){ + std::iostream::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr); + + const ll max_n = 1e5+5; + ll n; + std::cin>>n; + auto *edge = new std::vector[max_n]; + ll a, b; + for(ll i{1}; i>a>>b; + edge[a].push_back(b); + edge[b].push_back(a); + } + std::queue> q; + q.emplace(0, a); + std::bitset vis; + vis[a]=true; + ll max_a{}, max{}; + while(!q.empty()){ + auto top = q.front(); + q.pop(); + if(top.first > max){ + max = top.first; + max_a = top.second; + } + for(auto &i:edge[top.second]){ + if(vis[i]){ + continue; + } + q.push({top.first + 1, i}); + vis[i] = true; + } + } + q = std::queue>(); + q.emplace(0, max_a); + vis.reset(); + vis[max_a]=true; + ll ans{}; + while(!q.empty()){ + auto top = q.front(); + q.pop(); + ans = std::max(ans, top.first); + for(auto i: edge[top.second]){ + if(vis[i]){ + continue; + } + q.push({top.first+1, i}); + vis[i] = true; + } + } + std::cout<