Submission #788628

# Submission time Handle Problem Language Result Execution time Memory
788628 2023-07-20T12:20:44 Z WLZ Star Trek (CEOI20_startrek) C++17
100 / 100
65 ms 16360 KB
#include <bits/stdc++.h>
using namespace std;
 
const long long MOD = (long long) 1e9 + 7;

int n;
long long d;
vector< vector<int> > g;
vector<bool> lose;
vector<int> lose_child, crit_win, crit_lose, crit;
long long l, e;

long long modpow(long long b, long long p) {
  if (p == 0) {
    return 1;
  }
  long long ans = modpow((b * b) % MOD, p / 2);
  if (p % 2 == 1) {
    ans = (ans * b) % MOD;
  }
  return ans;
}

long long inv(long long x) {
  return modpow(x, MOD - 2);
}

long long mul(long long x, long long y) {
  return (x * y) % MOD;
}

void dfs(int u, int p) {
  lose[u] = true;
  for (auto& v : g[u]) {
    if (v == p) {
      continue;
    }
    dfs(v, u);
    if (lose[v]) {
      lose[u] = false;
      lose_child[u]++;
    }
  }
}

void calc(int u) {
  if (lose[u]) {
    crit[u] = crit_win[u] + 1;
  } else {
    if (lose_child[u] == 1) {
      crit[u] = crit_lose[u];
    } else {
      crit[u] = 0;
    }
  }
}

void dfs2(int u, int p) {
  for (auto& v : g[u]) {
    if (v == p) {
      continue;
    }
    dfs2(v, u);
    if (lose[v]) {
      crit_lose[u] += crit[v];
    } else {
      crit_win[u] += crit[v];
    }
  }
  calc(u);
}

void reroot(int from, int to) {
  if (lose[to]) {
    lose_child[from]--;
    crit_lose[from] -= crit[to];
  } else {
    crit_win[from] -= crit[to];
  }
  if (lose_child[from] == 0) {
    lose[from] = true;
  }
  calc(from);
  if (lose[from]) {
    lose_child[to]++;
    crit_lose[to] += crit[from];
  } else {
    crit_win[to] += crit[from];
  }
  if (lose_child[to] > 0) {
    lose[to] = false;
  }
  calc(to);
}

void dfs3(int u, int p) {
  if (lose[u]) {
    l = (l + 1) % MOD;
    e = ((e - crit[u]) % MOD + MOD) % MOD;
  } else {
    e = (e + crit[u]) % MOD;
  }
  for (auto& v : g[u]) {
    if (v != p) {
      reroot(u, v);
      dfs3(v, u);
      reroot(v, u);
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> d;
  g.resize(n + 1);
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  lose.resize(n + 1);
  lose_child.assign(n + 1, 0);
  crit_win.resize(n + 1);
  crit_lose.resize(n + 1);
  crit.resize(n + 1);
  dfs(1, -1);
  dfs2(1, -1);
  dfs3(1, -1);
  long long ans = mul(l, mul(((modpow(n, 2 * d) - modpow(e, d)) % MOD + MOD) % MOD, inv(((modpow(n, 2) - e) % MOD + MOD) % MOD)));
  //cout << l << endl;
  //cout << ((modpow(n, 2 * d) - modpow(e, d)) % MOD + MOD) % MOD << endl;
  //cout << ((modpow(n, 2) - e) % MOD + MOD) % MOD << endl;
  ans = mul(ans, crit[1]);
  if (!lose[1]) {
    ans = ((modpow(n, 2 * d) - ans) % MOD + MOD) % MOD;
  } 
  cout << ans << '\n';
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory 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 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
# Verdict Execution time Memory 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 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 316 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
# Verdict Execution time Memory 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 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 316 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 40 ms 12044 KB Output is correct
13 Correct 44 ms 16356 KB Output is correct
14 Correct 31 ms 8464 KB Output is correct
15 Correct 38 ms 8536 KB Output is correct
16 Correct 37 ms 8556 KB Output is correct
# Verdict Execution time Memory 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 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 316 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 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 452 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory 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 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 316 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 40 ms 12044 KB Output is correct
13 Correct 44 ms 16356 KB Output is correct
14 Correct 31 ms 8464 KB Output is correct
15 Correct 38 ms 8536 KB Output is correct
16 Correct 37 ms 8556 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 452 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 41 ms 12136 KB Output is correct
37 Correct 65 ms 16360 KB Output is correct
38 Correct 28 ms 8432 KB Output is correct
39 Correct 45 ms 8608 KB Output is correct
40 Correct 39 ms 8504 KB Output is correct
41 Correct 42 ms 14220 KB Output is correct
42 Correct 40 ms 14944 KB Output is correct
43 Correct 25 ms 7620 KB Output is correct
44 Correct 36 ms 8556 KB Output is correct
45 Correct 35 ms 8584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 320 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 316 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 40 ms 12044 KB Output is correct
20 Correct 44 ms 16356 KB Output is correct
21 Correct 31 ms 8464 KB Output is correct
22 Correct 38 ms 8536 KB Output is correct
23 Correct 37 ms 8556 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 468 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 452 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 41 ms 12136 KB Output is correct
44 Correct 65 ms 16360 KB Output is correct
45 Correct 28 ms 8432 KB Output is correct
46 Correct 45 ms 8608 KB Output is correct
47 Correct 39 ms 8504 KB Output is correct
48 Correct 42 ms 14220 KB Output is correct
49 Correct 40 ms 14944 KB Output is correct
50 Correct 25 ms 7620 KB Output is correct
51 Correct 36 ms 8556 KB Output is correct
52 Correct 35 ms 8584 KB Output is correct
53 Correct 43 ms 16324 KB Output is correct
54 Correct 44 ms 14728 KB Output is correct
55 Correct 22 ms 6864 KB Output is correct
56 Correct 38 ms 12108 KB Output is correct
57 Correct 36 ms 8520 KB Output is correct
58 Correct 35 ms 8552 KB Output is correct
59 Correct 35 ms 8504 KB Output is correct
60 Correct 35 ms 8520 KB Output is correct