Submission #336906

# Submission time Handle Problem Language Result Execution time Memory
336906 2020-12-17T08:38:00 Z dolphingarlic Star Trek (CEOI20_startrek) C++14
100 / 100
90 ms 16448 KB
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

const ll MOD = 1e9 + 7;

vector<int> graph[100001];
ll win_as_root[100001], critical[100001];

ll expo(ll base, ll pow) {
    ll ans = 1;
    while (pow) {
        if (pow & 1) ans = ans * base % MOD;
        pow >>= 1, base = base * base % MOD;
    }
    return ans;
}

void dfs1(int node = 1, int parent = 0) {
    for (int i : graph[node]) if (i != parent) {
        dfs1(i, node);
        win_as_root[node] += (win_as_root[i] == 0);
    }
    if (!win_as_root[node]) critical[node] = 1;
    for (int i : graph[node]) if (i != parent) {
        if ((win_as_root[node] == 1 && !win_as_root[i]) || !win_as_root[node])
            critical[node] += critical[i];
    }
}

void dfs2(int node = 1, int parent = 0, bool par_win = true, ll par_crit = 0) {
    win_as_root[node] += !par_win;
    ll all_crit = 0, lose_crit = 0;

    critical[node] = (win_as_root[node] == 0);
    all_crit += par_crit;
    if (!par_win) lose_crit += par_crit;
    if ((win_as_root[node] == 1 && !par_win) || !win_as_root[node])
        critical[node] += par_crit;
    for (int i : graph[node]) if (i != parent) {
        all_crit += critical[i];
        if (!win_as_root[i]) lose_crit += critical[i];
        if ((win_as_root[node] == 1 && !win_as_root[i]) || !win_as_root[node])
            critical[node] += critical[i];
    }

    for (int i : graph[node]) if (i != parent) {
        if (win_as_root[node] - (win_as_root[i] == 0) == 0)
            dfs2(i, node, false, all_crit - critical[i] + 1);
        else if (win_as_root[node] - (win_as_root[i] == 0) == 1)
            dfs2(i, node, true, lose_crit - (win_as_root[i] ? 0 : critical[i]));
        else
            dfs2(i, node, true, 0);
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ll n, d;
    cin >> n >> d;
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        graph[u].push_back(v);
        graph[v].push_back(u);
    }
    dfs1();
    dfs2();

    ll lose_cnt = 0, e_val = 0;
    for (int i = 1; i <= n; i++) {
        if (win_as_root[i]) e_val += critical[i];
        else lose_cnt++, e_val -= critical[i];
    }
    e_val = (e_val % MOD + MOD) % MOD;

    ll num = (expo(n, 2 * d) - expo(e_val, d) + MOD) % MOD;
    ll denom = expo((n * n - e_val + MOD) % MOD, MOD - 2);
    ll dp = lose_cnt * num % MOD * denom % MOD;
    if (win_as_root[1])
        cout << (expo(n, 2 * d) - critical[1] * dp % MOD + MOD) % MOD;
    else
        cout << critical[1] * dp % MOD;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2700 KB Output is correct
2 Correct 2 ms 2816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2688 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 2 ms 2688 KB Output is correct
5 Correct 2 ms 2668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 2 ms 2668 KB Output is correct
5 Correct 2 ms 2668 KB Output is correct
6 Correct 2 ms 2668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 2 ms 2668 KB Output is correct
5 Correct 2 ms 2668 KB Output is correct
6 Correct 2 ms 2668 KB Output is correct
7 Correct 2 ms 2796 KB Output is correct
8 Correct 2 ms 2796 KB Output is correct
9 Correct 3 ms 2796 KB Output is correct
10 Correct 3 ms 2796 KB Output is correct
11 Correct 2 ms 2796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 2 ms 2668 KB Output is correct
5 Correct 2 ms 2668 KB Output is correct
6 Correct 2 ms 2668 KB Output is correct
7 Correct 2 ms 2796 KB Output is correct
8 Correct 2 ms 2796 KB Output is correct
9 Correct 3 ms 2796 KB Output is correct
10 Correct 3 ms 2796 KB Output is correct
11 Correct 2 ms 2796 KB Output is correct
12 Correct 88 ms 10988 KB Output is correct
13 Correct 87 ms 15168 KB Output is correct
14 Correct 65 ms 7528 KB Output is correct
15 Correct 72 ms 7404 KB Output is correct
16 Correct 78 ms 7532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 2 ms 2668 KB Output is correct
5 Correct 2 ms 2668 KB Output is correct
6 Correct 2 ms 2668 KB Output is correct
7 Correct 2 ms 2796 KB Output is correct
8 Correct 2 ms 2796 KB Output is correct
9 Correct 3 ms 2796 KB Output is correct
10 Correct 3 ms 2796 KB Output is correct
11 Correct 2 ms 2796 KB Output is correct
12 Correct 2 ms 2668 KB Output is correct
13 Correct 2 ms 2796 KB Output is correct
14 Correct 2 ms 2668 KB Output is correct
15 Correct 2 ms 2688 KB Output is correct
16 Correct 2 ms 2668 KB Output is correct
17 Correct 2 ms 2668 KB Output is correct
18 Correct 2 ms 2668 KB Output is correct
19 Correct 2 ms 2668 KB Output is correct
20 Correct 2 ms 2668 KB Output is correct
21 Correct 2 ms 2796 KB Output is correct
22 Correct 2 ms 2796 KB Output is correct
23 Correct 2 ms 2796 KB Output is correct
24 Correct 2 ms 2796 KB Output is correct
25 Correct 3 ms 2796 KB Output is correct
26 Correct 2 ms 2796 KB Output is correct
27 Correct 2 ms 2796 KB Output is correct
28 Correct 2 ms 2668 KB Output is correct
29 Correct 3 ms 2796 KB Output is correct
30 Correct 2 ms 2796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 2 ms 2668 KB Output is correct
5 Correct 2 ms 2668 KB Output is correct
6 Correct 2 ms 2668 KB Output is correct
7 Correct 2 ms 2796 KB Output is correct
8 Correct 2 ms 2796 KB Output is correct
9 Correct 3 ms 2796 KB Output is correct
10 Correct 3 ms 2796 KB Output is correct
11 Correct 2 ms 2796 KB Output is correct
12 Correct 88 ms 10988 KB Output is correct
13 Correct 87 ms 15168 KB Output is correct
14 Correct 65 ms 7528 KB Output is correct
15 Correct 72 ms 7404 KB Output is correct
16 Correct 78 ms 7532 KB Output is correct
17 Correct 2 ms 2668 KB Output is correct
18 Correct 2 ms 2796 KB Output is correct
19 Correct 2 ms 2668 KB Output is correct
20 Correct 2 ms 2688 KB Output is correct
21 Correct 2 ms 2668 KB Output is correct
22 Correct 2 ms 2668 KB Output is correct
23 Correct 2 ms 2668 KB Output is correct
24 Correct 2 ms 2668 KB Output is correct
25 Correct 2 ms 2668 KB Output is correct
26 Correct 2 ms 2796 KB Output is correct
27 Correct 2 ms 2796 KB Output is correct
28 Correct 2 ms 2796 KB Output is correct
29 Correct 2 ms 2796 KB Output is correct
30 Correct 3 ms 2796 KB Output is correct
31 Correct 2 ms 2796 KB Output is correct
32 Correct 2 ms 2796 KB Output is correct
33 Correct 2 ms 2668 KB Output is correct
34 Correct 3 ms 2796 KB Output is correct
35 Correct 2 ms 2796 KB Output is correct
36 Correct 77 ms 10988 KB Output is correct
37 Correct 79 ms 15212 KB Output is correct
38 Correct 70 ms 7528 KB Output is correct
39 Correct 89 ms 7404 KB Output is correct
40 Correct 90 ms 7404 KB Output is correct
41 Correct 81 ms 13164 KB Output is correct
42 Correct 73 ms 14060 KB Output is correct
43 Correct 51 ms 7016 KB Output is correct
44 Correct 74 ms 7404 KB Output is correct
45 Correct 75 ms 7424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2700 KB Output is correct
2 Correct 2 ms 2816 KB Output is correct
3 Correct 2 ms 2688 KB Output is correct
4 Correct 2 ms 2668 KB Output is correct
5 Correct 2 ms 2668 KB Output is correct
6 Correct 2 ms 2688 KB Output is correct
7 Correct 2 ms 2668 KB Output is correct
8 Correct 2 ms 2668 KB Output is correct
9 Correct 2 ms 2668 KB Output is correct
10 Correct 2 ms 2668 KB Output is correct
11 Correct 2 ms 2668 KB Output is correct
12 Correct 2 ms 2668 KB Output is correct
13 Correct 2 ms 2668 KB Output is correct
14 Correct 2 ms 2796 KB Output is correct
15 Correct 2 ms 2796 KB Output is correct
16 Correct 3 ms 2796 KB Output is correct
17 Correct 3 ms 2796 KB Output is correct
18 Correct 2 ms 2796 KB Output is correct
19 Correct 88 ms 10988 KB Output is correct
20 Correct 87 ms 15168 KB Output is correct
21 Correct 65 ms 7528 KB Output is correct
22 Correct 72 ms 7404 KB Output is correct
23 Correct 78 ms 7532 KB Output is correct
24 Correct 2 ms 2668 KB Output is correct
25 Correct 2 ms 2796 KB Output is correct
26 Correct 2 ms 2668 KB Output is correct
27 Correct 2 ms 2688 KB Output is correct
28 Correct 2 ms 2668 KB Output is correct
29 Correct 2 ms 2668 KB Output is correct
30 Correct 2 ms 2668 KB Output is correct
31 Correct 2 ms 2668 KB Output is correct
32 Correct 2 ms 2668 KB Output is correct
33 Correct 2 ms 2796 KB Output is correct
34 Correct 2 ms 2796 KB Output is correct
35 Correct 2 ms 2796 KB Output is correct
36 Correct 2 ms 2796 KB Output is correct
37 Correct 3 ms 2796 KB Output is correct
38 Correct 2 ms 2796 KB Output is correct
39 Correct 2 ms 2796 KB Output is correct
40 Correct 2 ms 2668 KB Output is correct
41 Correct 3 ms 2796 KB Output is correct
42 Correct 2 ms 2796 KB Output is correct
43 Correct 77 ms 10988 KB Output is correct
44 Correct 79 ms 15212 KB Output is correct
45 Correct 70 ms 7528 KB Output is correct
46 Correct 89 ms 7404 KB Output is correct
47 Correct 90 ms 7404 KB Output is correct
48 Correct 81 ms 13164 KB Output is correct
49 Correct 73 ms 14060 KB Output is correct
50 Correct 51 ms 7016 KB Output is correct
51 Correct 74 ms 7404 KB Output is correct
52 Correct 75 ms 7424 KB Output is correct
53 Correct 84 ms 16448 KB Output is correct
54 Correct 87 ms 14828 KB Output is correct
55 Correct 45 ms 7400 KB Output is correct
56 Correct 79 ms 12268 KB Output is correct
57 Correct 72 ms 8684 KB Output is correct
58 Correct 72 ms 8684 KB Output is correct
59 Correct 79 ms 8556 KB Output is correct
60 Correct 75 ms 8556 KB Output is correct