結果

問題 No.2618 除霊
ユーザー KKT89KKT89
提出日時 2024-01-30 10:56:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 214 ms / 2,000 ms
コード長 1,592 bytes
コンパイル時間 3,231 ms
コンパイル使用メモリ 219,976 KB
実行使用メモリ 17,480 KB
最終ジャッジ日時 2024-01-30 10:56:25
合計ジャッジ時間 14,415 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 183 ms
16,428 KB
testcase_07 AC 181 ms
16,396 KB
testcase_08 AC 197 ms
16,772 KB
testcase_09 AC 195 ms
16,400 KB
testcase_10 AC 209 ms
16,624 KB
testcase_11 AC 154 ms
16,608 KB
testcase_12 AC 211 ms
16,556 KB
testcase_13 AC 125 ms
17,288 KB
testcase_14 AC 159 ms
17,236 KB
testcase_15 AC 121 ms
16,992 KB
testcase_16 AC 126 ms
16,940 KB
testcase_17 AC 149 ms
17,480 KB
testcase_18 AC 179 ms
16,856 KB
testcase_19 AC 133 ms
17,396 KB
testcase_20 AC 121 ms
17,068 KB
testcase_21 AC 145 ms
16,784 KB
testcase_22 AC 158 ms
17,456 KB
testcase_23 AC 129 ms
17,476 KB
testcase_24 AC 134 ms
17,152 KB
testcase_25 AC 153 ms
17,420 KB
testcase_26 AC 156 ms
16,756 KB
testcase_27 AC 136 ms
17,052 KB
testcase_28 AC 137 ms
17,412 KB
testcase_29 AC 177 ms
17,360 KB
testcase_30 AC 172 ms
17,352 KB
testcase_31 AC 199 ms
16,708 KB
testcase_32 AC 158 ms
16,836 KB
testcase_33 AC 172 ms
16,836 KB
testcase_34 AC 170 ms
16,252 KB
testcase_35 AC 205 ms
16,588 KB
testcase_36 AC 198 ms
16,836 KB
testcase_37 AC 197 ms
16,444 KB
testcase_38 AC 206 ms
16,836 KB
testcase_39 AC 203 ms
16,836 KB
testcase_40 AC 202 ms
16,616 KB
testcase_41 AC 214 ms
16,836 KB
testcase_42 AC 201 ms
16,636 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) { return (ull)rng() % B; }

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector<vector<int>> g(n);
    vector<int> deg(n), cnt(n), cnt2(n);
    for (int i = 0; i < n - 1; i++) {
        int x, y;
        cin >> x >> y;
        x -= 1, y -= 1;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    int m;
    cin >> m;
    vector<bool> f(n);
    for (int i = 0; i < m; i++) {
        int a;
        cin >> a;
        a -= 1;
        f[a] = true;
        for (int b : g[a]) {
            deg[b] += 1;
        }
    }
    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum += (deg[i] >= 1 or f[i]);
        for (int t : g[i]) {
            cnt[i] += (deg[t] == 1);
            cnt2[i] += (deg[t] == 1 and !f[t]);
        }
    }
    for (int i = 0; i < n; i++) {
        int res = sum;
        if (deg[i] >= 1 or f[i]) res -= 1;

        if (f[i]) {
            res -= cnt[i];
            for (int t : g[i]) {
                if (f[t]) {
                    res -= cnt2[t];
                }
            }
            cout << res << "\n";
        } else {
            for (int t : g[i]) {
                if (f[t]) {
                    res -= cnt2[t] + (deg[t] == 0);
                    if (deg[i] == 1) res += 1;
                }
            }
            cout << res << "\n";
        }
    }
}
0