結果

問題 No.2618 除霊
ユーザー zawakasuzawakasu
提出日時 2024-01-26 22:15:05
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 251 ms / 2,000 ms
コード長 2,207 bytes
コンパイル時間 3,283 ms
コンパイル使用メモリ 257,300 KB
実行使用メモリ 27,924 KB
最終ジャッジ日時 2024-01-26 22:15:31
合計ジャッジ時間 14,332 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 192 ms
18,220 KB
testcase_07 AC 188 ms
18,188 KB
testcase_08 AC 230 ms
20,356 KB
testcase_09 AC 214 ms
20,684 KB
testcase_10 AC 224 ms
21,176 KB
testcase_11 AC 155 ms
17,296 KB
testcase_12 AC 216 ms
20,140 KB
testcase_13 AC 121 ms
17,916 KB
testcase_14 AC 152 ms
21,696 KB
testcase_15 AC 117 ms
16,376 KB
testcase_16 AC 121 ms
16,452 KB
testcase_17 AC 160 ms
22,372 KB
testcase_18 AC 184 ms
26,692 KB
testcase_19 AC 128 ms
16,964 KB
testcase_20 AC 149 ms
16,440 KB
testcase_21 AC 152 ms
21,336 KB
testcase_22 AC 180 ms
27,924 KB
testcase_23 AC 130 ms
18,228 KB
testcase_24 AC 129 ms
18,036 KB
testcase_25 AC 156 ms
20,316 KB
testcase_26 AC 167 ms
21,220 KB
testcase_27 AC 130 ms
17,948 KB
testcase_28 AC 131 ms
18,164 KB
testcase_29 AC 152 ms
20,308 KB
testcase_30 AC 171 ms
21,988 KB
testcase_31 AC 221 ms
20,164 KB
testcase_32 AC 193 ms
16,196 KB
testcase_33 AC 177 ms
17,220 KB
testcase_34 AC 181 ms
17,660 KB
testcase_35 AC 197 ms
18,764 KB
testcase_36 AC 214 ms
19,908 KB
testcase_37 AC 240 ms
20,028 KB
testcase_38 AC 251 ms
21,060 KB
testcase_39 AC 225 ms
21,296 KB
testcase_40 AC 221 ms
21,204 KB
testcase_41 AC 229 ms
21,532 KB
testcase_42 AC 226 ms
21,256 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>




namespace zawa {

using i16 = std::int16_t;
using i32 = std::int32_t;
using i64 = std::int64_t;
using i128 = __int128_t;

using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;

using usize = std::size_t;

} // namespace zawa


namespace zawa {

void SetFastIO() {
    std::cin.tie(nullptr)->sync_with_stdio(false);
}

void SetPrecision(u32 dig) {
    std::cout << std::fixed << std::setprecision(dig);
}

} // namespace zawa
using namespace zawa;

int main() {
    SetFastIO();

    int n; std::cin >> n;
    std::vector g(n, std::vector<int>{});
    for (int _{} ; _ < n - 1 ; _++) {
        int a, b; std::cin >> a >> b;
        a--; b--;
        g[a].emplace_back(b);
        g[b].emplace_back(a);
    }
    
    std::vector<int> pos(n, -1);
    int m; std::cin >> m;
    std::vector<int> ghost(m);
    for (int i{} ; auto& gh : ghost) {
        std::cin >> gh;
        gh--;
        pos[gh] = i;
        i++;
    }

    std::vector<int> count(n); 
    for (int i{} ; i < n ; i++) if (pos[i] != -1) {
        count[i]++;
        for (auto x : g[i]) {
            count[x]++;
        }
    }

    std::vector unique(m, std::vector<int>{});
    for (int i{} ; i < n ; i++) if (pos[i] != -1) {
        int gh{pos[i]};
        if (count[i] == 1) unique[gh].push_back(i);
        for (auto x : g[i]) {
            if (count[x] == 1) unique[gh].push_back(x);
        }
    }

    // for (auto x : count) std::cerr << x << ' ';
    // std::cerr << std::endl;

    int all{(int)std::ranges::count_if(count, [](const auto& i) -> bool { return i > 0; })};
    for (int v{} ; v < n ; v++) {
        int ans{all};
        if (pos[v] != -1) ans -= unique[pos[v]].size();
        for (auto x : g[v]) if (pos[x] != -1) ans -= unique[pos[x]].size();
        if (count[v] > 1) {
            ans--;
        }
        if (pos[v] != -1) {
            // std::cerr << v << " hello" << std::endl;
            for (auto x : g[v]) {
                // std::cerr << "travase " << x << std::endl;
                if (pos[x] == -1) continue;
                if (count[x] == 2) ans--;
            }
        }
        std::cout << ans << '\n';
    }
}
0