結果

問題 No.277 根掘り葉掘り
ユーザー outlineoutline
提出日時 2021-02-15 18:41:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,604 bytes
コンパイル時間 1,559 ms
コンパイル使用メモリ 143,724 KB
実行使用メモリ 44,764 KB
最終ジャッジ日時 2023-09-30 15:40:25
合計ジャッジ時間 4,504 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 WA -
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 134 ms
44,764 KB
testcase_10 AC 71 ms
16,780 KB
testcase_11 AC 97 ms
15,696 KB
testcase_12 AC 128 ms
29,984 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;

using ll = long long;
constexpr int INF = 1001001001;
// constexpr int mod = 1000000007;
constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

template <typename T>
struct ReRooting {
    using SubtreeMerge = function<T(T, T)>;
    using PrefixSuffixMerge = function<T(T, T)>;

    vector<vector<int>> g;
    const SubtreeMerge f1;
    const PrefixSuffixMerge f2;
    const T IE;
    vector<T> subtree_dp, rerooting_dp;
    
    ReRooting(int V, const SubtreeMerge s, const PrefixSuffixMerge p, const T I)
        : g(V), f1(s), f2(p), IE(I), subtree_dp(V, I), rerooting_dp(V, I) {}

    void add_edge(int u, int v){
        g[u].emplace_back(v);
        g[v].emplace_back(u);
    }

    void subtree_dfs(int from, int par){
        for(int to : g[from]){
            if(to != par){
                subtree_dfs(to, from);
                subtree_dp[from] = f1(subtree_dp[from], subtree_dp[to]);
            }
        }
        if(subtree_dp[from] == IE)  subtree_dp[from] = 0;
    }

    void rerooting_function(int cur, T par_val, int par){
        int V = g[cur].size();
        vector<T> prefix(V + 1, IE), suffix(V + 1, IE);
        for(int i = 0; i < V; ++i){
            int child = g[cur][i];
            if(child == par)    prefix[i + 1] = f1(prefix[i], par_val);
            else    prefix[i + 1] = f1(prefix[i], subtree_dp[child]);
        }
        for(int i = V - 1; i >= 0; --i){
            int child = g[cur][i];
            if(child == par)    suffix[i] = f1(suffix[i + 1], par_val);
            else    suffix[i] = f1(suffix[i + 1], subtree_dp[child]);
        }
        for(int i = 0; i < V; ++i){
            int child = g[cur][i];
            if(child != par){
                rerooting_dp[child] = f1(subtree_dp[child], f2(prefix[i], suffix[i + 1]));
                rerooting_function(child, f2(prefix[i], suffix[i + 1]), cur);
            }
        }
    }

    vector<T> build(int root = 0){
        subtree_dfs(root, -1);
        rerooting_function(root, IE, -1);
        rerooting_dp[root] = subtree_dp[root];
        return rerooting_dp;
    }
};

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

    int n;
    cin >> n;
    vector<vector<int>> g(n);
    auto f = [](int a, int b){return min(a, b + 1);};
    ReRooting<int> gg(n, f, f, INF);
    for(int i = 1; i < n; ++i){
        int x, y;
        cin >> x >> y;
        --x, --y;
        g[x].emplace_back(y);
        g[y].emplace_back(x);
        gg.add_edge(x, y);
    }

    vector<int> dep(n);
    auto dfs = [&](auto&& self, int from = 0, int d = 0, int par = -1) -> void {
        dep[from] = d;
        for(int to : g[from]){
            if(to != par)   self(self, to, d + 1, from);
        }
    };
    dfs(dfs);

    auto dp = gg.build();

    for(int i = 0; i < n; ++i){
        cout << min(dep[i], dp[i]) << '\n';
    }

    return 0;
}
0