CSES Tree Distances I
// 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");
    }
    vector<int> ssp(int s) {
        vector<int> vis(n, 0), dis(n, INF);
        dis[s] = 0;
        function<void(int)> dfs = [&](int u) -> void {
            vis[u] = 1;
            for (auto v: e[u]) {
                if (vis[v]) continue;
                dis[v] = dis[u] + 1;
                dfs(v);
            }
        };
        dfs(s);
        return dis;
    }
    void solve() {
        auto [_, vs] = diameter();
        auto [s, t] = vs;
        auto diss = ssp(s);
        auto dist = ssp(t);
        rep(i, 0, n) cout << max(diss[i], dist[i]) << " \n"[i==n-1];
    }
};

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; --u, --v;
        g.add_edge(u, v);
    }
    g.build(0);
    g.solve();

    return 0;
}

Comments

  1. 10 months ago
    2025-12-06 2:23:41

    Gave taiwin99 a shot last night. Won a few bucks and had a laugh. The site’s a little basic, but who cares when you’re winning? I like it. taiwin99

  2. 9 months ago
    2025-12-19 13:38:34

    Yo, 188betthethao got my back when I need a quick bet. Straight and to the point, just how I like it. Check it out here: 188betthethao

  3. 9 months ago
    2025-12-29 6:06:48

    Okay, so I took a chance on vmaxx. The interface is clean and easy to use. Worth a try if you’re looking for something new.

  4. 8 months ago
    2026-1-27 6:30:07

    Interesting read! Understanding RTP & house edge is key to smart play. Platforms like gg panalo download prioritize transparency, which is great for informed decisions. Good analysis!

  5. 6 months ago
    2026-3-23 5:55:27

    That’s a great point about the evolution of gaming! It’s cool to see how AiScore builds on centuries of casino tradition with modern tech. For easy access, check out the aiscore download apk – convenient for any player! Really enjoying the platform’s diverse options.

  6. 6 months ago
    2026-3-24 1:05:10

    That’s a solid point about value betting – crucial for long-term success! Seeing platforms like 6win login cater to local payment options like GCash really improves accessibility for Filipino players, making it easier to get in the game. 👍

  7. 6 months ago
    2026-3-30 13:01:31

    Tree distance problems sharpen computational thinking skills that translate beautifully to real-world optimization challenges. Just as mastering DFS requires strategic depth, modern platforms like kkkk ph app demonstrate how algorithmic excellence creates seamless user experiences in gaming ecosystems.

  8. 4 months ago
    2026-5-20 19:06:53

    This color-coded navigation is brilliant for tracking odds visually! For a smoother entry into their ecosystem, try the 22bet download apk to bypass browser delays and dive straight into the action.

  9. 3 months ago
    2026-6-08 22:38:33

    This UI feels super intuitive! The blend of casino fun with betting depth is smart UX design. Check out sporting bet com for more structured action-it’s a great system build! ✨🎰

  10. 3 months ago
    2026-6-10 2:00:03

    The operational flow here is solid, especially the localized payment integration. For deeper dives, check out vc999 ডাউনলোড; it complements the platform’s robust structure well.

  11. 2 months ago
    2026-7-17 11:18:22

    Didn’t expect this level of detail! It’s more than just games; it feels structured. Good insights on discipline are crucial-always remember to check Classic Game Zone casino before diving in. Smart reading helps keep things safe.

  12. 2 months ago
    2026-7-24 23:00:57

    Yo, this breakdown is deep! 🤔 Thinking about the logic behind these games, it makes you look at casinoplus com ph app differently than just hype. Gotta check the stats on things like that! 📊

  13. 4 weeks ago
    2026-8-20 15:08:16

    This deep dive into tree distances highlights how fundamental graph theory principles govern optimization across all fields. Modeling shortest paths is key-from logistics networks to understanding the best value proposition, such as finding a reliable platform like boss77 vip. Truly universal concepts! boss77 vip

  14. 4 weeks ago
    2026-8-21 20:55:37

    Heard about 007winbet from a friend, gave it a shot and wasn’t disappointed. Good selection of slots and live games. Check it out yourself: 007winbet

  15. 4 weeks ago
    2026-8-21 20:55:55

    Heard about jilivsa from a friend and decided to check it out. Haven’t been disappointed! Pretty solid platform with a good range of games. Go on over to jilivsa and see for yourself.

  16. 4 weeks ago
    2026-8-21 20:56:12

    Just browsing around and found 88nnmedia. Looks like a media site. Not sure what to expect, but I’m always up for exploring something new. Anyone have any experience with them? Take a peek: 88nnmedia

  17. 4 weeks ago
    2026-8-22 23:46:36

    The payouts here are legit and the customer support is surprisingly helpful. I’ve tried a few others but bdcr777 feels more reliable for long term play.

  18. 3 weeks ago
    2026-8-24 20:31:23

    The slot mechanics feel solid, but the live dealer integration needs more depth to challenge veterans. Still, checking out bigbuny download apk shows some interesting bonus structures. Keep refining the core gameplay loop!

  19. 2 weeks ago
    2026-9-02 20:07:42

    Best betting options for local players. The odds are competitive and the app is very user friendly. Big thumbs up for ph1xbetph.

Send Comment Edit Comment


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