結果

問題 No.3514 Majority Driven Tree
コンテスト
ユーザー GOTKAKO
提出日時 2026-04-24 21:56:09
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 1,058 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,207 ms
コンパイル使用メモリ 221,364 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-04-24 21:56:14
合計ジャッジ時間 3,463 ms
ジャッジサーバーID
(参考情報)
judge2_1 / judge4_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other WA * 37
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int N; cin >> N;
    vector<vector<int>> Graph(N);
    for(int i=0; i<N-1; i++){
        int u,v; cin >> u >> v;
        u--; v--;
        Graph.at(u).push_back(v);
        Graph.at(v).push_back(u);
    }

    auto dfs = [&](auto dfs,int pos,int back) -> pair<int,int> {
        vector<pair<int,int>> kid;
        for(auto to : Graph.at(pos)) if(to != back) kid.push_back(dfs(dfs,to,pos));
        if(kid.size() == 0) return {1,0};

        int retw = 0,retb = 0,sumb = 0,sumw = 0;
        vector<int> D;
        for(auto [w,b] : kid) sumb += b,D.push_back(w-b);
        sort(D.begin(),D.end());
        D.push_back(1001001);

        retw = retb = sumb+1;
        int n = Graph.at(pos).size();
        for(int i=0; i<(n-1)/2; i++) sumb += D.at(i);
        retb = min(retb,sumb);
        sumb += D.at((n-1)/2);
        retw = min(retw,sumb);
        return {retw,retb};
    };
    auto [W,B] = dfs(dfs,0,-1);
    cout << W << endl; 
}
0