답안 #577814

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
577814 2022-06-15T09:29:55 Z tengiz05 Star Trek (CEOI20_startrek) C++17
45 / 100
102 ms 20664 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<i64>(n, 0));
    
    dfs = [&](int u, int p) {
        dp[1][u] = (c[0] + win[u] * c[1]) % P;
        dp[0][u] = (!win[u]) * c[1] % P;
        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[1][u] + dp[0][v] + (!!cnt) * dp[1][v]) % P;
                dp[0][u] = (dp[0][u] + (!cnt) * dp[1][v]) % P;
                cnt += !win[v];
            }
        }
    };
    
    dfs(0, -1);
    
    cout << dp[1][0] << "\n";
    
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 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 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 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 468 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 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 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 468 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 79 ms 13976 KB Output is correct
13 Correct 102 ms 20664 KB Output is correct
14 Correct 57 ms 8264 KB Output is correct
15 Correct 56 ms 8176 KB Output is correct
16 Correct 53 ms 8184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 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 468 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 0 ms 212 KB Output is correct
13 Incorrect 1 ms 340 KB Output isn't correct
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 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 468 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 79 ms 13976 KB Output is correct
13 Correct 102 ms 20664 KB Output is correct
14 Correct 57 ms 8264 KB Output is correct
15 Correct 56 ms 8176 KB Output is correct
16 Correct 53 ms 8184 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Incorrect 1 ms 340 KB Output isn't correct
19 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 1 ms 340 KB Output isn't correct
3 Halted 0 ms 0 KB -