Submission #1156285

#TimeUsernameProblemLanguageResultExecution timeMemory
1156285mmdrzadaStar Trek (CEOI20_startrek)C++20
8 / 100
1096 ms328 KiB
#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];


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);
}


bool dfs(int v, int p = -1) {
    if (v == exi && stat == false) return true;
    for(int neigh: adj[v]) {
        if (neigh == p) continue;
        if (dfs(neigh, v) == false) return true;
    }
    return false;
}

void solve() {
    cin >> n >> d;
    assert(n <= 1e3 && d <= 1e5);
    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 ++) {
        for(exi = 0; exi < n ; exi ++) {
            for(stat = 0; stat <= 1; stat++) {
                bool res = dfs(root);
                cnt[res][stat]++;
            }
        }
    }
    exi = -1;
    Mat base; base.n = 1, base.m = 2;
    for(int root = 0 ; root < n ; root ++) {
        base.mat[0][dfs(root)]++;
    }
    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;
    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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...