Submission #658632

# Submission time Handle Problem Language Result Execution time Memory
658632 2022-11-13T22:24:37 Z lunchbox1 Star Trek (CEOI20_startrek) C++17
100 / 100
187 ms 23548 KB
/*
 Star Trek
 https://oj.uz/problem/view/CEOI20_startrek
*/
#include <bits/stdc++.h>
using namespace std;

const int N = 1e5, P = 1e9 + 7;

struct dat {
 int a, b, c;
 bool g;

 dat(bool d) {
  a = b = c = -1, g = d;
 }
 void add(int i) {
  if (a == -1)
   a = i;
  else if (b == -1)
   b = i;
  else if (c == -1)
   c = i;
 }
 bool good(int i) {
  return (a != -1 && a != i) || (b != -1 && b != i) || (c != -1 && c != i) || g;
 }
};

struct mat {
 array<array<int, 2>, 2> v;
 
 mat(bool i = 0) {
  v[0][0] = v[0][1] = v[1][0] = v[1][1] = 0;
  if (i)
   v[0][0] = v[1][1] = 1;
 }
 mat operator*(mat o) {
  mat r;
  for (int a = 0; a < 2; a++)
   for (int b = 0; b < 2; b++)
    for (int c = 0; c < 2; c++)
     r.v[a][c] = (r.v[a][c] + 1LL * v[a][b] * o.v[b][c]) % P;
  return r;
 }
 void print() {
  cout << v[0][0] << ' ' << v[0][1] << ' ' << v[1][0] << ' ' << v[1][1] << '\n';
 }
};

vector<int> g[N];
array<int, 2> c[N];
bool w0[N], w1[N];

void dfs1(int i) {
 for (int j : g[i]) {
  g[j].erase(find(g[j].begin(), g[j].end(), i));
  dfs1(j), w0[i] |= !w0[j];
 }
}

void dfs2(int i, bool u) {
 w1[i] = !u;
 dat t(!u);
 for (int j : g[i])
  if (!w0[j])
   w1[i] = 1, t.add(j);
 for (int j : g[i])
  dfs2(j, t.good(j));
}

bool tmp;

void dfs3(int i) {
 dat t(0);
 for (int j : g[i])
  if (!w0[j])
   t.add(j);
 c[i][0] = c[i][1] = 0;
 for (int j : g[i]) {
  dfs3(j);
  (c[i][t.good(j) | !0] += c[j][0]) %= P;
  (c[i][t.good(j) | !1] += c[j][1]) %= P;
 }
 if (!tmp)
  c[i][w0[i] | !0]++;
 else
  c[i][w0[i] | !1]++;
}

mat m;

void dfs4(int i, bool u, array<int, 2> d) {
 dat t(0);
 if (!u)
  t.add(i);
 for (int j : g[i])
  if (!w0[j])
   t.add(j);
 int s0 = 0, s1 = 0;
 array<int, 2> q = {0, 0};
 q[t.good(i) || !0] += d[0], s0 += d[0];
 q[t.good(i) || !1] += d[1], s1 += d[1];
 for (int j : g[i]) {
  q[t.good(j) || !0] += c[j][0], s0 += c[j][0];
  q[t.good(j) || !1] += c[j][1], s1 += c[j][1];
 }
 swap(d, c[i]);
 for (int j : g[i]) {
  s0 -= c[j][0], s1 -= c[j][1];
  array<int, 2> e = {0, 0};
  e[t.good(j) | !tmp]++;
  e[1] += s0;
  if (t.c != -1)
   e[1] += s1;
  else if (t.b != -1) {
   if (j != t.a && j != t.b)
    e[1] += s1;
   else
    e[1] += s1 - c[t.a ^ t.b ^ j][1], e[0] += c[t.a ^ t.b ^ j][1];
  } else if (t.a != -1) {
   if (t.a == j)
    e[0] += s1;
   else
    e[1] += s1 - c[t.a][1], e[0] += c[t.a][1];
  } else
   e[0] += s1;
  dfs4(j, t.good(j), e);
  s0 += c[j][0], s1 += c[j][1];
 }
 swap(d, c[i]);
 q[t.good(-1) | !tmp]++;
 m.v[tmp][0] = (m.v[tmp][0] + q[0]) % P, m.v[tmp][1] = (m.v[tmp][1] + q[1]) % P;
}

int x, y;

array<int, 2> dfs5(int i) {
 dat t(0);
 for (int j : g[i])
  if (!w0[j])
   t.add(j);
 array<int, 2> c = {0, 0};
 for (int j : g[i]) {
  auto [c0, c1] = dfs5(j);
  (c[t.good(j) || !0] += c0) %= P;
  (c[t.good(j) || !1] += c1) %= P;
 }
 (c[w0[i] | !0] += x) %= P;
 (c[w0[i] | !1] += y) %= P;
 return c;
}

