#include <bits/stdc++.h>
using namespace std;
const int MAXN=1e5, MOD=1e9+7;
vector<int> adj[MAXN+1];
bool /*win0[MAXN+1],*/ win1[MAXN+1];
int losechildren[MAXN+1], critical[MAXN+1];
int n, lose=0;
long long e=0;
long long power(long long a, long long k)
{
long long result=1, current=a;
for (long long i=0; i<64; i++)
{
if (k&((long long)1<<i))
result=result*current%MOD;
current=current*current%MOD;
}
return result;
}
/*bool dfs0(int x, int t)
{
bool possible=false;
for (auto y : adj[x])
{
if (y==t)
continue;
if (!dfs0(y, x))
possible=true;
}
return win0[x]=possible;
}*/
/*void dfs1(int x, int t)
{
if (t==-1)
{
win1[x]=win0[x];
for (auto y : adj[x])
{
if (y==t)
continue;
dfs1(y, x);
}
return;
}
int memox=losechildren[x];
int memot=losechildren[t];
if (!win0[x])
losechildren[t]=losechildren[t]-1;
if (losechildren[t]==0)
losechildren[x]=losechildren[x]+1;
if (losechildren[x]>0)
win1[x]=true;
else
win1[x]=false;
for (auto y : adj[x])
{
if (y==t)
continue;
dfs1(y, x);
}
losechildren[x]=memox;
losechildren[t]=memot;
return;
}*/
pair<bool, int> dfscritical(int x, int t)
{
int critical=0, losecritical=0, losenodes=0;
for (auto y : adj[x])
{
if (y==t)
continue;
auto result=dfscritical(y, x);
critical=critical+result.second;
if (!result.first)
{
losecritical=losecritical+result.second;
losenodes=losenodes+1;
}
}
if (losenodes==0)
return {false, critical+1};
else if (losenodes==1)
return {true, losecritical};
return {true, 0};
}
/*void countlosechildren(int x, int t)
{
losechildren[x]=0;
for (auto y : adj[x])
{
if (y==t)
continue;
countlosechildren(y, x);
if (!win0[y])
losechildren[x]=losechildren[x]+1;
}
return;
}*/
long long dp(int d)
{
if (d==0)
return lose;
return (power(n, 2*d)+MOD-e*dp(d-1)%MOD)%MOD;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int x, y, i;
long long d, total;
cin >> n >> d;
for (i=0; i<n-1; i++)
{
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
if (n==2)
{
cout << power(4, d);
return 0;
}
//dfs0(1, -1);
//countlosechildren(1, -1);
//dfs1(1, -1);
for (i=1; i<=n; i++)
{
auto result=dfscritical(i, -1);
critical[i]=result.second;
win1[i]=result.first;
}
for (i=1; i<=n; i++)
if (!win1[i])
lose=lose+1;
for (i=1; i<=n; i++)
{
if (win1[i])
e=(e+critical[i])%MOD;
else
e=(e+MOD-critical[i])%MOD;
}
if (win1[1])
total=(power(n, 2*d)+MOD-critical[1]*dp(d-1)%MOD)%MOD;
else
total=critical[1]*dp(d-1)%MOD;
cout << total;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3420 KB |
Output is correct |
2 |
Incorrect |
8 ms |
3420 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3420 KB |
Output is correct |
2 |
Correct |
1 ms |
3420 KB |
Output is correct |
3 |
Correct |
1 ms |
3420 KB |
Output is correct |
4 |
Correct |
1 ms |
3420 KB |
Output is correct |
5 |
Correct |
1 ms |
3504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3420 KB |
Output is correct |
2 |
Correct |
1 ms |
3420 KB |
Output is correct |
3 |
Correct |
2 ms |
3420 KB |
Output is correct |
4 |
Correct |
1 ms |
3416 KB |
Output is correct |
5 |
Correct |
1 ms |
3420 KB |
Output is correct |
6 |
Correct |
1 ms |
3416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3420 KB |
Output is correct |
2 |
Correct |
1 ms |
3420 KB |
Output is correct |
3 |
Correct |
2 ms |
3420 KB |
Output is correct |
4 |
Correct |
1 ms |
3416 KB |
Output is correct |
5 |
Correct |
1 ms |
3420 KB |
Output is correct |
6 |
Correct |
1 ms |
3416 KB |
Output is correct |
7 |
Correct |
11 ms |
3588 KB |
Output is correct |
8 |
Correct |
12 ms |
3620 KB |
Output is correct |
9 |
Correct |
8 ms |
3420 KB |
Output is correct |
10 |
Correct |
8 ms |
3420 KB |
Output is correct |
11 |
Correct |
8 ms |
3420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3420 KB |
Output is correct |
2 |
Correct |
1 ms |
3420 KB |
Output is correct |
3 |
Correct |
2 ms |
3420 KB |
Output is correct |
4 |
Correct |
1 ms |
3416 KB |
Output is correct |
5 |
Correct |
1 ms |
3420 KB |
Output is correct |
6 |
Correct |
1 ms |
3416 KB |
Output is correct |
7 |
Correct |
11 ms |
3588 KB |
Output is correct |
8 |
Correct |
12 ms |
3620 KB |
Output is correct |
9 |
Correct |
8 ms |
3420 KB |
Output is correct |
10 |
Correct |
8 ms |
3420 KB |
Output is correct |
11 |
Correct |
8 ms |
3420 KB |
Output is correct |
12 |
Execution timed out |
1057 ms |
12892 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3420 KB |
Output is correct |
2 |
Correct |
1 ms |
3420 KB |
Output is correct |
3 |
Correct |
2 ms |
3420 KB |
Output is correct |
4 |
Correct |
1 ms |
3416 KB |
Output is correct |
5 |
Correct |
1 ms |
3420 KB |
Output is correct |
6 |
Correct |
1 ms |
3416 KB |
Output is correct |
7 |
Correct |
11 ms |
3588 KB |
Output is correct |
8 |
Correct |
12 ms |
3620 KB |
Output is correct |
9 |
Correct |
8 ms |
3420 KB |
Output is correct |
10 |
Correct |
8 ms |
3420 KB |
Output is correct |
11 |
Correct |
8 ms |
3420 KB |
Output is correct |
12 |
Correct |
1 ms |
3420 KB |
Output is correct |
13 |
Incorrect |
8 ms |
3544 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3420 KB |
Output is correct |
2 |
Correct |
1 ms |
3420 KB |
Output is correct |
3 |
Correct |
2 ms |
3420 KB |
Output is correct |
4 |
Correct |
1 ms |
3416 KB |
Output is correct |
5 |
Correct |
1 ms |
3420 KB |
Output is correct |
6 |
Correct |
1 ms |
3416 KB |
Output is correct |
7 |
Correct |
11 ms |
3588 KB |
Output is correct |
8 |
Correct |
12 ms |
3620 KB |
Output is correct |
9 |
Correct |
8 ms |
3420 KB |
Output is correct |
10 |
Correct |
8 ms |
3420 KB |
Output is correct |
11 |
Correct |
8 ms |
3420 KB |
Output is correct |
12 |
Execution timed out |
1057 ms |
12892 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3420 KB |
Output is correct |
2 |
Incorrect |
8 ms |
3420 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |