結果

問題 No.1928 Make a Binary Tree
ユーザー ruthenruthen
提出日時 2022-05-07 01:58:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 199 ms / 3,000 ms
コード長 1,547 bytes
コンパイル時間 2,088 ms
コンパイル使用メモリ 213,532 KB
実行使用メモリ 57,616 KB
最終ジャッジ日時 2023-09-20 10:16:21
合計ジャッジ時間 9,349 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
12,812 KB
testcase_01 AC 6 ms
12,712 KB
testcase_02 AC 5 ms
12,752 KB
testcase_03 AC 6 ms
12,940 KB
testcase_04 AC 6 ms
12,752 KB
testcase_05 AC 6 ms
12,808 KB
testcase_06 AC 6 ms
12,756 KB
testcase_07 AC 6 ms
12,940 KB
testcase_08 AC 6 ms
12,828 KB
testcase_09 AC 6 ms
12,756 KB
testcase_10 AC 6 ms
12,804 KB
testcase_11 AC 6 ms
12,716 KB
testcase_12 AC 6 ms
12,948 KB
testcase_13 AC 5 ms
12,712 KB
testcase_14 AC 6 ms
12,704 KB
testcase_15 AC 11 ms
13,656 KB
testcase_16 AC 9 ms
13,560 KB
testcase_17 AC 9 ms
13,304 KB
testcase_18 AC 11 ms
13,764 KB
testcase_19 AC 7 ms
12,884 KB
testcase_20 AC 9 ms
13,228 KB
testcase_21 AC 8 ms
13,148 KB
testcase_22 AC 9 ms
13,452 KB
testcase_23 AC 8 ms
13,512 KB
testcase_24 AC 11 ms
13,840 KB
testcase_25 AC 11 ms
13,676 KB
testcase_26 AC 165 ms
33,192 KB
testcase_27 AC 95 ms
25,220 KB
testcase_28 AC 104 ms
26,568 KB
testcase_29 AC 188 ms
35,972 KB
testcase_30 AC 82 ms
24,160 KB
testcase_31 AC 118 ms
28,352 KB
testcase_32 AC 185 ms
35,656 KB
testcase_33 AC 97 ms
25,428 KB
testcase_34 AC 117 ms
28,328 KB
testcase_35 AC 92 ms
32,868 KB
testcase_36 AC 102 ms
50,472 KB
testcase_37 AC 94 ms
37,276 KB
testcase_38 AC 6 ms
12,804 KB
testcase_39 AC 144 ms
50,412 KB
testcase_40 AC 144 ms
50,408 KB
testcase_41 AC 147 ms
50,560 KB
testcase_42 AC 141 ms
50,476 KB
testcase_43 AC 142 ms
50,544 KB
testcase_44 AC 143 ms
50,424 KB
testcase_45 AC 143 ms
50,544 KB
testcase_46 AC 139 ms
50,524 KB
testcase_47 AC 137 ms
50,552 KB
testcase_48 AC 139 ms
50,616 KB
testcase_49 AC 108 ms
57,616 KB
testcase_50 AC 159 ms
33,012 KB
testcase_51 AC 156 ms
32,836 KB
testcase_52 AC 167 ms
32,784 KB
testcase_53 AC 174 ms
32,840 KB
testcase_54 AC 166 ms
32,792 KB
testcase_55 AC 98 ms
45,280 KB
testcase_56 AC 198 ms
36,180 KB
testcase_57 AC 199 ms
35,904 KB
testcase_58 AC 178 ms
36,020 KB
testcase_59 AC 117 ms
33,420 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#ifdef _RUTHEN
#include "debug.hpp"
#else
#define show(...) true
#endif

using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
template <class T> using V = vector<T>;

map<int, int> mp[200005];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N;
    cin >> N;
    V<V<int>> G(N);
    rep(i, N - 1) {
        int x, y;
        cin >> x >> y;
        x--, y--;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    auto rec = [&](auto f, int cur, int par) -> void {
        for (auto &nex : G[cur]) {
            if (nex == par) continue;
            f(f, nex, cur);
            for (auto &[val, cnt] : mp[nex]) {
                if (val > 0) mp[cur][val] += cnt;
            }
        }
        if (mp[cur].size() == 0) {
            mp[cur][1] += 1;
            return;
        }
        auto [val, cnt] = *mp[cur].rbegin();
        if (cnt >= 2) {
            mp[cur][val] -= 2;
            if (mp[cur][val] == 0) mp[cur].erase(val);
            mp[cur][2 * val + 1] += 1;
        } else {
            mp[cur].erase(val);
            int ret = val;
            if (mp[cur].size() > 0) {
                auto [val2, cnt2] = *mp[cur].rbegin();
                mp[cur][val2] -= 1;
                if (mp[cur][val2] == 0) mp[cur].erase(val2);
                ret += val2;
            }
            mp[cur][ret + 1] += 1;
        }
        return;
    };
    rec(rec, 0, -1);
    int ans = (*mp[0].rbegin()).first;
    cout << ans << '\n';
    return 0;
}
0