結果

問題 No.2618 除霊
ユーザー KKT89
提出日時 2024-01-30 10:56:11
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 204 ms / 2,000 ms
コード長 1,592 bytes
コンパイル時間 2,680 ms
コンパイル使用メモリ 217,892 KB
最終ジャッジ日時 2025-02-19 00:46:22
ジャッジサーバーID
(参考情報)
judge4 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

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