답안 #577809

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
577809 2022-06-15T09:28:31 Z tengiz05 Star Trek (CEOI20_startrek) C++17
30 / 100
102 ms 12752 KB
#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

constexpr int P = 1E9 + 7;

i64 power(i64 a, i64 b) {
    i64 res = 1;
    for (; b > 0; a = a * a % P, b /= 2) {
        if (b % 2) {
            res = res * a % P;
        }
    }
    return res;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n;
    i64 D;
    cin >> n >> D;
    
    if (n == 2) {
        cout << power(4, D) << "\n";
        return 0;
    }
    
    vector<vector<int>> e(n);
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        e[u].push_back(v);
        e[v].push_back(u);
    }
    
    vector<bool> win(n);
    
    function<void(int, int)> dfs = [&](int u, int p) {
        win[u] = false;
        for (int v : e[u]) {
            if (v != p) {
                dfs(v, u);
                win[u] = win[u] | (!win[v]);
            }
        }
    };
    dfs(0, -1);
    
    int c[2] {};
    dfs = [&](int u, int p) {
        c[win[u]]++;
        int cnt = 0;
        for (int v : e[u]) {
            cnt += win[v] == 0;
        }
        for (int v : e[u]) {
            if (v != p) {
                bool wu = win[u];
                bool wv = win[v];
                cnt -= !wv;
                win[u] = !!cnt;
                win[v] = win[v] | (!win[u]);
                dfs(v, u);
                win[u] = wu;
                win[v] = wv;
                cnt += !wv;
            }
        }
    };
    dfs(0, -1);
    
    vector dp(2, vector<int>(n, 0));
    
    dfs = [&](int u, int p) {
        dp[1][u] = c[0] + win[u] * c[1];
        dp[0][u] = (!win[u]) * c[1];
        int cnt = 0;
        for (int v : e[u]) {
            if (v != p) {
                cnt += !win[v];
            }
        }
        for (int v : e[u]) {
            if (v != p) {
                dfs(v, u);
                cnt -= !win[v];
                dp[1][u] += dp[0][v] + (!!cnt) * dp[1][v];
                dp[0][u] += (!cnt) * dp[1][v];
                cnt += !win[v];
            }
        }
    };
    
    dfs(0, -1);
    
    cout << dp[1][0] << "\n";
    
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Incorrect 1 ms 340 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 224 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Incorrect 102 ms 12752 KB Output isn't correct
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Incorrect 1 ms 368 KB Output isn't correct
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Incorrect 102 ms 12752 KB Output isn't correct
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Incorrect 1 ms 340 KB Output isn't correct
3 Halted 0 ms 0 KB -