結果

問題 No.2677 Minmax Independent Set
ユーザー nono00nono00
提出日時 2024-03-15 23:32:57
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,379 bytes
コンパイル時間 3,366 ms
コンパイル使用メモリ 257,868 KB
実行使用メモリ 34,432 KB
最終ジャッジ日時 2024-09-30 03:07:02
合計ジャッジ時間 7,081 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 3 ms
5,248 KB
testcase_04 AC 3 ms
5,248 KB
testcase_05 AC 91 ms
14,692 KB
testcase_06 AC 93 ms
15,076 KB
testcase_07 AC 67 ms
15,340 KB
testcase_08 AC 64 ms
15,804 KB
testcase_09 AC 70 ms
16,740 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 68 ms
34,432 KB
testcase_13 AC 74 ms
19,972 KB
testcase_14 AC 64 ms
19,948 KB
testcase_15 AC 71 ms
24,552 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 47 ms
12,648 KB
testcase_29 AC 76 ms
26,396 KB
testcase_30 AC 1 ms
5,248 KB
testcase_31 AC 2 ms
5,248 KB
testcase_32 AC 1 ms
5,248 KB
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 AC 19 ms
6,764 KB
testcase_51 AC 3 ms
5,248 KB
testcase_52 AC 42 ms
10,984 KB
testcase_53 AC 39 ms
10,476 KB
testcase_54 AC 3 ms
5,248 KB
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 AC 44 ms
12,648 KB
testcase_61 WA -
testcase_62 AC 46 ms
17,000 KB
testcase_63 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#include <iterator>
#include <ranges>
#include <vector>

namespace nono {

template <class T>
struct EdgeBase {
    int from;
    int to;
    T weight;
    EdgeBase() {}
    EdgeBase(int from, int to, T weight = 1): from(from), to(to), weight(weight) {}
};

using Edge = EdgeBase<int>;
template <class T>
using WeightedEdge = EdgeBase<T>;

template <class T>
class Graph {
    struct Edge_ {
        int to;
        T weight;
        int id;
    };

    using iterator = std::vector<Edge_>::iterator;
    using const_iterator = std::vector<Edge_>::const_iterator;
    using subrange = std::ranges::subrange<iterator, iterator>;
    using const_subrange = std::ranges::subrange<const_iterator, const_iterator>;

  public:
    template <class U>
    friend Graph<U> to_undirected_graph(int n, const std::vector<EdgeBase<U>>& edges);
    template <class U>
    friend Graph<U> to_directed_graph(int n, const std::vector<EdgeBase<U>>& edges);

    subrange operator[](int i) {
        return std::ranges::subrange(edges_.begin() + indptr_[i], edges_.begin() + indptr_[i + 1]);
    }
    const_subrange operator[](int i) const {
        return std::ranges::subrange(edges_.begin() + indptr_[i], edges_.begin() + indptr_[i + 1]);
    }

    int size() const {
        return n_;
    }
    int edge_size() const {
        return m_;
    }
    bool is_directed() const {
        return directed_;
    }
    bool is_undirected() const {
        return !is_directed();
    }

  private:
    Graph(int n, const std::vector<EdgeBase<T>>& edges, bool directed)
        : n_(n),
          m_(edges.size()),
          indptr_(n_ + 1),
          edges_(directed ? edges.size() : 2 * edges.size()),
          directed_(directed) {
        for (const auto& e: edges) {
            indptr_[e.from + 1]++;
            if (!directed_) indptr_[e.to + 1]++;
        }
        for (int i = 0; i < n_; i++) {
            indptr_[i + 1] += indptr_[i];
        }
        auto index = indptr_;
        for (int i = 0; i < std::ssize(edges); i++) {
            const auto& e = edges[i];
            edges_[index[e.from]++] = Edge_(e.to, e.weight, i);
            if (!directed_) edges_[index[e.to]++] = Edge_(e.from, e.weight, i);
        }
    }

    int n_;
    int m_;
    std::vector<int> indptr_;
    std::vector<Edge_> edges_;
    bool directed_;
};

template <class T>
Graph<T> to_undirected_graph(int n, const std::vector<EdgeBase<T>>& edges) {
    return Graph<T>(n, edges, false);
}

template <class T>
Graph<T> to_directed_graph(int n, const std::vector<EdgeBase<T>>& edges) {
    return Graph<T>(n, edges, true);
}

}  //  namespace nono

namespace nono {

void solve() {
    int n;
    std::cin >> n;
    std::vector<Edge> edges;
    for (int i = 0; i + 1 < n; i++) {
        int u, v;
        std::cin >> u >> v;
        u--;
        v--;
        edges.emplace_back(u, v);
    }
    auto graph = to_undirected_graph(n, edges);

    std::vector<int> bdp(n);
    std::vector<int> wdp(n);
    {
        auto dfs = [&](auto self, int u, int p) -> void {
            bdp[u] = 1;
            wdp[u] = 0;
            for (auto e: graph[u]) {
                if (e.to == p) continue;
                self(self, e.to, u);
                bdp[u] += wdp[e.to];
                wdp[u] += bdp[e.to];
            }
        };
        dfs(dfs, 0, -1);
    }
    int ans = 1e9;
    {
        auto dfs = [&](auto self, int u, int p, int pwv, int pbv) -> void {
            int black = 1;
            for (auto e: graph[u]) {
                if (e.to == p) {
                    black += pwv;
                } else {
                    black += wdp[e.to];
                }
            }
            ans = std::min(ans, black);
            int white = 0;
            for (auto e: graph[u]) {
                if (e.to == p) {
                    white += pbv;
                } else {
                    white += bdp[e.to];
                }
            }
            for (auto e: graph[u]) {
                if (e.to == p) continue;
                self(self, e.to, u, white - bdp[e.to], black - wdp[e.to]);
            }
        };
        dfs(dfs, 0, -1, 0, 0);
    }
    std::cout << ans << '\n';
}

}  //  namespace nono

int main() {
    std::cin.tie(0)->sync_with_stdio(0);
    std::cout << std::fixed << std::setprecision(16);
    int t = 1;

    while (t--) nono::solve();
}
0