#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 a, b;
int calcDp( int n ) {
if ( n == 0 )
return 1;
if ( n % 2 == 1 )
return (lgPut( a, n ) + b * calcDp( n - 1 )) % MOD;
int x = calcDp( n / 2 );
x = (x * (lgPut( a, n / 2 ) + lgPut( b, n / 2)) % MOD - lgPut( a, n / 2 ) * lgPut( b, n / 2 ) % MOD + MOD) % MOD;
return x;
}
signed main() {
int n, 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 );
a = n * n % MOD;
b = c;
while ( b < 0 )
b += MOD;
int dpd = calcDp( d - 1 );
dpd = dpd * l % MOD;
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6736 KB |
Output is correct |
2 |
Correct |
2 ms |
6736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6736 KB |
Output is correct |
2 |
Correct |
1 ms |
6736 KB |
Output is correct |
3 |
Correct |
2 ms |
6904 KB |
Output is correct |
4 |
Correct |
2 ms |
6736 KB |
Output is correct |
5 |
Correct |
2 ms |
6736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6736 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
2 ms |
6736 KB |
Output is correct |
4 |
Correct |
2 ms |
6736 KB |
Output is correct |
5 |
Correct |
2 ms |
6736 KB |
Output is correct |
6 |
Correct |
2 ms |
6736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6736 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
2 ms |
6736 KB |
Output is correct |
4 |
Correct |
2 ms |
6736 KB |
Output is correct |
5 |
Correct |
2 ms |
6736 KB |
Output is correct |
6 |
Correct |
2 ms |
6736 KB |
Output is correct |
7 |
Correct |
3 ms |
6736 KB |
Output is correct |
8 |
Correct |
2 ms |
6736 KB |
Output is correct |
9 |
Correct |
2 ms |
6736 KB |
Output is correct |
10 |
Correct |
2 ms |
6736 KB |
Output is correct |
11 |
Correct |
3 ms |
6736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6736 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
2 ms |
6736 KB |
Output is correct |
4 |
Correct |
2 ms |
6736 KB |
Output is correct |
5 |
Correct |
2 ms |
6736 KB |
Output is correct |
6 |
Correct |
2 ms |
6736 KB |
Output is correct |
7 |
Correct |
3 ms |
6736 KB |
Output is correct |
8 |
Correct |
2 ms |
6736 KB |
Output is correct |
9 |
Correct |
2 ms |
6736 KB |
Output is correct |
10 |
Correct |
2 ms |
6736 KB |
Output is correct |
11 |
Correct |
3 ms |
6736 KB |
Output is correct |
12 |
Correct |
97 ms |
14664 KB |
Output is correct |
13 |
Correct |
84 ms |
18752 KB |
Output is correct |
14 |
Correct |
50 ms |
11200 KB |
Output is correct |
15 |
Correct |
62 ms |
11336 KB |
Output is correct |
16 |
Correct |
60 ms |
11348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6736 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
2 ms |
6736 KB |
Output is correct |
4 |
Correct |
2 ms |
6736 KB |
Output is correct |
5 |
Correct |
2 ms |
6736 KB |
Output is correct |
6 |
Correct |
2 ms |
6736 KB |
Output is correct |
7 |
Correct |
3 ms |
6736 KB |
Output is correct |
8 |
Correct |
2 ms |
6736 KB |
Output is correct |
9 |
Correct |
2 ms |
6736 KB |
Output is correct |
10 |
Correct |
2 ms |
6736 KB |
Output is correct |
11 |
Correct |
3 ms |
6736 KB |
Output is correct |
12 |
Correct |
2 ms |
6912 KB |
Output is correct |
13 |
Correct |
2 ms |
6736 KB |
Output is correct |
14 |
Correct |
1 ms |
6736 KB |
Output is correct |
15 |
Correct |
2 ms |
6736 KB |
Output is correct |
16 |
Correct |
2 ms |
6736 KB |
Output is correct |
17 |
Correct |
2 ms |
6736 KB |
Output is correct |
18 |
Correct |
2 ms |
6736 KB |
Output is correct |
19 |
Correct |
2 ms |
6752 KB |
Output is correct |
20 |
Correct |
3 ms |
6772 KB |
Output is correct |
21 |
Correct |
3 ms |
6736 KB |
Output is correct |
22 |
Correct |
2 ms |
6788 KB |
Output is correct |
23 |
Correct |
2 ms |
6736 KB |
Output is correct |
24 |
Correct |
2 ms |
6736 KB |
Output is correct |
25 |
Correct |
2 ms |
6736 KB |
Output is correct |
26 |
Correct |
2 ms |
6736 KB |
Output is correct |
27 |
Correct |
2 ms |
6736 KB |
Output is correct |
28 |
Correct |
2 ms |
6736 KB |
Output is correct |
29 |
Correct |
2 ms |
6736 KB |
Output is correct |
30 |
Correct |
2 ms |
6796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6736 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
2 ms |
6736 KB |
Output is correct |
4 |
Correct |
2 ms |
6736 KB |
Output is correct |
5 |
Correct |
2 ms |
6736 KB |
Output is correct |
6 |
Correct |
2 ms |
6736 KB |
Output is correct |
7 |
Correct |
3 ms |
6736 KB |
Output is correct |
8 |
Correct |
2 ms |
6736 KB |
Output is correct |
9 |
Correct |
2 ms |
6736 KB |
Output is correct |
10 |
Correct |
2 ms |
6736 KB |
Output is correct |
11 |
Correct |
3 ms |
6736 KB |
Output is correct |
12 |
Correct |
97 ms |
14664 KB |
Output is correct |
13 |
Correct |
84 ms |
18752 KB |
Output is correct |
14 |
Correct |
50 ms |
11200 KB |
Output is correct |
15 |
Correct |
62 ms |
11336 KB |
Output is correct |
16 |
Correct |
60 ms |
11348 KB |
Output is correct |
17 |
Correct |
2 ms |
6912 KB |
Output is correct |
18 |
Correct |
2 ms |
6736 KB |
Output is correct |
19 |
Correct |
1 ms |
6736 KB |
Output is correct |
20 |
Correct |
2 ms |
6736 KB |
Output is correct |
21 |
Correct |
2 ms |
6736 KB |
Output is correct |
22 |
Correct |
2 ms |
6736 KB |
Output is correct |
23 |
Correct |
2 ms |
6736 KB |
Output is correct |
24 |
Correct |
2 ms |
6752 KB |
Output is correct |
25 |
Correct |
3 ms |
6772 KB |
Output is correct |
26 |
Correct |
3 ms |
6736 KB |
Output is correct |
27 |
Correct |
2 ms |
6788 KB |
Output is correct |
28 |
Correct |
2 ms |
6736 KB |
Output is correct |
29 |
Correct |
2 ms |
6736 KB |
Output is correct |
30 |
Correct |
2 ms |
6736 KB |
Output is correct |
31 |
Correct |
2 ms |
6736 KB |
Output is correct |
32 |
Correct |
2 ms |
6736 KB |
Output is correct |
33 |
Correct |
2 ms |
6736 KB |
Output is correct |
34 |
Correct |
2 ms |
6736 KB |
Output is correct |
35 |
Correct |
2 ms |
6796 KB |
Output is correct |
36 |
Correct |
68 ms |
14676 KB |
Output is correct |
37 |
Correct |
86 ms |
18828 KB |
Output is correct |
38 |
Correct |
48 ms |
11156 KB |
Output is correct |
39 |
Correct |
65 ms |
11400 KB |
Output is correct |
40 |
Correct |
66 ms |
11396 KB |
Output is correct |
41 |
Correct |
72 ms |
16828 KB |
Output is correct |
42 |
Correct |
66 ms |
17748 KB |
Output is correct |
43 |
Correct |
42 ms |
10696 KB |
Output is correct |
44 |
Correct |
60 ms |
11344 KB |
Output is correct |
45 |
Correct |
65 ms |
11424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6736 KB |
Output is correct |
2 |
Correct |
2 ms |
6736 KB |
Output is correct |
3 |
Correct |
2 ms |
6736 KB |
Output is correct |
4 |
Correct |
1 ms |
6736 KB |
Output is correct |
5 |
Correct |
2 ms |
6904 KB |
Output is correct |
6 |
Correct |
2 ms |
6736 KB |
Output is correct |
7 |
Correct |
2 ms |
6736 KB |
Output is correct |
8 |
Correct |
2 ms |
6736 KB |
Output is correct |
9 |
Correct |
2 ms |
6748 KB |
Output is correct |
10 |
Correct |
2 ms |
6736 KB |
Output is correct |
11 |
Correct |
2 ms |
6736 KB |
Output is correct |
12 |
Correct |
2 ms |
6736 KB |
Output is correct |
13 |
Correct |
2 ms |
6736 KB |
Output is correct |
14 |
Correct |
3 ms |
6736 KB |
Output is correct |
15 |
Correct |
2 ms |
6736 KB |
Output is correct |
16 |
Correct |
2 ms |
6736 KB |
Output is correct |
17 |
Correct |
2 ms |
6736 KB |
Output is correct |
18 |
Correct |
3 ms |
6736 KB |
Output is correct |
19 |
Correct |
97 ms |
14664 KB |
Output is correct |
20 |
Correct |
84 ms |
18752 KB |
Output is correct |
21 |
Correct |
50 ms |
11200 KB |
Output is correct |
22 |
Correct |
62 ms |
11336 KB |
Output is correct |
23 |
Correct |
60 ms |
11348 KB |
Output is correct |
24 |
Correct |
2 ms |
6912 KB |
Output is correct |
25 |
Correct |
2 ms |
6736 KB |
Output is correct |
26 |
Correct |
1 ms |
6736 KB |
Output is correct |
27 |
Correct |
2 ms |
6736 KB |
Output is correct |
28 |
Correct |
2 ms |
6736 KB |
Output is correct |
29 |
Correct |
2 ms |
6736 KB |
Output is correct |
30 |
Correct |
2 ms |
6736 KB |
Output is correct |
31 |
Correct |
2 ms |
6752 KB |
Output is correct |
32 |
Correct |
3 ms |
6772 KB |
Output is correct |
33 |
Correct |
3 ms |
6736 KB |
Output is correct |
34 |
Correct |
2 ms |
6788 KB |
Output is correct |
35 |
Correct |
2 ms |
6736 KB |
Output is correct |
36 |
Correct |
2 ms |
6736 KB |
Output is correct |
37 |
Correct |
2 ms |
6736 KB |
Output is correct |
38 |
Correct |
2 ms |
6736 KB |
Output is correct |
39 |
Correct |
2 ms |
6736 KB |
Output is correct |
40 |
Correct |
2 ms |
6736 KB |
Output is correct |
41 |
Correct |
2 ms |
6736 KB |
Output is correct |
42 |
Correct |
2 ms |
6796 KB |
Output is correct |
43 |
Correct |
68 ms |
14676 KB |
Output is correct |
44 |
Correct |
86 ms |
18828 KB |
Output is correct |
45 |
Correct |
48 ms |
11156 KB |
Output is correct |
46 |
Correct |
65 ms |
11400 KB |
Output is correct |
47 |
Correct |
66 ms |
11396 KB |
Output is correct |
48 |
Correct |
72 ms |
16828 KB |
Output is correct |
49 |
Correct |
66 ms |
17748 KB |
Output is correct |
50 |
Correct |
42 ms |
10696 KB |
Output is correct |
51 |
Correct |
60 ms |
11344 KB |
Output is correct |
52 |
Correct |
65 ms |
11424 KB |
Output is correct |
53 |
Correct |
75 ms |
18760 KB |
Output is correct |
54 |
Correct |
81 ms |
17224 KB |
Output is correct |
55 |
Correct |
40 ms |
10176 KB |
Output is correct |
56 |
Correct |
67 ms |
14940 KB |
Output is correct |
57 |
Correct |
75 ms |
11604 KB |
Output is correct |
58 |
Correct |
59 ms |
11512 KB |
Output is correct |
59 |
Correct |
65 ms |
11336 KB |
Output is correct |
60 |
Correct |
60 ms |
11344 KB |
Output is correct |