結果

問題 No.1976 Cut then Connect
ユーザー CyanmondCyanmond
提出日時 2022-06-10 23:21:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 12,957 bytes
コンパイル時間 2,555 ms
コンパイル使用メモリ 212,780 KB
実行使用メモリ 15,616 KB
最終ジャッジ日時 2024-09-21 06:52:05
合計ジャッジ時間 5,743 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 169 ms
14,848 KB
testcase_03 WA -
testcase_04 AC 28 ms
6,944 KB
testcase_05 AC 75 ms
9,344 KB
testcase_06 AC 119 ms
12,416 KB
testcase_07 WA -
testcase_08 AC 175 ms
15,608 KB
testcase_09 AC 141 ms
13,696 KB
testcase_10 AC 33 ms
6,944 KB
testcase_11 AC 170 ms
15,232 KB
testcase_12 WA -
testcase_13 AC 126 ms
12,416 KB
testcase_14 AC 115 ms
11,904 KB
testcase_15 AC 101 ms
11,392 KB
testcase_16 AC 152 ms
14,464 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 121 ms
12,416 KB
testcase_20 AC 181 ms
15,616 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 2 ms
6,944 KB
testcase_26 AC 2 ms
6,944 KB
testcase_27 AC 2 ms
6,944 KB
testcase_28 AC 2 ms
6,944 KB
testcase_29 AC 2 ms
6,944 KB
testcase_30 AC 2 ms
6,940 KB
testcase_31 AC 2 ms
6,940 KB
testcase_32 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 2 "ProCon/Library/template/util.hpp"
#include <algorithm>
#include <array>
#include <cassert>
#include <cstdint>
#include <iostream>
#include <iterator>
#include <limits>
#include <numeric>
#include <queue>
#include <random>
#include <tuple>
#include <type_traits>
#include <vector>

using i8 = std::int8_t;
using u8 = std::uint8_t;
using i16 = std::int16_t;
using i32 = std::int32_t;
using i64 = std::int64_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;

constexpr i8 operator""_i8(unsigned long long n) noexcept { return static_cast<i8>(n); }
constexpr i16 operator""_i16(unsigned long long n) noexcept { return static_cast<i16>(n); }
constexpr i32 operator""_i32(unsigned long long n) noexcept { return static_cast<i32>(n); }
constexpr i64 operator""_i64(unsigned long long n) noexcept { return static_cast<i64>(n); }
constexpr u8 operator""_u8(unsigned long long n) noexcept { return static_cast<u8>(n); }
constexpr u16 operator""_u16(unsigned long long n) noexcept { return static_cast<u16>(n); }
constexpr u32 operator""_u32(unsigned long long n) noexcept { return static_cast<u32>(n); }
constexpr u64 operator""_u64(unsigned long long n) noexcept { return static_cast<u64>(n); }

constexpr char eoln = '\n';

template <typename T, T Div = 2> constexpr T infty = std::numeric_limits<T>::max() / Div - 2;
template <class T> using RevPriorityQueue = std::priority_queue<T, std::vector<T>, std::greater<T>>;

constexpr std::array<std::pair<int, int>, 4> dxy4 = {{{1, 0}, {0, 1}, {-1, 0}, {0, -1}}};

constexpr std::array<i64, 6> infty_list = {infty<short>,  infty<int>, infty<i64>,
                                           infty<i64, 3>, infty<u32>, infty<u64>};
template <typename T> bool is_infty(const T v) {
    if (v > 0 and static_cast<u64>(v) > infty<u64>) return false;
    for (const auto e : infty_list) {
        if (e == static_cast<i64>(v)) return true;
    }
    return false;
}

class Range {
    struct Iterator {
        int itr;
        constexpr Iterator(const int pos) noexcept : itr(pos) {}
        constexpr void operator++() noexcept { ++itr; }
        constexpr bool operator!=(const Iterator &other) const noexcept { return itr != other.itr; }
        constexpr int operator*() const noexcept { return itr; }
    };
    const Iterator first, last;

  public:
    explicit constexpr Range(const int f, const int l) noexcept : first(f), last(std::max(f, l)) {}
    constexpr Iterator begin() const noexcept { return first; }
    constexpr Iterator end() const noexcept { return last; }
};

class ReversedRange {
    struct Iterator {
        int itr;
        constexpr Iterator(const int pos) noexcept : itr(pos) {}
        constexpr void operator++() noexcept { --itr; }
        constexpr bool operator!=(const Iterator &other) const noexcept { return itr != other.itr; }
        constexpr int operator*() const noexcept { return itr; }
    };
    const Iterator first, last;

  public:
    explicit constexpr ReversedRange(const int f, const int l) noexcept
        : first(l - 1), last(std::min(f, l) - 1) {}
    constexpr Iterator begin() const noexcept { return first; }
    constexpr Iterator end() const noexcept { return last; }
};

#define SIKICM_REP1(i, r) for (const int i : Range(0, r))
#define SIKICM_REP2(i, l, r) for (int i : Range(l, r))
#define SIKICM_RVP1(i, r) for (const int i : ReversedRange(0, r))
#define SIKICM_RVP2(i, l, r) for (int i : ReversedRange(l, r))
#define SIKICM_SELECT2(a, b, c, name, ...) name
#define REP(...) SIKICM_SELECT2(__VA_ARGS__, SIKICM_REP2, SIKICM_REP1)(__VA_ARGS__)
#define RVP(...) SIKICM_SELECT2(__VA_ARGS__, SIKICM_RVP2, SIKICM_RVP1)(__VA_ARGS__)
#define HRL(n) for ([[maybe_unused]] const int loop_counter : Range(0, n))

template <class Container> constexpr int len(const Container &c) {
    return static_cast<int>(std::size(c));
}

template <typename T> constexpr bool chmin(T &v, const T a) {
    if (v > a) {
        v = a;
        return true;
    }
    return false;
}

template <typename T> constexpr bool chmax(T &v, const T a) {
    if (v < a) {
        v = a;
        return true;
    }
    return false;
}

template <typename T> constexpr T ceil_div(const T x, const T y) {
    assert(y != 0);
    assert(x >= 0 and y > 0);
    return (x + y - 1) / y;
}

template <class Container, class T> constexpr int lwb(const Container &c, const T &val) {
    return static_cast<int>(std::distance(c.cbegin(), std::lower_bound(c.cbegin(), c.cend(), val)));
}

template <class Container, class T> constexpr int upb(const Container &c, const T &val) {
    return static_cast<int>(std::distance(c.cbegin(), std::upper_bound(c.cbegin(), c.cend(), val)));
}

template <class Container, class F> constexpr int lmp(const Container &c, const F &f) {
    return static_cast<int>(
        std::distance(c.cbegin(), std::partition_point(c.cbegin(), c.cend(), f)));
}

template <class T> auto make_vec(const int n, const T &value) { return std::vector<T>(n, value); }
template <class... Args> auto make_vec(const int n, Args... args) {
    return std::vector<decltype(make_vec(args...))>(n, make_vec(args...));
}

template <class T, class... Tail>
void renumber(const std::vector<int> &order, std::vector<T> &head, Tail &... tail) {
    const int n = len(order);
    std::vector<T> sorted_head(n);
    REP(i, n) sorted_head[i] = head[order[i]];

    head = std::move(sorted_head);
    if constexpr (sizeof...(Tail) != 0) {
        renumber(order, tail...);
    }
}

template <class Head, class... Tail>
std::vector<int> priority_sort(std::vector<Head> &head, std::vector<Tail> &... tail) {
    const int n = len(head);
    std::vector<std::tuple<Head, Tail..., int>> res(n);
    REP(i, n) res[i] = std::make_tuple(head[i], tail[i]..., i);
    std::sort(res.begin(), res.end());

    std::vector<int> order(n);
    REP(i, 0, n)
    order[i] = std::get<std::tuple_size_v<std::tuple<Head, Tail...>>>(res[i]);
    renumber(order, head, tail...);
    return order;
}

std::vector<int> iotav(const int n) {
    std::vector<int> ret(n);
    std::iota(ret.begin(), ret.end(), 0);
    return ret;
}

template <class Container> auto calc_sum(const Container &c) {
    return std::accumulate(c.cbegin(), c.cend(), static_cast<typename Container::value_type>(0));
}

template <typename T> constexpr T ceil_log2(const T x) {
    int e = 0;
    while ((static_cast<T>(1) << e) < x) ++e;
    return e;
}

template <typename T> constexpr int ceil_pow2(const T x) { return 1 << ceil_log2(x); }
#line 5 "ProCon/Library/template/debug.hpp"
#include <utility>

namespace DebugImpl {

template <typename U, typename = void> struct is_specialize : std::false_type {};
template <typename U>
struct is_specialize<U, typename std::conditional<false, typename U::iterator, void>::type>
    : std::true_type {};
template <typename U>
struct is_specialize<U, typename std::conditional<false, decltype(U::first), void>::type>
    : std::true_type {};
template <typename U>
struct is_specialize<U, std::enable_if_t<std::is_integral<U>::value, void>> : std::true_type {};

void dump(const char &t) { std::cerr << t; }

void dump(const std::string &t) { std::cerr << t; }

void dump(const bool &t) { std::cerr << (t ? "true" : "false"); }

template <typename U, std::enable_if_t<!is_specialize<U>::value, std::nullptr_t> = nullptr>
void dump(const U &t) {
    std::cerr << t;
}

template <typename T>
void dump(const T &t, std::enable_if_t<std::is_integral<T>::value> * = nullptr) {
    std::string res;
    if (is_infty(t)) res = "inf";
    if constexpr (std::is_signed<T>::value) {
        if (is_infty(-t)) res = "-inf";
    }
    if (res.empty()) res = std::to_string(t);
    std::cerr << res;
}

template <typename T, typename U> void dump(const std::pair<T, U> &);
template <typename T> void dump(const std::pair<T *, int> &);

template <typename T>
void dump(const T &t, std::enable_if_t<!std::is_void<typename T::iterator>::value> * = nullptr) {
    std::cerr << "[ ";
    for (auto it = t.begin(); it != t.end();) {
        dump(*it);
        std::cerr << (++it == t.end() ? "" : ", ");
    }
    std::cerr << " ]";
}

template <typename T, typename U> void dump(const std::pair<T, U> &t) {
    std::cerr << "( ";
    dump(t.first);
    std::cerr << ", ";
    dump(t.second);
    std::cerr << " )";
}

template <typename T> void dump(const std::pair<T *, int> &t) {
    std::cerr << "[ ";
    for (int i = 0; i < t.second; i++) {
        dump(t.first[i]);
        std::cerr << (i == t.second - 1 ? "" : ", ");
    }
    std::cerr << " ]";
}

void trace() { std::cerr << std::endl; }
template <typename Head, typename... Tail> void trace(Head &&head, Tail &&... tail) {
    std::cerr << " ";
    dump(head);
    if (sizeof...(tail) != 0) std::cerr << ',';
    trace(std::forward<Tail>(tail)...);
}

} // namespace DebugImpl

#ifdef SIKI_DEBUG
#define vpr(...)                                                                                   \
    do {                                                                                           \
        std::cerr << "## " << #__VA_ARGS__ << " =";                                               \
        DebugImpl::trace(__VA_ARGS__);                                                             \
    } while (0)
#else
#define vpr(...) (void(0))
#endif

// https://github.com/NyaanNyaan/library/blob/master/template/debug.hpp
#line 2 "ProCon/Library/template/macro.hpp"

#define ALL(x) (x).begin(), (x).end()
#define pf push_front
#define pb push_back
#define ef emplace_front
#define eb emplace_back
#define ppf pop_front
#define ppb pop_back
#line 4 "main.cpp"
#include <bits/stdc++.h>

using namespace std;

int main() {
    int N;
    cin >> N;
    vector<int> U(N - 1), V(N - 1);
    vector<vector<int>> Tree(N);
    REP(i, N - 1) {
        cin >> U[i] >> V[i];
        --U[i], --V[i];
        Tree[U[i]].pb(V[i]);
        Tree[V[i]].pb(U[i]);
    }

    vector<vector<int>> chd_ds(N);
    {
        auto dfs = [&](auto &&self, const int v, const int p) -> int {
            for (const int t : Tree[v]) {
                if (t == p) {
                    chd_ds[v].pb(0);
                    continue;
                }
                const int chd_s = self(self, t, v);
                chd_ds[v].pb(chd_s);
            }
            return *max_element(ALL(chd_ds[v])) + 1;
        };
        dfs(dfs, 0, -1);
    }
    vpr(chd_ds);

    vector<int> subtree_dim(N);
    {
        auto dfs = [&](auto &&self, const int v, const int p) -> void {
            int ret = 0;
            for (const int t : Tree[v]) {
                if (t == p) continue;
                self(self, t, v);
                chmax(ret, subtree_dim[t]);
            }
            auto calc_subtree_dim1 = [&]() {
                int ma = 0, ma_id = 0;
                REP(i, len(Tree[v]))
                if (chmax(ma, chd_ds[v][i])) ma_id = i;
                int sm = 0;
                REP(i, len(Tree[v])) if (i != ma_id) chmax(sm, chd_ds[v][i]);
                return ma + sm;
            };
            subtree_dim[v] = max(ret, calc_subtree_dim1());
        };
        dfs(dfs, 0, -1);
    }

    vpr(subtree_dim);
    int ans = infty<int>;

    auto dfs_reroot = [&](auto &&self, const int v, const int p, const int p_max_depth,
                          const int p_dim) -> void {
        if (len(Tree[v]) == 1 and v != 0) {
            chmin(ans, max(p_dim, ceil_div(p_dim, 2) + 1));
            return;
        }
        if (v != 0)
            chmin(ans, max({subtree_dim[v], p_dim,
                            ceil_div(subtree_dim[v], 2) + ceil_div(p_dim, 2) + 1}));

        vector<int> ma1_l, ma2_l, ma1_r, ma2_r;
        auto make_tb = [&](vector<int> &m1, vector<int> &m2) {
            m1.pb(0);
            m2.pb(0);
            for (const int e : chd_ds[v]) {
                if (m1.back() <= e) {
                    m2.pb(m1.back());
                    m1.pb(e);
                } else if (m2.back() < e) {
                    m1.pb(m1.back());
                    m2.pb(e);
                } else {
                    m1.pb(m1.back());
                    m2.pb(m2.back());
                }
            }
        };
        make_tb(ma1_l, ma2_l);
        reverse(ALL(chd_ds[v]));
        make_tb(ma1_r, ma2_r);
        reverse(ALL(chd_ds[v]));
        reverse(ALL(ma1_r));
        reverse(ALL(ma2_r));

        REP(i, len(Tree[v])) {
            const int t = Tree[v][i];
            if (t == p) continue;
            int v1 = max(ma1_l[i], ma1_r[i + 1]);
            int v2 = max(min(ma1_l[i], ma1_r[i + 1]), max(ma2_l[i], ma2_r[i + 1]));
            int n_dim = v1 + v2 + p_max_depth - min({v1, v2, p_max_depth});
            chmax(n_dim, p_dim);
            chmax(n_dim, max({v1, v2, p_max_depth}));
            int n_mad = max(p_max_depth + 1, max(ma1_l[i], ma1_r[i + 1]) + 1);
            self(self, t, v, n_mad, n_dim);
        }
    };
    dfs_reroot(dfs_reroot, 0, -1, 0, 0);

    cout << ans << endl;
}
0