結果

問題 No.1718 Random Squirrel
ユーザー nawawannawawan
提出日時 2021-10-22 23:52:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,321 bytes
コンパイル時間 2,949 ms
コンパイル使用メモリ 223,928 KB
実行使用メモリ 47,576 KB
最終ジャッジ日時 2023-10-24 16:10:21
合計ジャッジ時間 9,282 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 WA -
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 WA -
testcase_12 AC 40 ms
7,092 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 211 ms
20,852 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 294 ms
27,068 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 367 ms
32,140 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 369 ms
32,136 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 222 ms
33,316 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 271 ms
47,576 KB
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
struct LCA{
    vector<vector<int>> parent;
    vector<int> depth;
    vector<vector<int>> G;
    int N;
    LCA(){}
    LCA(vector<vector<int>> &g) : N(g.size()), depth(g.size()), G(g){
        parent.resize(41, vector<int>(N));
    }
    LCA(int n): N(n), depth(n), G(n){
        parent.resize(41, vector<int>(n));
    }
    void add_edge(int u, int v){
        G[u].push_back(v);
        G[v].push_back(u);
    }
    void build(int root = 0){
        dfs(root, -1, 0, G);
        for(int i = 0; i < 40; i++){
            for(int j = 0; j < N; j++){
                if(parent[i][j] == -1) parent[i + 1][j] = -1;
                else parent[i + 1][j] = parent[i][parent[i][j]];
            }
        }
    }
    int lca(int v, int u){//lcaを求める
        if(depth[v] < depth[u]) swap(v, u);
        for(int k = 0; k < 41; k++){
            if((depth[v] - depth[u]) >> k & 1) v = parent[k][v];
        }
        if(v == u) return u;
        for(int k = 40; k >= 0; k--){
            if(parent[k][u] != parent[k][v]){
                u = parent[k][u];
                v = parent[k][v];
            }
        }
        return parent[0][u];
    }
private:
    void dfs(int v, int par, int d, vector<vector<int>>&G){
        parent[0][v] = par;
        depth[v] = d;
        for(int i: G[v]){
            if(i != par) dfs(i, v, d + 1, G);
        }
    }
};
int main(){
    int N, K;
    cin >> N >> K;
    vector<vector<int>> G(N);
    for(int i = 0; i < N - 1; i++){
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    vector<bool> D(N);
    for(int i = 0; i < K; i++) {
        int k;
        cin >> k;
        k--;
        D[k] = true;
    }
    LCA lca(G);
    lca.build();
    int INF = 1000000000;
    int l = 0, r = 0;
    {
        vector<int> d(N, -INF);
        queue<int> q;
        q.push(0);
        d[0] = 0;
        while(!q.empty()){
            int now = q.front();
            q.pop();
            for(int to : G[now]){
                if(d[to] == -INF){
                    q.push(to);
                    d[to] = d[now] + 1;
                }
            }
        }
        int ma = -INF;
        for(int i = 0; i < N; i++){
            if(D[i]){
                if(ma < d[i]) {
                    l = i;
                    ma = d[i];
                }
            }
        }
        q.push(l);
        d.assign(N, -INF);
        d[l] = 0;
        while(!q.empty()){
            int now = q.front();
            q.pop();
            for(int to : G[now]){
                if(d[to] == -INF){
                    q.push(to);
                    d[to] = d[now] + 1;
                }
            }
        }
        ma = -INF;
        for(int i = 0; i < N; i++){
            if(D[i]){
                if(ma < d[i]) {
                    r = i;
                    ma = d[i];
                }
            }
        }
    }
    vector<int> d(N, INF);
    queue<int> q;
    q.push(0);
    d[0] = 0;
    while(!q.empty()){
        int now = q.front();
        q.pop();
        for(int to: G[now]){
            if(d[to] == INF){
                d[to] = d[now] + 1;
                q.push(to);
            }
        }
    }
    vector<int> cnt(N);
    int sum = 0;
    auto dfs = [&](auto dfs, int now, int par = -1)->bool{
        bool f = false;
        for(int to : G[now]){
            if(to != par){
                f = dfs(dfs, to, now);
                cnt[now] += cnt[to];
            }
        }
        if(D[now]) {
            f = true;
            cnt[now]++;
        }
        if(f) sum += 2;
        return f;
    };
    dfs(dfs, 0);
    vector<int> ans(N);
    sum -= 2;
    auto dfs2 = [&](auto dfs2, int now, int s, int par = -1) -> void{
        for(int to : G[now]){
            if(to != par){
                if(cnt[to] == N) dfs2(dfs2, to, s - 2, now);
                else if(cnt[to] == 0) dfs2(dfs2, to, s + 2, now);
                else dfs2(dfs2, to, s, now);
            }
        }
        int tl = lca.lca(l, now), tr = lca.lca(r, now);
        int dl = d[now] + d[l] - 2 * d[tl], dr = d[now] + d[r] - 2 * d[tr];
        ans[now] = s - max(dl, dr);
    };
    dfs2(dfs2, 0, sum);
    for(int i = 0; i < N; i++){
        cout << ans[i] << endl;
    }
}
0