#include <bits/stdc++.h>
using namespace std;
int n;
long long d;
const int mod=1e9+7;
long long fastpow(long long a,long long b) {
if (b==0) {
return 1;
}
if (b%2==1) {
return (fastpow(a,b-1)*a)%mod;
}
long long half=fastpow(a,b/2);
return (half*half)%mod;
}
vector<int> adj[100001];
int dp[100001];
int dp1[100001];
int dep[100001];
int deg[1001];
void dfs1(int v,int prev) {
for(int i=0;i<adj[v].size();i++) {
int nt=adj[v][i];
if (nt==prev){
continue;
}
dep[nt]=dep[v]+1;
dfs1(nt,v);
deg[v]++;
if (dp[nt]==0) {
dp[v]++;
}
}
}
void dfs2(int v,int prev) {
if (prev==0) {
dp1[v]=1;
}
for(int i=0;i<adj[v].size();i++) {
int nt=adj[v][i];
if (nt==prev) {
continue;
}
if (dp[nt]==0&&dp[v]>1) {
dp1[nt]=0;
}
else if (dp[nt]>0&&dp[v]>0) {
dp1[nt]=0;
}
else {
dp1[nt]=2-dp1[v];
}
dfs2(nt,v);
}
}
void dfs3(int v,int prev) {
if (prev==0) {
dp1[v]=0;
}
for(int i=0;i<adj[v].size();i++) {
int nt=adj[v][i];
if (nt==prev) {
continue;
}
if (dp[nt]==0&&dp[v]>1) {
dp1[nt]=0;
}
else if (dp[nt]>0&&dp[v]>0) {
dp1[nt]=0;
}
else {
dp1[nt]=1-dp1[v];
}
dfs3(nt,v);
}
}
long long d1,d2,d3,d4;
long long one,two,three,four;
int main(void) {
scanf("%d %lld",&n,&d);
for(int i=1;i<n;i++) {
int u,v;
scanf("%d %d",&u,&v);
adj[u].push_back(v);
adj[v].push_back(u);
}
long long s=0;
dfs1(1,0);
dfs3(1,0);
for(int i=1;i<=n;i++) if (dp[i]!=0||dp1[i]!=0) { s++;}
//printf("%lld\n",s);
for(int i=1;i<=n;i++) {
memset(dp,0,sizeof(dp));
memset(dp1,0,sizeof(dp1));
memset(deg,0,sizeof(deg));
dep[i]=0;
dfs1(i,0);
dfs2(i,0);
for(int j=1;j<=n;j++) {
if (dp[j]>0) {
d1++;
if (j==1) one++;
}
else if (i==j) {
d3++;
if (j==1) three++;
}
else if (dp1[j]==2) {
d1++;
if (j==1) one++;
}
else if (dp1[j]==0) {
d2++;
if (j==1) two++;
}
else {
if (dep[i]%2==dep[j]%2) {
d3++;
if (j==1) three++;
}
else {
d4++;
if (j==1) four++;
}
}
//printf("%d %d %d %d %d %d\n",i,j,one,two,three,four);
}
}
//printf("%lld %lld %lld %lld\n",one,two,three,four);
for(int i=1;i<d;i++) {
s=fastpow(n,2*i-1)*(d1+d3)+s*(d4-d3);
s%=mod;
s+=mod;
s%=mod;
}
long long ret=fastpow(n,2*d-1)*one+four*s+three*(fastpow(n,2*d-1)-s);
ret%=mod;
ret+=mod;
ret%=mod;
printf("%lld",ret);
}
Compilation message
startrek.cpp: In function 'void dfs1(int, int)':
startrek.cpp:26:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | for(int i=0;i<adj[v].size();i++) {
| ~^~~~~~~~~~~~~~
startrek.cpp: In function 'void dfs2(int, int)':
startrek.cpp:44:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for(int i=0;i<adj[v].size();i++) {
| ~^~~~~~~~~~~~~~
startrek.cpp: In function 'void dfs3(int, int)':
startrek.cpp:66:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | for(int i=0;i<adj[v].size();i++) {
| ~^~~~~~~~~~~~~~
startrek.cpp: In function 'int main()':
startrek.cpp:88:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
88 | scanf("%d %lld",&n,&d);
| ~~~~~^~~~~~~~~~~~~~~~~
startrek.cpp:91:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
91 | scanf("%d %d",&u,&v);
| ~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3412 KB |
Output is correct |
2 |
Correct |
67 ms |
3448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3432 KB |
Output is correct |
2 |
Correct |
4 ms |
3424 KB |
Output is correct |
3 |
Execution timed out |
1076 ms |
3420 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
5 ms |
3412 KB |
Output is correct |
3 |
Correct |
6 ms |
3444 KB |
Output is correct |
4 |
Correct |
4 ms |
3444 KB |
Output is correct |
5 |
Correct |
6 ms |
3412 KB |
Output is correct |
6 |
Correct |
4 ms |
3412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
5 ms |
3412 KB |
Output is correct |
3 |
Correct |
6 ms |
3444 KB |
Output is correct |
4 |
Correct |
4 ms |
3444 KB |
Output is correct |
5 |
Correct |
6 ms |
3412 KB |
Output is correct |
6 |
Correct |
4 ms |
3412 KB |
Output is correct |
7 |
Correct |
70 ms |
3412 KB |
Output is correct |
8 |
Correct |
73 ms |
3540 KB |
Output is correct |
9 |
Correct |
50 ms |
3452 KB |
Output is correct |
10 |
Correct |
60 ms |
3452 KB |
Output is correct |
11 |
Correct |
58 ms |
3412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
5 ms |
3412 KB |
Output is correct |
3 |
Correct |
6 ms |
3444 KB |
Output is correct |
4 |
Correct |
4 ms |
3444 KB |
Output is correct |
5 |
Correct |
6 ms |
3412 KB |
Output is correct |
6 |
Correct |
4 ms |
3412 KB |
Output is correct |
7 |
Correct |
70 ms |
3412 KB |
Output is correct |
8 |
Correct |
73 ms |
3540 KB |
Output is correct |
9 |
Correct |
50 ms |
3452 KB |
Output is correct |
10 |
Correct |
60 ms |
3452 KB |
Output is correct |
11 |
Correct |
58 ms |
3412 KB |
Output is correct |
12 |
Execution timed out |
1092 ms |
13220 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
5 ms |
3412 KB |
Output is correct |
3 |
Correct |
6 ms |
3444 KB |
Output is correct |
4 |
Correct |
4 ms |
3444 KB |
Output is correct |
5 |
Correct |
6 ms |
3412 KB |
Output is correct |
6 |
Correct |
4 ms |
3412 KB |
Output is correct |
7 |
Correct |
70 ms |
3412 KB |
Output is correct |
8 |
Correct |
73 ms |
3540 KB |
Output is correct |
9 |
Correct |
50 ms |
3452 KB |
Output is correct |
10 |
Correct |
60 ms |
3452 KB |
Output is correct |
11 |
Correct |
58 ms |
3412 KB |
Output is correct |
12 |
Correct |
2 ms |
3412 KB |
Output is correct |
13 |
Correct |
70 ms |
3452 KB |
Output is correct |
14 |
Correct |
2 ms |
3412 KB |
Output is correct |
15 |
Correct |
4 ms |
3424 KB |
Output is correct |
16 |
Correct |
4 ms |
3412 KB |
Output is correct |
17 |
Correct |
4 ms |
3420 KB |
Output is correct |
18 |
Correct |
4 ms |
3412 KB |
Output is correct |
19 |
Correct |
7 ms |
3420 KB |
Output is correct |
20 |
Correct |
5 ms |
3412 KB |
Output is correct |
21 |
Correct |
71 ms |
3500 KB |
Output is correct |
22 |
Correct |
71 ms |
3540 KB |
Output is correct |
23 |
Correct |
60 ms |
3472 KB |
Output is correct |
24 |
Correct |
86 ms |
3460 KB |
Output is correct |
25 |
Correct |
68 ms |
3456 KB |
Output is correct |
26 |
Correct |
83 ms |
3412 KB |
Output is correct |
27 |
Correct |
114 ms |
3532 KB |
Output is correct |
28 |
Correct |
56 ms |
3412 KB |
Output is correct |
29 |
Correct |
73 ms |
3456 KB |
Output is correct |
30 |
Correct |
84 ms |
3460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
5 ms |
3412 KB |
Output is correct |
3 |
Correct |
6 ms |
3444 KB |
Output is correct |
4 |
Correct |
4 ms |
3444 KB |
Output is correct |
5 |
Correct |
6 ms |
3412 KB |
Output is correct |
6 |
Correct |
4 ms |
3412 KB |
Output is correct |
7 |
Correct |
70 ms |
3412 KB |
Output is correct |
8 |
Correct |
73 ms |
3540 KB |
Output is correct |
9 |
Correct |
50 ms |
3452 KB |
Output is correct |
10 |
Correct |
60 ms |
3452 KB |
Output is correct |
11 |
Correct |
58 ms |
3412 KB |
Output is correct |
12 |
Execution timed out |
1092 ms |
13220 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3412 KB |
Output is correct |
2 |
Correct |
67 ms |
3448 KB |
Output is correct |
3 |
Correct |
2 ms |
3432 KB |
Output is correct |
4 |
Correct |
4 ms |
3424 KB |
Output is correct |
5 |
Execution timed out |
1076 ms |
3420 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |