結果

問題 No.277 根掘り葉掘り
ユーザー outlineoutline
提出日時 2021-02-15 18:48:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 135 ms / 3,000 ms
コード長 3,654 bytes
コンパイル時間 1,753 ms
コンパイル使用メモリ 145,232 KB
実行使用メモリ 44,804 KB
最終ジャッジ日時 2023-09-30 16:02:12
合計ジャッジ時間 4,319 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 135 ms
44,804 KB
testcase_10 AC 67 ms
16,632 KB
testcase_11 AC 90 ms
15,460 KB
testcase_12 AC 116 ms
29,976 KB
testcase_13 AC 118 ms
15,892 KB
testcase_14 AC 94 ms
16,932 KB
testcase_15 AC 102 ms
18,464 KB
testcase_16 AC 99 ms
17,016 KB
testcase_17 AC 93 ms
16,896 KB
testcase_18 AC 96 ms
16,564 KB
testcase_19 AC 94 ms
16,216 KB
権限があれば一括ダウンロードができます

ソースコード

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);};
    auto m = [](int a, int b){return min(a, b);};
    ReRooting<int> gg(n, f, m, 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