#include <bits/stdc++.h>
#define mod 1000000007
using namespace std;
long long put(long long baza, long long exp)
{
if(!exp)
return 1;
long long ans = put(baza, exp/2);
ans *= ans;
ans %= mod;
if(exp % 2)
{
ans *= baza;
ans %= mod;
}
return ans;
}
long long n,d;
bool win[100005];
long long nrCritF[100005];
vector<int> v[100005];
bool dp[100005];
int nrFChild[100005];
int nrCrit[100005];
long long cntF[100005];
void calcDp(int p, int avoid=0)
{
nrFChild[p] = 0;
dp[p] = false;
nrCrit[p] = 0;
for(int it : v[p])
{
if(it == avoid)
continue;
if(!dp[it])
{
nrFChild[p] ++;
dp[p] = true;
}
}
if(!dp[p])
nrCrit[p] = 1;
for(int it : v[p])
{
if(it == avoid)
continue;
if(!dp[p] && dp[it])
nrCrit[p] += nrCrit[it];
if(dp[p] && !dp[it] && nrFChild[p] == 1)
nrCrit[p] += nrCrit[it];
}
}
void dfs(int p, int par)
{
for(int it : v[p])
{
if(it == par)
continue;
dfs(it, p);
}
calcDp(p, par);
}
void calc(int p, int par)
{
win[p] = dp[p];
nrCritF[p] = nrCrit[p];
for(int it : v[p])
{
if(it == par)
continue;
calcDp(p, it);
calcDp(it);
calc(it, p);
calcDp(it, p);
calcDp(p);
}
}
int main()
{
cin>>n>>d;
if(n == 2)
{
cout<<put(n, 2*d);
return 0;
}
for(int i=1;i<n;i++)
{
int x, y;
cin>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
dfs(1, 0);
calc(1, 0);
/*
for(int i=1;i<=n;i++)
cout<<win[i]<<' '<<nrCritF[i]<<'\n';
*/
for(int i=1;i<=n;i++)
if(!win[i])
cntF[i] = 1;
for(int i=d-1; i>=0; i--)
{
long long nrF = 0;
for(int j=1;j<=n;j++)
nrF += cntF[j];
for(int j=1;j<=n;j++)
{
if(win[j])
{
cntF[j] = nrCritF[j] * nrF;
cntF[j] %= mod;
}
else
{
cntF[j] = nrCritF[j] * (n - nrF);
cntF[j] %= mod;
cntF[j] += (n - nrCritF[j]) * n;
cntF[j] %= mod;
}
}
}
cout<<(put(n, 2*d) - cntF[1] + mod) % mod;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
6 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2664 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2664 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2664 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2664 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
3 ms |
2668 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
4 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2664 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
3 ms |
2668 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
4 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
98 ms |
11756 KB |
Output is correct |
13 |
Correct |
97 ms |
14068 KB |
Output is correct |
14 |
Execution timed out |
1084 ms |
8668 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2664 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
3 ms |
2668 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
4 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Incorrect |
6 ms |
2664 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2664 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
3 ms |
2668 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
4 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
98 ms |
11756 KB |
Output is correct |
13 |
Correct |
97 ms |
14068 KB |
Output is correct |
14 |
Execution timed out |
1084 ms |
8668 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
6 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |