제출 #1151312

#제출 시각아이디문제언어결과실행 시간메모리
1151312vladiliusStar Trek (CEOI20_startrek)C++20
43 / 100
1093 ms327680 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); } vector<vector<int>> p(n + 1, vector<int>(n + 1)), h(n + 1, vector<int>(n + 1)); function<void(int, int, int&)> fill = [&](int v, int pr, int& u){ p[u][v] = pr; h[u][v] = 1; for (int i: g[v]){ if (i == pr) continue; fill(i, v, u); h[u][v] = min(h[u][v], h[u][i]); } h[u][v] = !h[u][v]; }; for (int i = 1; i <= n; i++){ fill(i, 0, i); } vector<vector<vector<bool>>> f(n + 1, vector<vector<bool>>(n + 1, vector<bool>(2))); function<void(int, int&)> dfs = [&](int v, int& u){ int s = 0, x = 0; for (int i: g[v]){ if (i == p[u][v]) continue; if (!h[u][i]){ s++; x = i; } } for (int i: g[v]){ if (i == p[u][v]) continue; f[u][i][0] = f[u][v][1]; if (!s || (s == 1 && i == x)){ f[u][i][1] = f[u][v][0]; } else { f[u][i][1] = f[u][v][1]; } dfs(i, u); } }; for (int i = 1; i <= n; i++){ f[i][i][0] = 0; f[i][i][1] = 1; dfs(i, i); } vector<vector<int>> dp(n + 1, vector<int>(2)); vector<int> c1(n + 1), c0(n + 1); int S0 = 0, S1 = 0; for (int i = 1; i <= n; i++){ dp[i][h[i][i]] = 1; for (int j = 1; j <= n; j++){ if (f[i][j][1]){ c1[i]++; } else { c0[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 (h[i][i]) h1++; else h0++; s0 = (s0 + dp[i][0]) % mod; s1 = (s1 + dp[i][1]) % mod; } int out = dp[1][1]; for (int i = 1; i <= d; i++){ out = (1LL * c1[1] * s0) % mod; if (h[1][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...