Submission #932431

#TimeUsernameProblemLanguageResultExecution timeMemory
932431LucaIlieStar Trek (CEOI20_startrek)C++17
38 / 100
103 ms17728 KiB
#include <bits/stdc++.h> #define int long long using namespace std; const int MAX_N = 1e5; const int MAX_D = 1e5; const int MOD = 1e9 + 7; bool canWin[MAX_N + 1]; int countChildrenLosers[MAX_N + 1], critical[MAX_N + 1], sumCriticalW[MAX_N + 1], sumCriticalL[MAX_N + 1], dp[MAX_D]; vector<int> adj[MAX_N + 1]; void dfsGame( int u, int p ) { canWin[u] = false; countChildrenLosers[u] = 0; for ( int v: adj[u] ) { if ( v == p ) continue; dfsGame( v, u ); canWin[u] |= !canWin[v]; countChildrenLosers[u] += !canWin[v]; } } void calcCritical( int u ) { if ( canWin[u] ) { if ( countChildrenLosers[u] >= 2 ) critical[u] = 0; else critical[u] = sumCriticalL[u]; } else critical[u] = sumCriticalW[u] + 1; } void dfsCritical( int u, int p ) { sumCriticalW[u] = sumCriticalL[u] = 0; for ( int v: adj[u] ) { if ( v == p ) continue; dfsCritical( v, u ); if ( canWin[v] ) sumCriticalW[u] += critical[v]; else sumCriticalL[u] += critical[v]; } calcCritical( u ); } void reroot( int u, int v ) { if ( canWin[v] ) sumCriticalW[u] -= critical[v]; else { sumCriticalL[u] -= critical[v]; countChildrenLosers[u]--; } canWin[u] = (countChildrenLosers[u] > 0); calcCritical( u ); if ( canWin[u] ) sumCriticalW[v] += critical[u]; else { sumCriticalL[v] += critical[u]; countChildrenLosers[v]++; } canWin[v] = (countChildrenLosers[v] > 0); calcCritical( v ); } int l = 0, c = 0; void dfs( int u, int p ) { l += !canWin[u]; c += (canWin[u] ? 1 : -1) * critical[u]; for ( int v: adj[u] ) { if ( v == p ) continue; reroot( u, v ); dfs( v, u ); reroot( v, u ); } } int lgPut( int x, int n ) { if ( n == 0 ) return 1; int p = lgPut( x, n / 2 ); p = (long long)p * p % MOD; if ( n % 2 == 1 ) p = (long long)p * x % MOD; return p; } int n; int calcDp( int i ) { if ( i == 0 ) return l; return (l * lgPut( n, 2 * i ) + c * calcDp( i - 1 )) % MOD; } signed main() { int d; cin >> n >> d; for ( int i = 0; i < n - 1; i++ ) { int u, v; cin >> u >> v; adj[u].push_back( v ); adj[v].push_back( u ); } dfsGame( 1, 0 ); dfsCritical( 1, 0 ); dfs( 1, 0 ); while ( c < 0 ) c += MOD; int dpd = (n == 2 ? lgPut( n, 2 * d ) : calcDp( d - 1 ) ); dfsGame( 1, 0 ); dfsCritical( 1, 0 ); if ( canWin[1] ) cout << (lgPut( n, 2 * d ) - dpd * critical[1] % MOD + MOD) % MOD; else cout << dpd * critical[1] % MOD << "\n"; 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...