#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1e5;
const ll MOD = 1e9+7;
int N; ll D;
vector<int> adj[MAXN+10];
typedef vector<vector<ll>> Mat;
Mat operator * (const Mat &p, const Mat &q)
{
Mat ret=vector<vector<ll>>(p.size(), vector<ll>(q[0].size()));
assert(p[0].size()==q.size());
for(int i=0; i<p.size(); i++) for(int j=0; j<q[0].size(); j++)
{
for(int k=0; k<p[0].size(); k++)
{
ret[i][j]+=p[i][k]*q[k][j]%MOD;
ret[i][j]%=MOD;
}
}
return ret;
}
Mat mypow(Mat x, ll y)
{
if(y==0) return {{1, 0}, {0, 1}};
if(y%2) return mypow(x, y-1)*x;
Mat t=mypow(x, y/2);
return t*t;
}
ll ans;
ll a, b, c, d, p, q, x, y;
int dp[MAXN+10], dp2[MAXN+10], zero;
int cnt0[MAXN+10], cnt1[MAXN+10];
void dfs(int now, int bef)
{
for(int nxt : adj[now])
{
if(nxt==bef) continue;
dfs(nxt, now);
dp[now]+=!dp[nxt];
}
zero+=!dp[now];
dp2[now]=!dp[now];
for(int nxt : adj[now])
{
if(nxt==bef) continue;
if(dp[nxt]==0) cnt0[now]+=dp2[nxt];
else if(dp[nxt]==1) cnt1[now]+=dp2[nxt];
}
if(dp[now]==0) dp2[now]+=cnt1[now];
else if(dp[now]==1) dp2[now]+=cnt0[now];
}
void dfs2(int now, int bef)
{
//printf("%d : dp %d dp2 %d zero %d\n", now, dp[now], dp2[now], zero);
//for(int i=1; i<=N; i++) printf("%d : %d / ", i, dp2[i]); printf("\n");
if(dp[now]==0) c++;
else a++;
if(dp[now]==0) d+=N-zero;
else b+=N-zero;
if(dp[now]==0)
{
b+=dp2[now];
d+=zero-dp2[now];
}
else
{
d+=dp2[now];
b+=zero-dp2[now];
}
//printf("%lld %lld %lld %lld\n", a, b, c, d);
if(now==1) { x=a; y=b; }
for(int nxt : adj[now])
{
if(nxt==bef) continue;
int m1=dp2[now], m2=dp2[nxt];
int m3=cnt1[now], m4=cnt0[now], m5=cnt1[nxt], m6=cnt0[nxt];
zero-=!dp[now];
zero-=!dp[nxt];
dp[now]-=!dp[nxt];
if(dp[nxt]==0) cnt0[now]-=dp2[nxt];
else if(dp[nxt]==1) cnt1[now]-=dp2[nxt];
dp2[now]=!dp[now];
if(dp[now]==0) dp2[now]+=cnt1[now];
else if(dp[now]==1) dp2[now]+=cnt0[now];
dp[nxt]+=!dp[now];
if(dp[now]==0) cnt0[nxt]+=dp2[now];
else if(dp[now]==1) cnt1[nxt]+=dp2[now];
dp2[nxt]=!dp[nxt];
if(dp[nxt]==0) dp2[nxt]+=cnt1[nxt];
else if(dp[nxt]==1) dp2[nxt]+=cnt0[nxt];
zero+=!dp[nxt];
zero+=!dp[now];
dfs2(nxt, now);
zero-=!dp[now];
zero-=!dp[nxt];
dp[nxt]-=!dp[now];
dp[now]+=!dp[nxt];
zero+=!dp[nxt];
zero+=!dp[now];
dp2[now]=m1;
dp2[nxt]=m2;
cnt1[now]=m3; cnt0[now]=m4;
cnt1[nxt]=m5; cnt0[nxt]=m6;
}
}
int main()
{
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);
}
dfs(1, 1);
dfs2(1, 1);
p=a; q=c;
a*=N; c*=N; x*=N;
a%=MOD; b%=MOD; c%=MOD; d%=MOD; x%=MOD; y%=MOD; p%=MOD; q%=MOD;
Mat M2={{a, b}, {c, d}};
Mat M1={{x, y}, {0, 0}};
Mat M3={{p}, {q}};
Mat MM=M1*mypow(M2, D-1)*M3;
//printf("!a %lld b %lld c %lld d %lld / x %lld y %lld\n", a, b, c, d, x, y);
printf("%lld\n", MM[0][0]);
}
Compilation message
startrek.cpp: In function 'Mat operator*(const Mat&, const Mat&)':
startrek.cpp:20:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | for(int i=0; i<p.size(); i++) for(int j=0; j<q[0].size(); j++)
| ~^~~~~~~~~
startrek.cpp:20:46: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | for(int i=0; i<p.size(); i++) for(int j=0; j<q[0].size(); j++)
| ~^~~~~~~~~~~~
startrek.cpp:22:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for(int k=0; k<p[0].size(); k++)
| ~^~~~~~~~~~~~
startrek.cpp: In function 'int main()':
startrek.cpp:140:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
140 | scanf("%d%lld", &N, &D);
| ~~~~~^~~~~~~~~~~~~~~~~~
startrek.cpp:145:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
145 | scanf("%d%d", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
2 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
2 ms |
2720 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Correct |
2 ms |
2688 KB |
Output is correct |
5 |
Correct |
2 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
2 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Correct |
2 ms |
2688 KB |
Output is correct |
5 |
Correct |
2 ms |
2688 KB |
Output is correct |
6 |
Correct |
2 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
2 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Correct |
2 ms |
2688 KB |
Output is correct |
5 |
Correct |
2 ms |
2688 KB |
Output is correct |
6 |
Correct |
2 ms |
2688 KB |
Output is correct |
7 |
Correct |
3 ms |
2816 KB |
Output is correct |
8 |
Correct |
3 ms |
2816 KB |
Output is correct |
9 |
Correct |
2 ms |
2688 KB |
Output is correct |
10 |
Correct |
2 ms |
2688 KB |
Output is correct |
11 |
Correct |
3 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
2 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Correct |
2 ms |
2688 KB |
Output is correct |
5 |
Correct |
2 ms |
2688 KB |
Output is correct |
6 |
Correct |
2 ms |
2688 KB |
Output is correct |
7 |
Correct |
3 ms |
2816 KB |
Output is correct |
8 |
Correct |
3 ms |
2816 KB |
Output is correct |
9 |
Correct |
2 ms |
2688 KB |
Output is correct |
10 |
Correct |
2 ms |
2688 KB |
Output is correct |
11 |
Correct |
3 ms |
2688 KB |
Output is correct |
12 |
Correct |
117 ms |
11656 KB |
Output is correct |
13 |
Correct |
138 ms |
16736 KB |
Output is correct |
14 |
Correct |
83 ms |
7620 KB |
Output is correct |
15 |
Correct |
110 ms |
7364 KB |
Output is correct |
16 |
Correct |
96 ms |
7416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
2 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Correct |
2 ms |
2688 KB |
Output is correct |
5 |
Correct |
2 ms |
2688 KB |
Output is correct |
6 |
Correct |
2 ms |
2688 KB |
Output is correct |
7 |
Correct |
3 ms |
2816 KB |
Output is correct |
8 |
Correct |
3 ms |
2816 KB |
Output is correct |
9 |
Correct |
2 ms |
2688 KB |
Output is correct |
10 |
Correct |
2 ms |
2688 KB |
Output is correct |
11 |
Correct |
3 ms |
2688 KB |
Output is correct |
12 |
Correct |
2 ms |
2688 KB |
Output is correct |
13 |
Correct |
3 ms |
2688 KB |
Output is correct |
14 |
Correct |
2 ms |
2688 KB |
Output is correct |
15 |
Correct |
2 ms |
2688 KB |
Output is correct |
16 |
Correct |
2 ms |
2688 KB |
Output is correct |
17 |
Correct |
3 ms |
2688 KB |
Output is correct |
18 |
Correct |
2 ms |
2688 KB |
Output is correct |
19 |
Correct |
2 ms |
2688 KB |
Output is correct |
20 |
Correct |
2 ms |
2688 KB |
Output is correct |
21 |
Correct |
3 ms |
2816 KB |
Output is correct |
22 |
Correct |
3 ms |
2816 KB |
Output is correct |
23 |
Correct |
2 ms |
2688 KB |
Output is correct |
24 |
Correct |
2 ms |
2688 KB |
Output is correct |
25 |
Correct |
2 ms |
2688 KB |
Output is correct |
26 |
Correct |
2 ms |
2688 KB |
Output is correct |
27 |
Correct |
3 ms |
2816 KB |
Output is correct |
28 |
Correct |
2 ms |
2688 KB |
Output is correct |
29 |
Correct |
3 ms |
2688 KB |
Output is correct |
30 |
Correct |
3 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
2 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Correct |
2 ms |
2688 KB |
Output is correct |
5 |
Correct |
2 ms |
2688 KB |
Output is correct |
6 |
Correct |
2 ms |
2688 KB |
Output is correct |
7 |
Correct |
3 ms |
2816 KB |
Output is correct |
8 |
Correct |
3 ms |
2816 KB |
Output is correct |
9 |
Correct |
2 ms |
2688 KB |
Output is correct |
10 |
Correct |
2 ms |
2688 KB |
Output is correct |
11 |
Correct |
3 ms |
2688 KB |
Output is correct |
12 |
Correct |
117 ms |
11656 KB |
Output is correct |
13 |
Correct |
138 ms |
16736 KB |
Output is correct |
14 |
Correct |
83 ms |
7620 KB |
Output is correct |
15 |
Correct |
110 ms |
7364 KB |
Output is correct |
16 |
Correct |
96 ms |
7416 KB |
Output is correct |
17 |
Correct |
2 ms |
2688 KB |
Output is correct |
18 |
Correct |
3 ms |
2688 KB |
Output is correct |
19 |
Correct |
2 ms |
2688 KB |
Output is correct |
20 |
Correct |
2 ms |
2688 KB |
Output is correct |
21 |
Correct |
2 ms |
2688 KB |
Output is correct |
22 |
Correct |
3 ms |
2688 KB |
Output is correct |
23 |
Correct |
2 ms |
2688 KB |
Output is correct |
24 |
Correct |
2 ms |
2688 KB |
Output is correct |
25 |
Correct |
2 ms |
2688 KB |
Output is correct |
26 |
Correct |
3 ms |
2816 KB |
Output is correct |
27 |
Correct |
3 ms |
2816 KB |
Output is correct |
28 |
Correct |
2 ms |
2688 KB |
Output is correct |
29 |
Correct |
2 ms |
2688 KB |
Output is correct |
30 |
Correct |
2 ms |
2688 KB |
Output is correct |
31 |
Correct |
2 ms |
2688 KB |
Output is correct |
32 |
Correct |
3 ms |
2816 KB |
Output is correct |
33 |
Correct |
2 ms |
2688 KB |
Output is correct |
34 |
Correct |
3 ms |
2688 KB |
Output is correct |
35 |
Correct |
3 ms |
2688 KB |
Output is correct |
36 |
Correct |
102 ms |
11640 KB |
Output is correct |
37 |
Correct |
104 ms |
16760 KB |
Output is correct |
38 |
Correct |
81 ms |
7540 KB |
Output is correct |
39 |
Correct |
118 ms |
7540 KB |
Output is correct |
40 |
Correct |
101 ms |
7584 KB |
Output is correct |
41 |
Correct |
104 ms |
14456 KB |
Output is correct |
42 |
Correct |
100 ms |
15608 KB |
Output is correct |
43 |
Correct |
63 ms |
7028 KB |
Output is correct |
44 |
Correct |
105 ms |
7416 KB |
Output is correct |
45 |
Correct |
94 ms |
7416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
2 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Correct |
2 ms |
2720 KB |
Output is correct |
5 |
Correct |
2 ms |
2688 KB |
Output is correct |
6 |
Correct |
2 ms |
2688 KB |
Output is correct |
7 |
Correct |
2 ms |
2688 KB |
Output is correct |
8 |
Correct |
2 ms |
2688 KB |
Output is correct |
9 |
Correct |
2 ms |
2688 KB |
Output is correct |
10 |
Correct |
2 ms |
2688 KB |
Output is correct |
11 |
Correct |
2 ms |
2688 KB |
Output is correct |
12 |
Correct |
2 ms |
2688 KB |
Output is correct |
13 |
Correct |
2 ms |
2688 KB |
Output is correct |
14 |
Correct |
3 ms |
2816 KB |
Output is correct |
15 |
Correct |
3 ms |
2816 KB |
Output is correct |
16 |
Correct |
2 ms |
2688 KB |
Output is correct |
17 |
Correct |
2 ms |
2688 KB |
Output is correct |
18 |
Correct |
3 ms |
2688 KB |
Output is correct |
19 |
Correct |
117 ms |
11656 KB |
Output is correct |
20 |
Correct |
138 ms |
16736 KB |
Output is correct |
21 |
Correct |
83 ms |
7620 KB |
Output is correct |
22 |
Correct |
110 ms |
7364 KB |
Output is correct |
23 |
Correct |
96 ms |
7416 KB |
Output is correct |
24 |
Correct |
2 ms |
2688 KB |
Output is correct |
25 |
Correct |
3 ms |
2688 KB |
Output is correct |
26 |
Correct |
2 ms |
2688 KB |
Output is correct |
27 |
Correct |
2 ms |
2688 KB |
Output is correct |
28 |
Correct |
2 ms |
2688 KB |
Output is correct |
29 |
Correct |
3 ms |
2688 KB |
Output is correct |
30 |
Correct |
2 ms |
2688 KB |
Output is correct |
31 |
Correct |
2 ms |
2688 KB |
Output is correct |
32 |
Correct |
2 ms |
2688 KB |
Output is correct |
33 |
Correct |
3 ms |
2816 KB |
Output is correct |
34 |
Correct |
3 ms |
2816 KB |
Output is correct |
35 |
Correct |
2 ms |
2688 KB |
Output is correct |
36 |
Correct |
2 ms |
2688 KB |
Output is correct |
37 |
Correct |
2 ms |
2688 KB |
Output is correct |
38 |
Correct |
2 ms |
2688 KB |
Output is correct |
39 |
Correct |
3 ms |
2816 KB |
Output is correct |
40 |
Correct |
2 ms |
2688 KB |
Output is correct |
41 |
Correct |
3 ms |
2688 KB |
Output is correct |
42 |
Correct |
3 ms |
2688 KB |
Output is correct |
43 |
Correct |
102 ms |
11640 KB |
Output is correct |
44 |
Correct |
104 ms |
16760 KB |
Output is correct |
45 |
Correct |
81 ms |
7540 KB |
Output is correct |
46 |
Correct |
118 ms |
7540 KB |
Output is correct |
47 |
Correct |
101 ms |
7584 KB |
Output is correct |
48 |
Correct |
104 ms |
14456 KB |
Output is correct |
49 |
Correct |
100 ms |
15608 KB |
Output is correct |
50 |
Correct |
63 ms |
7028 KB |
Output is correct |
51 |
Correct |
105 ms |
7416 KB |
Output is correct |
52 |
Correct |
94 ms |
7416 KB |
Output is correct |
53 |
Correct |
111 ms |
16788 KB |
Output is correct |
54 |
Correct |
98 ms |
14840 KB |
Output is correct |
55 |
Correct |
56 ms |
6644 KB |
Output is correct |
56 |
Correct |
96 ms |
11640 KB |
Output is correct |
57 |
Correct |
95 ms |
7416 KB |
Output is correct |
58 |
Correct |
94 ms |
7672 KB |
Output is correct |
59 |
Correct |
108 ms |
7416 KB |
Output is correct |
60 |
Correct |
100 ms |
7544 KB |
Output is correct |