Submission #1091240

#TimeUsernameProblemLanguageResultExecution timeMemory
1091240mispertionStar Trek (CEOI20_startrek)C++17
78 / 100
1064 ms65428 KiB
#include<bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); typedef long long ll; #define int ll typedef unsigned long long ull; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define pb push_back #define all(x) x.begin(), x.end() #define sz(x) (int)x.size() #define mispertion ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) #define F first #define S second #define getlast(s) (*s.rbegin()) #define debg cout << "OK\n" const ld PI = 3.1415926535; const int N = 1e6 + 6; const int M = 7e6 + 1; const int mod = 1e9+7; const int infi = INT_MAX; const ll infl = LLONG_MAX; const int P = 31; int mult(int a, int b) { return a * 1LL * b % mod; } int sum(int a, int b) { if (a + b < 0) return a + b + mod; if (a + b >= mod) return a + b - mod; return a + b; } ll binpow(ll a, ll n) { if (n == 0) return 1; if (n % 2 == 1) { return binpow(a, n - 1) * a % mod; } else { ll b = binpow(a, n / 2); return b * b % mod; } } int n, d, dp[N], ndp[N], cd[N], cu[N], C[N], st[N], ddp[N]; vector<int> g[N]; void dfs(int v, int p){ int cnt0 = 0; for(auto u : g[v]){ if(u == p) continue; dfs(u, v); if(dp[u] == 0) cnt0++; } dp[v] = (cnt0 != 0); if(dp[v] == 0){ cd[v] = 1; for(auto u : g[v]){ if(u == p) continue; cd[v] += cd[u]; } }else{ cd[v] = 0; if(cnt0 == 1){ for(auto u : g[v]){ if(u == p) continue; if(dp[u] == 0) cd[v] += cd[u]; } } } } void dfs1(int v, int p){ int cnt0 = (ndp[v] == 0), sm0 = 0, sm = cu[v]; if(ndp[v] == 0){ sm0 += cu[v]; } for(auto u : g[v]){ if(u == p) continue; cnt0 += (dp[u] == 0); sm += cd[u]; if(dp[u] == 0) sm0 += cd[u]; } for(auto u : g[v]){ if(u == p) continue; cnt0 -= (dp[u] == 0); sm -= cd[u]; if(dp[u] == 0) sm0 -= cd[u]; ndp[u] = (cnt0 != 0); if(ndp[u] == 0){ cu[u] = 1 + sm; }else{ if(cnt0 == 1) cu[u] = sm0; } dfs1(u, v); cnt0 += (dp[u] == 0); sm += cd[u]; if(dp[u] == 0) sm0 += cd[u]; } } void dfs2(int v, int p){ int cnt0 = (ndp[v] == 0); for(auto u : g[v]){ if(u == p) continue; cnt0 += (dp[u] == 0); } st[v] = (cnt0 != 0); if(st[v] == 0){ C[v] = 1 + cu[v]; for(auto u : g[v]){ if(u == p) continue; C[v] += cd[u]; } }else{ if(cnt0 == 1){ if(ndp[v] == 0) C[v] = cu[v]; for(auto u : g[v]){ if(u == p) continue; if(dp[u] == 0) C[v] += cd[u]; } } } for(auto u : g[v]) if(u != p) dfs2(u, v); } void solve(){ cin >> n >> d; for(int i = 2; i <= n; i++){ int u, v; cin >> u >> v; g[v].pb(u); g[u].pb(v); } dfs(1, 0); ndp[1] = 1; cu[1] = 0; dfs1(1, 0); dfs2(1, 0); int L = 0; for(int i = 1; i <= n; i++) L += (st[i] == 0); int E = 0; for(int i = 1; i <= n; i++){ if(st[i]) E += C[i]; else E -= C[i]; } ddp[0] = L; int n2 = mult(n, n); for(int i = 1; i < d; i++){ ddp[i] = sum(mult(ddp[i - 1], E), mult(L, binpow(n2, i))); } int ret; if(st[1]){ ret = sum(binpow(n2, d), -mult(C[1], ddp[d - 1])); }else{ ret = mult(C[1], ddp[d - 1]); } cout << ret << '\n'; } signed main() { mispertion; int t = 1; //cin >> t; while(t--){ solve(); } return 0; }
#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...