int main() {
 ios::sync_with_stdio(0), cin.tie(0);
 int n;
 long long k;
 cin >> n >> k;
 for (int h = 0; h < n - 1; h++) {
  int i, j;
  cin >> i >> j, i--, j--;
  g[i].push_back(j), g[j].push_back(i);
 }
 dfs1(0);
 dfs2(0, 1);
 tmp = 0;
 dfs3(0);
 dfs4(0, 1, {0, 0});
 tmp = 1;
 dfs3(0);
 dfs4(0, 1, {0, 0});
 mat p(1);
 k--;
 while (k > 0) {
  if (k & 1)
   p = p * m;
  m = m * m, k >>= 1;
 }
 int c = 0;
 for (int i = 0; i < n; i++)
  c += w1[i];
 x = (1LL * (n - c) * p.v[0][0] + 1LL * c * p.v[1][0]) % P;
 y = (1LL * (n - c) * p.v[0][1] + 1LL * c * p.v[1][1]) % P;
 cout << dfs5(0)[1] << '\n';
 return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 3 ms 2740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2680 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 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2676 KB Output is correct
3 Correct 3 ms 2644 KB Output is correct
4 Correct 5 ms 2644 KB Output is correct
5 Correct 3 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 2644 KB Output is correct
2 Correct 2 ms 2676 KB Output is correct
3 Correct 3 ms 2644 KB Output is correct
4 Correct 5 ms 2644 KB Output is correct
5 Correct 3 ms 2672 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2772 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2700 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2676 KB Output is correct
3 Correct 3 ms 2644 KB Output is correct
4 Correct 5 ms 2644 KB Output is correct
5 Correct 3 ms 2672 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2772 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2700 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 160 ms 15112 KB Output is correct
13 Correct 150 ms 23536 KB Output is correct
14 Correct 93 ms 7880 KB Output is correct
15 Correct 95 ms 7968 KB Output is correct
16 Correct 100 ms 7868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2676 KB Output is correct
3 Correct 3 ms 2644 KB Output is correct
4 Correct 5 ms 2644 KB Output is correct
5 Correct 3 ms 2672 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2772 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2700 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2676 KB Output is correct
13 Correct 4 ms 2644 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 3 ms 2644 KB Output is correct
16 Correct 3 ms 2644 KB Output is correct
17 Correct 3 ms 2652 KB Output is correct
18 Correct 3 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 2772 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 3 ms 2644 KB Output is correct
26 Correct 2 ms 2680 KB Output is correct
27 Correct 3 ms 2772 KB Output is correct
28 Correct 2 ms 2644 KB Output is correct
29 Correct 2 ms 2680 KB Output is correct
30 Correct 3 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2676 KB Output is correct
3 Correct 3 ms 2644 KB Output is correct
4 Correct 5 ms 2644 KB Output is correct
5 Correct 3 ms 2672 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2772 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2700 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 160 ms 15112 KB Output is correct
13 Correct 150 ms 23536 KB Output is correct
14 Correct 93 ms 7880 KB Output is correct
15 Correct 95 ms 7968 KB Output is correct
16 Correct 100 ms 7868 KB Output is correct
17 Correct 2 ms 2676 KB Output is correct
18 Correct 4 ms 2644 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 3 ms 2644 KB Output is correct
21 Correct 3 ms 2644 KB Output is correct
22 Correct 3 ms 2652 KB Output is correct
23 Correct 3 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 2772 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 3 ms 2644 KB Output is correct
31 Correct 2 ms 2680 KB Output is correct
32 Correct 3 ms 2772 KB Output is correct
33 Correct 2 ms 2644 KB Output is correct
34 Correct 2 ms 2680 KB Output is correct
35 Correct 3 ms 2644 KB Output is correct
36 Correct 184 ms 15116 KB Output is correct
37 Correct 165 ms 23540 KB Output is correct
38 Correct 106 ms 7832 KB Output is correct
39 Correct 113 ms 7892 KB Output is correct
40 Correct 109 ms 7968 KB Output is correct
41 Correct 138 ms 19500 KB Output is correct
42 Correct 187 ms 21748 KB Output is correct
43 Correct 97 ms 7288 KB Output is correct
44 Correct 91 ms 7892 KB Output is correct
45 Correct 113 ms 7980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 3 ms 2740 KB Output is correct
3 Correct 1 ms 2680 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2676 KB Output is correct
10 Correct 3 ms 2644 KB Output is correct
11 Correct 5 ms 2644 KB Output is correct
12 Correct 3 ms 2672 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 2 ms 2772 KB Output is correct
15 Correct 2 ms 2772 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 2 ms 2700 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 160 ms 15112 KB Output is correct
20 Correct 150 ms 23536 KB Output is correct
21 Correct 93 ms 7880 KB Output is correct
22 Correct 95 ms 7968 KB Output is correct
23 Correct 100 ms 7868 KB Output is correct
24 Correct 2 ms 2676 KB Output is correct
25 Correct 4 ms 2644 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 3 ms 2644 KB Output is correct
28 Correct 3 ms 2644 KB Output is correct
29 Correct 3 ms 2652 KB Output is correct
30 Correct 3 ms 2644 KB Output is correct
31 Correct 2 ms 2644 KB Output is correct
32 Correct 2 ms 2644 KB Output is correct
33 Correct 2 ms 2772 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 3 ms 2644 KB Output is correct
38 Correct 2 ms 2680 KB Output is correct
39 Correct 3 ms 2772 KB Output is correct
40 Correct 2 ms 2644 KB Output is correct
41 Correct 2 ms 2680 KB Output is correct
42 Correct 3 ms 2644 KB Output is correct
43 Correct 184 ms 15116 KB Output is correct
44 Correct 165 ms 23540 KB Output is correct
45 Correct 106 ms 7832 KB Output is correct
46 Correct 113 ms 7892 KB Output is correct
47 Correct 109 ms 7968 KB Output is correct
48 Correct 138 ms 19500 KB Output is correct
49 Correct 187 ms 21748 KB Output is correct
50 Correct 97 ms 7288 KB Output is correct
51 Correct 91 ms 7892 KB Output is correct
52 Correct 113 ms 7980 KB Output is correct
53 Correct 141 ms 23548 KB Output is correct
54 Correct 153 ms 20360 KB Output is correct
55 Correct 46 ms 6788 KB Output is correct
56 Correct 115 ms 15096 KB Output is correct
57 Correct 91 ms 8088 KB Output is correct
58 Correct 85 ms 8000 KB Output is correct
59 Correct 104 ms 7972 KB Output is correct
60 Correct 100 ms 7916 KB Output is correct