#include <bits/stdc++.h>
#define int long long
using namespace std;
#define fastIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define pii pair<int, int>
#define S second
#define F first
#define sep ' '
#define vi vector<int>
#define mid (l+r)/2
#define pbb pair<bool, bool>
#define trint array<int, 3>
const int N = 1e3+10, M = 1e9+7;
int n, d;
vi adj[N];
int exi, stat;
int cnt[2][2];
int dp[N][3][2];
int pf[N], sf[N];
int sz[N];
struct Mat {
int n, m;
int mat[5][5];
Mat () {
memset(mat, 0, sizeof mat);
}
};
Mat mul(Mat a, Mat b) {
assert(a.m == b.n);
Mat res;
res.n = a.n, res.m = b.m;
for(int i = 0 ; i < res.n ; i ++) {
for(int j = 0 ; j < res.m ; j ++) {
for(int k = 0 ; k < a.m ; k ++) {
res.mat[i][j] = (res.mat[i][j] + 1ll*a.mat[i][k]*b.mat[k][j]%M)%M;
}
}
}
return res;
}
Mat mat_pow(Mat x, int y) {
assert(x.n == x.m);
Mat res; res.n = x.n, res.m = x.m;
for(int i = 0 ; i < res.n ; i ++) res.mat[i][i] = 1;
while(y) {
if (y&1) {
res = mul(res, x);
}
y /= 2;
x = mul(x, x);
}
return res;
}
int my_pow(int x, int y) {
int res = 1;
while(y) {
if (y&1) res = 1ll*res*x%M;
y /= 2;
x = 1ll*x*x%M;
}
return res;
}
int inv(int x) {
return my_pow(x, M-2);
}
/**
* 0 -> nothing
* 1 -> lose sued
* 2 -> win used
*/
void dfs(int v, int p = -1) {
if (p != -1) adj[v].erase(find(adj[v].begin(), adj[v].end(), p));
dp[v][0][0] = 1;
dp[v][0][1] = 0;
sz[v]++;
for(int neigh: adj[v]) {
dfs(neigh, v);
sz[v] += sz[neigh];
dp[v][0][0] &= dp[neigh][0][1];
}
dp[v][0][1] = 1 - dp[v][0][0];
for(int i = 0 ; i < adj[v].size() ; i ++) {
pf[i] = (i == 0 ? 1 : pf[i-1]) & dp[adj[v][i]][0][1];
}
for(int i = adj[v].size()-1 ; i >= 0 ; i --) {
sf[i] = (i == adj[v].size()-1 ? 1 : sf[i+1]) & dp[adj[v][i]][0][1];
}
dp[v][2][0] += dp[v][0][0]; // add the win ticket for your self
for(int i = 0 ; i < adj[v].size() ; i ++) {
dp[v][1][0] += (i == 0 ? 1 : pf[i-1]) * dp[adj[v][i]][1][1] * (i == adj[v].size()-1 ? 1 : sf[i+1]);
dp[v][2][0] += (i == 0 ? 1 : pf[i-1]) * dp[adj[v][i]][2][1] * (i == adj[v].size()-1 ? 1 : sf[i+1]);
}
dp[v][1][1] = sz[v] - dp[v][1][0];
dp[v][2][1] = sz[v] - dp[v][2][0];
if (p != -1) adj[v].pb(p);
}
void solve() {
cin >> n >> d;
for(int i = 0 ; i < n-1 ; i ++) {
int u, v; cin >> u >> v;u --, v --; adj[u].pb(v), adj[v].pb(u);
}
for(int root = 0 ; root < n ; root ++) {
memset(dp, 0, sizeof dp);
memset(sz, 0, sizeof sz);
dfs(root);
cnt[1][0] += dp[root][1][1];
cnt[1][1] += dp[root][2][1];
cnt[0][0] += dp[root][1][0];
cnt[0][1] += dp[root][2][0];
for(int i = 0 ; i < 2 ; i++) {
for(int j = 0 ; j < 2 ; j ++) {
cnt[i][j] %= M;
}
}
}
exi = -1;
Mat base; base.n = 1, base.m = 2;
for(int root = 0 ; root < n ; root ++) {
memset(dp, 0, sizeof dp);
memset(sz, 0, sizeof sz);
dfs(root);
base.mat[0][dp[root][0][1]]++;
}
Mat Z; Z.n = Z.m = 2;
for(int i = 0 ; i < 2 ; i ++) {
for(int j = 0 ; j < 2 ; j ++) {
Z.mat[j][i] = cnt[i][j];
}
}
base = mul(base, mat_pow(Z, d-1));
int ans = 0;
memset(dp, 0, sizeof dp);
memset(sz, 0, sizeof sz);
dfs(0);
ans += 1ll * base.mat[0][0] * (dp[0][1][1]) % M + 1ll * base.mat[0][1] * dp[0][2][1] % M;
ans %= M;
// for(exi = 0; exi < n ; exi ++) {
// for(stat = 0; stat <= 1; stat++) {
// if (dfs(0)) {
// ans += base.mat[0][stat];
// ans %= M;
// }
// }
// }
cout << ans << endl;
}
int32_t main() {
fastIO;
solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |