結果

問題 No.1718 Random Squirrel
ユーザー milanis48663220milanis48663220
提出日時 2022-04-30 11:18:23
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 278 ms / 2,000 ms
コード長 4,223 bytes
コンパイル時間 1,716 ms
コンパイル使用メモリ 130,824 KB
実行使用メモリ 70,960 KB
最終ジャッジ日時 2023-09-12 04:00:06
合計ジャッジ時間 10,417 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 183 ms
18,164 KB
testcase_12 AC 30 ms
5,836 KB
testcase_13 AC 102 ms
12,132 KB
testcase_14 AC 176 ms
18,696 KB
testcase_15 AC 155 ms
16,340 KB
testcase_16 AC 69 ms
9,300 KB
testcase_17 AC 159 ms
17,328 KB
testcase_18 AC 217 ms
21,064 KB
testcase_19 AC 147 ms
15,720 KB
testcase_20 AC 60 ms
8,472 KB
testcase_21 AC 256 ms
24,612 KB
testcase_22 AC 278 ms
24,996 KB
testcase_23 AC 259 ms
24,608 KB
testcase_24 AC 255 ms
24,464 KB
testcase_25 AC 263 ms
25,000 KB
testcase_26 AC 194 ms
25,136 KB
testcase_27 AC 192 ms
25,116 KB
testcase_28 AC 193 ms
24,884 KB
testcase_29 AC 245 ms
70,844 KB
testcase_30 AC 244 ms
70,904 KB
testcase_31 AC 244 ms
70,908 KB
testcase_32 AC 242 ms
70,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

#include <vector>

using namespace std;

struct S {
    int sum_children;
    ll come_back, not_come_back;
    S(int sum_children, ll come_back, ll not_come_back): sum_children(sum_children), come_back(come_back), not_come_back(not_come_back) {}
};

S e(){
    return S(0, 0, 0);
}

S f(S dp, int v){
    return dp;
};

bool is_drink[100000];

S g(S dp, int v){
    if(is_drink[v]){
        return S(dp.sum_children+1, dp.come_back, dp.not_come_back);
    }
    return dp;
};

S merge(S a, S b){
    if(b.sum_children == 0) return a;
    ll come_back = a.come_back+b.come_back+2;
    ll not_come_back = min(a.come_back+b.not_come_back+1, a.not_come_back+b.come_back+2);
    return S(a.sum_children+b.sum_children, come_back, not_come_back);
};

S merge2(S a, S b){
    if(b.sum_children == 0) return a;
    ll come_back = a.come_back+b.come_back;
    ll not_come_back = min(a.come_back+b.not_come_back, a.not_come_back+b.come_back);
    return S(a.sum_children+b.sum_children, come_back, not_come_back);
};

template<typename T>
struct ReRooting {
    int n;
    T(*e)();
    T(*merge)(T, T);
    T(*f)(T, int);
    T(*g)(T, int);
    vector<vector<int>> G;
    vector<vector<T>> dp;
    ReRooting(int n, T(*e)(), T(*merge)(T, T), T(*f)(T, int), T(*g)(T, int)): n(n), e(e), merge(merge), f(f), g(g) {
        dp.resize(n);
        G.resize(n);
    }
    ReRooting(vector<vector<int>> G, T(*e)(), T(*merge)(T, T), T(*f)(T, int), T(*g)(T, int)): n(G.size()), G(G), e(e), merge(merge), f(f), g(g) {
        dp.resize(n);
    }
    void add_edge(int u, int v){
        G[u].push_back(v);
        G[v].push_back(u);
    }
    T dfs1(int v, int p){
        T ans = e();
        for(int i = 0; i < G[v].size(); i++){
            int u = G[v][i];
            if(u == p) continue;
            dp[v][i] = dfs1(u, v);
            ans = merge(ans, dp[v][i]);
        }
        return g(ans, v);
    }
    void dfs2(int v, int p, T from_par){
        int deg = G[v].size();
        for(int i = 0; i < deg; i++){
            if(G[v][i] == p){
                dp[v][i] = from_par;
                break;
            }
        }
        vector<T> from_right(deg+1, e());
        for(int i = deg-1; i >= 0; i--){
            from_right[i] = merge(from_right[i+1], f(dp[v][i], G[v][i]));
        }
        T from_left = e();
        for(int i = 0; i < deg; i++){
            int u = G[v][i];
            if(u != p){
                T x = merge2(from_left, from_right[i+1]);
                dfs2(u, v, g(x, v));
            }
            from_left = merge(from_left, f(dp[v][i], G[v][i]));
        }
    }
    vector<T> solve(int r = 0){
        for(int i = 0; i < n; i++) dp[i].assign(G[i].size(), e());
        dfs1(r, -1);
        dfs2(r, -1, e());
        vector<T> ans(n, e());
        for(int v = 0; v < n; v++){
            for(int i = 0; i < G[v].size(); i++){
                ans[v] = merge(ans[v], f(dp[v][i], G[v][i]));
            }
            ans[v] = g(ans[v], v);
        }
        return ans;
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n, k; cin >> n >> k;
    vector<vector<int>> G(n);
    for(int i = 0; i < n-1; i++){
        int u, v; cin >> u >> v; u--; v--;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    vector<int> d(k);
    for(int i = 0; i < k; i++) {
        cin >> d[i];
        d[i]--;
        is_drink[d[i]] = true;
    }

    ReRooting<S> rr(G, e, merge, f, g);
    auto ans = rr.solve();
    for(int i = 0; i < n; i++){
        cout << ans[i].not_come_back << endl;
    }
}
0