Submission #667670

#TimeUsernameProblemLanguageResultExecution timeMemory
667670mychecksedadStar Trek (CEOI20_startrek)C++17
7 / 100
16 ms24004 KiB
/* Author : Mychecksdead */ #include<bits/stdc++.h> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; typedef long long int ll; typedef long double ld; #define MOD (1000000000+7) #define MOD1 (998244353) #define PI 3.1415926535 #define pb push_back #define setp() cout << setprecision(15) #define all(x) x.begin(), x.end() #define debug(x) cerr << #x << " is " << x << '\n'; const int N = 1e6+100, M = 1e5+10, F = 2147483646, K = 20; ll po(ll a, ll b){ ll res = 1; while(b){ if(b&1) (res *= a) %= MOD; (a *= a) %= MOD; b >>= 1; } return res; } ll d, dp[N]; // subtree and parent vector<int> g[N]; int n, X = 0, par[N], up[N][K], mx[N][K], sz[N], is[N], dep[N]; void pre(int v, int p){ dp[v] = 0; par[v] = p; up[v][0] = par[p]; dep[v] = dep[p] + 1; for(int u: g[v]){ if(u != p){ pre(u, v); dp[v] |= !dp[u]; } } } void ppre(int v, int p){ is[v] = dp[v]; if(p != 0){ is[v] |= !is[p]; } for(int u: g[v]){ if(u != p){ ppre(u, v); } } } void solve(){ cin >> n >> d; for(int i = 0 ; i < n - 1; ++i){ int u, v; cin >> u >> v; g[u].pb(v); g[v].pb(u); } if(n == 2){ cout << po(4, d); return; } par[0] = 0; dp[0] = is[0] = 0; dep[0] = -1; pre(1, 0); ppre(1, 0); for(int i = 1; i <= n; ++i){ mx[i][0] = max(n * is[i], is[par[par[i]]] * n); } for(int j = 1; j < K; ++j) for(int i = 1; i <= n; ++i){ mx[i][j] = max(mx[i][j - 1], mx[up[i][j - 1]][j - 1]); up[i][j] = up[up[i][j - 1]][j - 1]; } for(int i = 1; i <= n; ++i) X += 1 - is[i]; // dfs(1, 0); ll ans = max(X, is[1] * n); for(int i = 2; i <= n; ++i){ int m = 0, v = i; if(dep[i] % 2) v = par[i]; for(int j = K - 1; j >= 0; --j){ if(dep[v] & (1<<j)){ m = max(m, mx[v][j]); v = up[v][j]; } } if(!(dep[i] % 2 && is[i])) if(!is[i]) m = max(X, m); else{ m = max(n - X, m); } // cout << m << ' '; (ans += m) %= MOD; } cout << ans; } int main(){ cin.tie(0); ios::sync_with_stdio(0); int T = 1, aa; // cin >> T;aa=T; while(T--){ // cout << "Case #" << aa-T << ": "; solve(); cout << '\n'; } return 0; }

Compilation message (stderr)

startrek.cpp: In function 'void solve()':
startrek.cpp:98:11: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
   98 |         if(!(dep[i] % 2 && is[i]))
      |           ^
startrek.cpp: In function 'int main()':
startrek.cpp:116:16: warning: unused variable 'aa' [-Wunused-variable]
  116 |     int T = 1, aa;
      |                ^~
#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...