CSES Tree Diameter
// compile: make data
// run: ./data < data.in
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#ifdef LOCAL
#include <debug/codeforces.h>
#define debug(x...) _debug_print(#x, x);
#define Debug(x...) _debug_print_format(#x, x);
#else
#define debug(x...)
#define Debug(x...)
#endif
template<typename...Args> void print_(Args...args){((cout<<args<<" "),...)<<endl;}
#define VI vector<int>
#define VII vector<vector<int>>
#define VIII vector<vector<vector<int>>>
#define rep(i,a,b) for(int i=(a);i<(int)(b);++i)
#define sz(v) ((int)(v).size())
#define print(...) print_(__VA_ARGS__);
#define FIND(a, x) ((find(a.begin(),a.end(),(x))!=a.end())?1:0)
#define cmin(x,...) x=min({(x), __VA_ARGS__})
#define cmax(x,...) x=max({(x), __VA_ARGS__})
#define INTMAX (int)(9223372036854775807)
#define INF (int)(1152921504606846976)
#define NaN (int)(0x8b88e1d0595d51d1)
#define double long double
#define int long long
#define MAXN 200010

struct Tree {
    int n;
    vector<vector<int>> e;
    struct node {
        vector<node*> ch;
        node* parent = nullptr;
        int size;
    };
    vector<node> vertex;
    node *root;
    Tree(int V) {
        n = V;
        e.resize(n);
        vertex.resize(n);
    }
    void add_edge(int u, int v) {
        e[u].push_back(v), e[v].push_back(u);
    }
    int ind(node *x) {return x - &vertex[0]; }
    void build(int rt = 0) {
        function<void(int, int)> dfs = [&](int u, int pre) {
            vertex[u].size = 1;
            for (int v: e[u]) {
                if (v == pre) continue;
                vertex[u].ch.push_back(&vertex[v]);
                vertex[v].parent = &vertex[u];
                dfs(v, u);
                vertex[u].size += vertex[v].size;
            }
        };
        root = &vertex[rt];
        dfs(rt, -1);
    }
    pair<int, pair<int, int>> diameter() {
        int maxd = -INF, s = -1, t = -1;
        vector<int> vis(n, 0);
        function<void(int, int, int&)> dfs = [&](int u, int dis, int &st) -> void {
            vis[u] = 1;
            if (dis > maxd) maxd = dis, st = u;
            for (auto v: e[u]) if (!vis[v]) dfs(v, dis+1, st);
        };
        dfs(ind(root), 0, s);
        maxd = -INF, fill(vis.begin(), vis.end(), 0);
        dfs(s, 0, t);
        return {maxd, {s, t}};
    }
    void graphviz_dump(int plus = 0, string filename = "graph.dot") {
        FILE *f = fopen(filename.c_str(), "w");
        fprintf(f, "digraph Tree {\n");
        rep(i, 0, n) {
            node *x = &vertex[i];
            fprintf(f, "    %lld;\n");
        }
        rep(i, 0, n) {
            node *x = &vertex[i];
            for (auto y: x->ch) {
                fprintf(f, "    %ld -> %ld;\n", x - &vertex[0], y - &vertex[0]);
            }
        }
        fprintf(f, "}\n");
    }
};

int32_t main() {
    ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

    int n; cin >> n;
    Tree g(n);
    rep(i, 0, n-1) {
        int u, v; cin >> u >> v;
        g.add_edge(u-1, v-1);
    }
    g.build(0);
    auto res = g.diameter();
    cout << res.first << endl;

    return 0;
}

Comments

  1. 1 month ago
    2026-8-14 2:25:59

    The daily login rewards really make a difference over time. I appreciate that they have fair RNG certifications and regular third party audits. Playing here feels secure and genuinely entertaining. mahjong288login

  2. 1 month ago
    2026-8-14 2:26:15

    Bringing the boardwalk energy straight to my screen. I appreciate the clean layout and how straightforward the bonus rules are written. No hidden traps or confusing jargon just pure entertainment. Perfect spot to unwind after a long week at work casinoatlanticcityonline

  3. 1 month ago
    2026-8-14 2:26:34

    I have tried so many betting platforms over the years and this one finally made me stick around. The odds are competitive, live matches stream without lag and cash out features work perfectly. Definitely my new favorite for game night. tr1xbetlogin

Send Comment Edit Comment


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
Previous
Next