Submission #579121

# Submission time Handle Problem Language Result Execution time Memory
579121 2022-06-18T11:54:35 Z piOOE Star Trek (CEOI20_startrek) C++17
100 / 100
93 ms 19484 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 100000, mod = 1e9 + 7;

int add(int a, int b) {
    return a + b < mod ? a + b : a + b - mod;
}

int mul(int a, int b) {
    return a * (ll) b % mod;
}

int binp(int a, ll p) {
    int ans = 1;
    for (; p > 0; p >>= 1, a = mul(a, a)) {
        if (p & 1) {
            ans = mul(ans, a);
        }
    }
    return ans;
}

int sub(int a, int b) {
    return a >= b ? a - b : a - b + mod;
}

int inv(int a) {
    return binp(a, mod - 2);
}

vector<int> g[N];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    ll d;
    cin >> n >> d;
    for (int i = 1; i < n; ++i) {
        int a, b;
        cin >> a >> b;
        --a, --b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    vector<bool> dp(n);
    vector<int> cntLoosing(n), c(n), total(n), only_loosing(n);

    function<void(int, int)> dfs1 = [&](int v, int p) {
        dp[v] = false;
        cntLoosing[v] = 0;
        for (int to: g[v]) {
            if (to != p) {
                dfs1(to, v);
                if (!dp[to]) {
                    only_loosing[v] += c[to];
                    ++cntLoosing[v];
                    dp[v] = true;
                }
                total[v] += c[to];
            }
        }
        if (cntLoosing[v] > 1) {
            c[v] = 0;
        } else if (cntLoosing[v] == 1) {
            c[v] = only_loosing[v];
        } else {
            c[v] = total[v] + 1;
        }
    };

    function<void(int, int)> dfs2 = [&](int v, int p) {
        dp[v] = cntLoosing[v];
        if (cntLoosing[v] > 1) {
            c[v] = 0;
        } else if (cntLoosing[v] == 1) {
            c[v] = only_loosing[v];
        } else {
            c[v] = total[v] + 1;
        }
        for (int to: g[v]) {
            if (to != p) {
                int gg = c[to];
                total[v] -= gg;
                bool zz = dp[to];
                if (!dp[to]) {
                    --cntLoosing[v];
                    only_loosing[v] -= gg;
                }
                int now;
                if (cntLoosing[v] > 1) {
                    now = 0;
                } else if (cntLoosing[v] == 1) {
                    now = only_loosing[v];
                } else {
                    now = total[v] + 1;
                    dp[to] = true;
                    ++cntLoosing[to];
                    only_loosing[to] += now;
                }
                total[to] += now;
                dfs2(to, v);

                total[v] += gg;

                if (!zz) {
                    cntLoosing[v] += 1;
                    only_loosing[v] += gg;
                }

            }
        }

    };

    dfs1(0, -1);

    dfs2(0, -1);

    int E = 0, L = 0;

    for (int i = 0; i < n; ++i) {
        if (dp[i]) {
            E = add(E, c[i]);
        } else {
            L += 1;
            E = sub(E, c[i]);
        }
    }

    int a = mul(n, n);

    int x = mul(L, mul(sub(binp(a, d), binp(E, d)), inv(sub(a, E))));

    int ans;

    if (dp[0]) {
        ans = mul(x, c[0]);
    } else {
        ans = sub(binp(a, d), mul(x, c[0]));
    }

    cout << sub(binp(a, d), ans);
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2672 KB Output is correct
2 Correct 2 ms 2672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2668 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2676 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2672 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2672 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2672 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2672 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 3 ms 2772 KB Output is correct
8 Correct 2 ms 2900 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 3 ms 2676 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2672 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2672 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 3 ms 2772 KB Output is correct
8 Correct 2 ms 2900 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 3 ms 2676 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 73 ms 13516 KB Output is correct
13 Correct 74 ms 19384 KB Output is correct
14 Correct 52 ms 8516 KB Output is correct
15 Correct 64 ms 8516 KB Output is correct
16 Correct 53 ms 8524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2672 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2672 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 3 ms 2772 KB Output is correct
8 Correct 2 ms 2900 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 3 ms 2676 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 2 ms 2668 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 2 ms 2676 KB Output is correct
22 Correct 2 ms 2772 KB Output is correct
23 Correct 2 ms 2644 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 2 ms 2796 KB Output is correct
28 Correct 2 ms 2644 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2672 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2672 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 3 ms 2772 KB Output is correct
8 Correct 2 ms 2900 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 3 ms 2676 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 73 ms 13516 KB Output is correct
13 Correct 74 ms 19384 KB Output is correct
14 Correct 52 ms 8516 KB Output is correct
15 Correct 64 ms 8516 KB Output is correct
16 Correct 53 ms 8524 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 2 ms 2644 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 2 ms 2644 KB Output is correct
24 Correct 2 ms 2668 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 2 ms 2676 KB Output is correct
27 Correct 2 ms 2772 KB Output is correct
28 Correct 2 ms 2644 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 2 ms 2644 KB Output is correct
31 Correct 2 ms 2644 KB Output is correct
32 Correct 2 ms 2796 KB Output is correct
33 Correct 2 ms 2644 KB Output is correct
34 Correct 2 ms 2644 KB Output is correct
35 Correct 2 ms 2644 KB Output is correct
36 Correct 71 ms 13572 KB Output is correct
37 Correct 81 ms 19484 KB Output is correct
38 Correct 59 ms 8520 KB Output is correct
39 Correct 69 ms 8588 KB Output is correct
40 Correct 65 ms 8584 KB Output is correct
41 Correct 68 ms 16608 KB Output is correct
42 Correct 61 ms 17900 KB Output is correct
43 Correct 42 ms 7792 KB Output is correct
44 Correct 51 ms 8496 KB Output is correct
45 Correct 50 ms 8460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2672 KB Output is correct
2 Correct 2 ms 2672 KB Output is correct
3 Correct 1 ms 2668 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2676 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2672 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2672 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 3 ms 2772 KB Output is correct
15 Correct 2 ms 2900 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 3 ms 2676 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 73 ms 13516 KB Output is correct
20 Correct 74 ms 19384 KB Output is correct
21 Correct 52 ms 8516 KB Output is correct
22 Correct 64 ms 8516 KB Output is correct
23 Correct 53 ms 8524 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 2 ms 2644 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 2 ms 2644 KB Output is correct
31 Correct 2 ms 2668 KB Output is correct
32 Correct 2 ms 2644 KB Output is correct
33 Correct 2 ms 2676 KB Output is correct
34 Correct 2 ms 2772 KB Output is correct
35 Correct 2 ms 2644 KB Output is correct
36 Correct 2 ms 2644 KB Output is correct
37 Correct 2 ms 2644 KB Output is correct
38 Correct 2 ms 2644 KB Output is correct
39 Correct 2 ms 2796 KB Output is correct
40 Correct 2 ms 2644 KB Output is correct
41 Correct 2 ms 2644 KB Output is correct
42 Correct 2 ms 2644 KB Output is correct
43 Correct 71 ms 13572 KB Output is correct
44 Correct 81 ms 19484 KB Output is correct
45 Correct 59 ms 8520 KB Output is correct
46 Correct 69 ms 8588 KB Output is correct
47 Correct 65 ms 8584 KB Output is correct
48 Correct 68 ms 16608 KB Output is correct
49 Correct 61 ms 17900 KB Output is correct
50 Correct 42 ms 7792 KB Output is correct
51 Correct 51 ms 8496 KB Output is correct
52 Correct 50 ms 8460 KB Output is correct
53 Correct 69 ms 19448 KB Output is correct
54 Correct 68 ms 17248 KB Output is correct
55 Correct 36 ms 7296 KB Output is correct
56 Correct 93 ms 13560 KB Output is correct
57 Correct 57 ms 8640 KB Output is correct
58 Correct 55 ms 8512 KB Output is correct
59 Correct 52 ms 8588 KB Output is correct
60 Correct 60 ms 8520 KB Output is correct