Submission #1151337

#TimeUsernameProblemLanguageResultExecution timeMemory
1151337vladiliusStar Trek (CEOI20_startrek)C++20
45 / 100
101 ms33864 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; #define pb push_back #define ff first #define ss second const int mod = 1e9 + 7; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; ll d; cin>>n>>d; vector<int> g[n + 1]; for (int i = 1; i < n; i++){ int u, v; cin>>u>>v; g[u].pb(v); g[v].pb(u); } if (n == 2){ int out = 4, pr = 4; d--; while (d > 0){ if (d & 1){ out = (1LL * out * pr) % mod; } pr = (1LL * pr * pr) % mod; d >>= 1; } cout<<out<<"\n"; return 0; } vector<int> h(n + 1), p(n + 1); function<void(int, int)> dfs = [&](int v, int pr){ h[v] = 1; p[v] = pr; for (int i: g[v]){ if (i == pr) continue; dfs(i, v); h[v] = min(h[v], h[i]); } h[v] = !h[v]; }; dfs(1, 0); vector<vector<bool>> dp(n + 1, vector<bool>(2)); function<void(int)> fill = [&](int v){ int s = 0, x = 0; for (int i: g[v]){ if (i == p[v]) continue; if (!h[i]){ s++; x = i; } } for (int i: g[v]){ if (i == p[v]) continue; dp[i][0] = dp[v][1]; if (!s || (s == 1 && i == x)){ dp[i][1] = dp[v][0]; } else { dp[i][1] = dp[v][1]; } fill(i); } }; dp[1][0] = 0; dp[1][1] = 1; fill(1); vector<int> c1(n + 1), c0(n + 1); int cc1 = 0, cc0 = 0; for (int i = 1; i <= n; i++){ if (dp[i][1]) cc1++; else cc0++; } vector<int> f(n + 1), t = h; function<void(int)> rr = [&](int v){ c1[v] = cc1; c0[v] = cc0; f[v] = t[v]; int s = 0, x = 0; for (int i: g[v]){ if (!t[i]){ s++; x = i; } } for (int i: g[v]){ if (i == p[v]) continue; int i0 = dp[i][0], i1 = dp[i][1], C1 = cc1, C0 = cc0; if (i1) cc1--; else cc0--; dp[i][0] = 0; dp[i][1] = 1; cc1++; int S = 0, X = 0; for (int j: g[i]){ if (!h[j]){ S++; X = j; } } int v0 = dp[v][0], v1 = dp[v][1]; if (v1) cc1--; else cc0--; dp[v][0] = dp[i][1]; if (!S || (S == 1 && v == X)){ dp[v][1] = dp[i][0]; } else { dp[v][1] = dp[i][1]; } if (dp[v][1]) cc1++; else cc0++; int tv = t[v], ti = t[i]; if (s == 1 && i == x){ t[v] = 0; } t[i] = 1; for (int j: g[i]){ t[i] = min(t[i], t[j]); } t[i] = !t[i]; rr(i); t[v] = tv; t[i] = ti; dp[i][0] = i0; dp[i][1] = i1; dp[v][0] = v0; dp[v][1] = v1; cc0 = C0; cc1 = C1; } }; rr(1); int S0 = 0, S1 = 0; for (int i = 1; i <= n; i++){ S0 = (S0 + c0[i]) % mod; S1 = (S1 + c1[i]) % mod; } int h0 = 0, h1 = 0, s0 = 0, s1 = 0; for (int i = 1; i <= n; i++){ if (f[i]){ h1++; s1++; } else { h0++; s0++; } } int out = f[1]; for (int i = 1; i <= d; i++){ out = (1LL * c1[1] * s0) % mod; if (f[1]) out = (out + 1LL * s1 * n) % mod; int x = s0, y = s1; s0 = (1LL * x * S0 + 1LL * y * n * h0) % mod; s1 = (1LL * x * S1 + 1LL * y * n * h1) % mod; } cout<<out<<"\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...