#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = 1000000007;
#define fi first
#define se second
#define pb push_back
ll modPow(ll a,ll b,ll c){
ll r=1;
while(b){
if(b&1) (r*=a)%=mod;
(a*=a)%=mod;
b/=2;
}
return r;
}
vector<ll> g[100000];
int v[100000];
bool win[100000];
void dfs(ll a,ll b){
v[a]=1;
for(auto e:g[a]){
if(e==b) continue;
dfs(e,a);
if(!win[e]) win[a]=true;
v[a]+=v[e];
}
}
ll cnt=0;
void dfs2(ll a,ll b,ll x){
if(x==0) cnt++;
if(x==1){
ll k=0;
for(auto e:g[a]){
if(e==b) continue;
if(!win[e]) k++;
}
if(k>=2) return;
}
for(auto e:g[a]){
if(e==b) continue;
if(x==1&&win[e]) continue;
dfs2(e,a,x^1);
}
}
ll cnt2=0;
void dfs3(ll a,ll b,ll x){
if(x) cnt2++;
if(x==0){
ll k=0;
for(auto e:g[a]){
if(e==b) continue;
if(!win[e]) k++;
}
if(k>=2) return;
}
for(auto e:g[a]){
if(e==b) continue;
if(x==0&&win[e]) continue;
dfs3(e,a,x^1);
}
}
int main(){
ll n,d;
cin>>n>>d;
assert(n<=1000&&d<=100000);
if(n==2){
cout<<modPow(4,2*d,mod)<<endl;
return 0;
}
for(int i=0;i<n-1;i++){
ll a,b;
cin>>a>>b;
a--;
b--;
g[a].pb(b);
g[b].pb(a);
}
ll x=0,y=0;
ll z=0;
ll type=0,x1=0,y1=0;
for(int i=0;i<n;i++){
cnt=0,cnt2=0;
for(int j=0;j<n;j++) win[j]=false;
dfs(i,-1);
ll cn=0,V=0;
for(auto e:g[i]){
if(!win[e]){
cn++;
V=e;
}
}
if(cn) z++;
if(cn>=2){
x++;
}
if(cn==1){
dfs3(V,i,1);
x+=modPow(n,mod-2,mod)*(n-cnt2);
x%=mod;
y+=cnt2;
y%=mod;
if(i==0){
type=1;
x1+=modPow(n,mod-2,mod)*(n-cnt2);
y1+=cnt2;
}
}
if(cn==0){
dfs2(i,-1,0);
y-=cnt;
y+=mod;
y%=mod;
x+=modPow(n%mod,mod-2,mod)*cnt;
x%=mod;
if(i==0){
type=1;
x1+=modPow(n%mod,mod-2,mod)*cnt;
y1-=cnt;
y1+=mod;
}
}
}
x1%=mod;
y1%=mod;
for(int i=1;i<d;i++){
z=(z*y + x*modPow(n,2*i,mod));
z%=mod;
}
if(type==0){
cout<<modPow(n,2*d,mod)<<endl;
}
else{
cout<<(x1*modPow(n,2*d,mod)+y1*z)%mod<<endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
14 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
22 ms |
2644 KB |
Output is correct |
8 |
Correct |
27 ms |
2760 KB |
Output is correct |
9 |
Correct |
19 ms |
2644 KB |
Output is correct |
10 |
Correct |
15 ms |
2696 KB |
Output is correct |
11 |
Correct |
21 ms |
2772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
22 ms |
2644 KB |
Output is correct |
8 |
Correct |
27 ms |
2760 KB |
Output is correct |
9 |
Correct |
19 ms |
2644 KB |
Output is correct |
10 |
Correct |
15 ms |
2696 KB |
Output is correct |
11 |
Correct |
21 ms |
2772 KB |
Output is correct |
12 |
Runtime error |
3 ms |
5204 KB |
Execution killed with signal 6 |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
22 ms |
2644 KB |
Output is correct |
8 |
Correct |
27 ms |
2760 KB |
Output is correct |
9 |
Correct |
19 ms |
2644 KB |
Output is correct |
10 |
Correct |
15 ms |
2696 KB |
Output is correct |
11 |
Correct |
21 ms |
2772 KB |
Output is correct |
12 |
Correct |
1 ms |
2644 KB |
Output is correct |
13 |
Correct |
14 ms |
2692 KB |
Output is correct |
14 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
22 ms |
2644 KB |
Output is correct |
8 |
Correct |
27 ms |
2760 KB |
Output is correct |
9 |
Correct |
19 ms |
2644 KB |
Output is correct |
10 |
Correct |
15 ms |
2696 KB |
Output is correct |
11 |
Correct |
21 ms |
2772 KB |
Output is correct |
12 |
Runtime error |
3 ms |
5204 KB |
Execution killed with signal 6 |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
14 ms |
2644 KB |
Output is correct |
3 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |