CSES Findinga Centroid
// compile: make data
// run: ./data < big.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, depth, val, dfn;
        node *heavy = nullptr, *top = nullptr;
    };
    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) {
            node *x = &vertex[u];
            x->size = 1;
            x->depth = x==root ? 0 : x->parent->depth + 1;
            for (int v: e[u]) {
                if (v == pre) continue;
                node *y = &vertex[v];
                x->ch.push_back(y);
                y->parent = x;
                dfs(v, u);
                x->size += y->size;
                if (!x->heavy || y->size > x->heavy->size) x->heavy = y;
            }
        };
        root = &vertex[rt];
        dfs(rt, -1);
    }
    int centroid() {
        node *x = root;
        while (x) {
            int maxsz = -INF;
            node *maxnode = nullptr;
            for (auto y: x->ch) if (y->size > maxsz) maxsz = y->size, maxnode = y;
            if (maxsz <= n / 2) return ind(x);
            x = maxnode;
        }
        return -1;
    }

    void graphviz_dump(int plus = 0, string filename = "graph.dot") {
        string vlabel = "", elabel = "";
        // vlabel = " color=\"red\"";
        elabel = " color=\"red\"";
        set<int> vset; set<pair<int, int>> eset;
        rep(i, 0, n) if (vertex[i].heavy) vset.insert(ind(vertex[i].heavy)), eset.insert({i, ind(vertex[i].heavy)});
        FILE *f = fopen(filename.c_str(), "w");
        fprintf(f, "digraph Tree {\n");
        rep(i, 0, n) {
            node *x = &vertex[i];
            fprintf(f, "    %lld;\n", vset.count(i) ? vlabel.c_str() : "");
        }
        rep(i, 0, n) {
            node *x = &vertex[i];
            for (auto y: x->ch) {
                fprintf(f, "    %ld -> %ld[arrowhead=\"none\"%s];\n", x - &vertex[0], y - &vertex[0], eset.count({i, ind(y)}) ? elabel.c_str() : "");
            }
        }
        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);
    cout << g.centroid() + 1 << endl;

    return 0;
}

Comments

  1. 5 months ago
    2025-12-06 2:39:57

    Good times at 777becasino! A decent selection of games and a pretty smooth experience all around. Have a peek: 777becasino

  2. 5 months ago
    2025-12-19 13:54:53

    Been using keo188bet for a while now, and honestly, I’m happy with it. The odds are decent and the site’s easy to navigate. Give it a try if you’re into that sort of thing. Link’s right here: keo188bet

  3. 4 months ago
    2025-12-29 6:23:07

    CCZZCasino is alright, nothing crazy but it gets the job done. Fair games and quick loading times. Could be worth checking out: cczzcasino

  4. 3 weeks ago
    2026-4-13 6:24:45

    Been spinning the reels on s888slot. They’ve got a good selection of games, and I actually hit a decent win for once. Might be worth your time to checkout s888slot.

  5. 3 weeks ago
    2026-4-13 6:25:01

    Just signed up for i88casino today. Seems alright so far. They had the game I was looking for, so that’s a win so far. Worth checking out i88casino if you’re hunting something specific.

  6. 3 weeks ago
    2026-4-13 6:25:16

    Hey, I’ve been using hiww88vip’s VIP access, and it’s definitely worth it! Get some good offers. It can be fun when you have some free time. Check out hiww88vip and tell me what you think!

Send Comment Edit Comment


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