#include <bits/stdc++.h>
#define lli long long int
#define ld long double
#define pb push_back
#define MP make_pair
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define REP(i, n) for(int i = 0; (i) < (n); (i)++)
using namespace std;
void fastio() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
const int N = 1e5 + 5;
const int INF = 1e9 + 500;
const int MOD = 1e9 + 7;
const int B = 2;
int add(int x, int y) {
if(x + y >= MOD) return x + y - MOD;
return x + y;
}
int mult(int x, int y) {
return (int)((1ll * x * y) % MOD);
}
int subt(int x, int y) {
if(x - y < 0) return x - y + MOD;
return x - y;
}
int fp(int x, lli y) {
int ret = 1;
while(y > 0ll) {
if(y & 1ll) {
ret = mult(ret, x);
}
x = mult(x, x);
y /= 2ll;
}
return ret;
}
struct Matrix {
array<array<int, B>, B> mat;
Matrix() {
REP(i, B) REP(j, B) mat[i][j] = 0;
}
void print() {
REP(i, B) {
REP(j, B) {
cout << mat[i][j] << " ";
}
cout << "\n";
}
cout << "\n\n";
}
};
Matrix mult(Matrix x, Matrix y) {
Matrix ret;
REP(i, 2) REP(j, 2) REP(k, 2) {
ret.mat[i][j] = add(ret.mat[i][j], mult(x.mat[i][k], y.mat[k][j]));
}
return ret;
}
array<int, B> mult(array<int, B> x, Matrix y) {
array<int, B> ret = {0, 0};
REP(i, 2) {
REP(j, 2) {
ret[j] = add(ret[j], mult(y.mat[i][j], x[i]));
}
}
return ret;
}
Matrix fp(Matrix x, lli y) {
Matrix ret;
REP(i, B) ret.mat[i][i] = 1;
while(y > 0ll) {
if(y & 1) {
ret = mult(ret, x);
}
y /= 2ll;
x = mult(x, x);
}
return ret;
}
int n;
lli d;
vector<vector<int> > adj(N, vector<int>());
vector<int> dp(N, 0), dpr(N, 0);
vector<int> dpcrit(N, 0), dpc(N, 0);
vector<int> wc(N, 0), lc(N, 0);
int L = 0;
int CL = 0, CW = 0;
void dfs1(int node, int par) {
for(auto c : adj[node]) {
if(c == par) continue;
dfs1(c, node);
if(!dpr[c]) dpr[node]++;
}
}
void dfs2(int node, int par) {
if(!dpr[node]) {
dpcrit[node] = 1;
}
for(auto c : adj[node]) {
if(c == par) continue;
dfs2(c, node);
if(dpr[c]) wc[node] += dpcrit[c];
else lc[node] += dpcrit[c];
if(dpr[node] == 1 && !dpr[c]) {
dpcrit[node] += dpcrit[c];
}
if(!dpr[node] && dpr[c]) {
dpcrit[node] += dpcrit[c];
}
}
}
void change_root(int p, int x) {
if(!dpr[x]) {
dpr[p]--;
lc[p] -= dpcrit[x];
}
else {
wc[p] -= dpcrit[x];
}
if(dpr[p] >= 2) {
dpcrit[p] = 0;
}
else if(dpr[p] == 1) {
dpcrit[p] = lc[p];
}
else {
dpcrit[p] = wc[p] + 1;
}
if(!dpr[p]) {
dpr[x]++;
lc[x] += dpcrit[p];
}
else {
wc[x] += dpcrit[p];
}
if(dpr[x] >= 2) {
dpcrit[x] = 0;
}
else if(dpr[x] == 1) {
dpcrit[x] = lc[x];
}
else {
dpcrit[x] = wc[x] + 1;
}
}
void reroot(int node, int par) {
dp[node] = dpr[node];
dpc[node] = dpcrit[node];
for(auto c : adj[node]) {
if(c == par) continue;
change_root(node, c);
reroot(c, node);
change_root(c, node);
}
}
void solve() {
cin >> n >> d;
REP(i, n - 1) {
int u, v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
dfs1(1, 0);
dfs2(1, 0);
// for(int i = 1; i <= n; i++) {
// cout << "i:" << i << " dpr:" << dpr[i] << " crit:" << dpcrit[i] << "\n";
// }
reroot(1, 0);
// for(int i = 1; i <= n; i++) {
// cout << "i:" << i << " dp:" << dp[i] << " c:" << dpc[i] << "\n";
// }
for(int i = 1; i <= n; i++) {
if(!dp[i]) {
L++;
CL = add(CL, dpc[i]);
}
else {
CW = add(CW, dpc[i]);
}
}
// cout << "L:" << L << " CL:" << CL << " CW:" << CW << "\n";
// int LD = L, LDN = 0;
// for(int i = 1; i < d; i++) {
// LDN = add(mult(LD, subt(CW, CL)), mult(L, fp(n, 2ll * i)));
// swap(LD, LDN);
// }
Matrix rel;
rel.mat[0] = {subt(CW, CL), 0};
rel.mat[1] = {1, mult(n, n)};
array<int, B> st = {L, mult(L, mult(n, n))};
// rel.print();
st = mult(st, fp(rel, d - 1));
// REP(i, B) {
// cout << st[i] << " ";
// }
// cout << " AAA\n";
int ans = 0;
if(dp[1]) {
ans = mult(dpc[1], st[0]);
}
else {
ans = subt(fp(n, 2ll * d), mult(dpc[1], st[0]));
}
ans = subt(fp(n, 2ll * d), ans);
cout << ans << "\n";
}
signed main() {
fastio();
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
3 ms |
4956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
3 ms |
4956 KB |
Output is correct |
3 |
Correct |
3 ms |
4952 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4956 KB |
Output is correct |
2 |
Correct |
3 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
3 ms |
5208 KB |
Output is correct |
6 |
Correct |
3 ms |
4976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4956 KB |
Output is correct |
2 |
Correct |
3 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
3 ms |
5208 KB |
Output is correct |
6 |
Correct |
3 ms |
4976 KB |
Output is correct |
7 |
Correct |
3 ms |
5212 KB |
Output is correct |
8 |
Correct |
3 ms |
5212 KB |
Output is correct |
9 |
Correct |
3 ms |
4956 KB |
Output is correct |
10 |
Correct |
3 ms |
4956 KB |
Output is correct |
11 |
Correct |
3 ms |
4956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4956 KB |
Output is correct |
2 |
Correct |
3 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
3 ms |
5208 KB |
Output is correct |
6 |
Correct |
3 ms |
4976 KB |
Output is correct |
7 |
Correct |
3 ms |
5212 KB |
Output is correct |
8 |
Correct |
3 ms |
5212 KB |
Output is correct |
9 |
Correct |
3 ms |
4956 KB |
Output is correct |
10 |
Correct |
3 ms |
4956 KB |
Output is correct |
11 |
Correct |
3 ms |
4956 KB |
Output is correct |
12 |
Correct |
39 ms |
11044 KB |
Output is correct |
13 |
Correct |
46 ms |
14420 KB |
Output is correct |
14 |
Correct |
28 ms |
8404 KB |
Output is correct |
15 |
Correct |
38 ms |
8592 KB |
Output is correct |
16 |
Correct |
41 ms |
8284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4956 KB |
Output is correct |
2 |
Correct |
3 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
3 ms |
5208 KB |
Output is correct |
6 |
Correct |
3 ms |
4976 KB |
Output is correct |
7 |
Correct |
3 ms |
5212 KB |
Output is correct |
8 |
Correct |
3 ms |
5212 KB |
Output is correct |
9 |
Correct |
3 ms |
4956 KB |
Output is correct |
10 |
Correct |
3 ms |
4956 KB |
Output is correct |
11 |
Correct |
3 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
3 ms |
5172 KB |
Output is correct |
14 |
Correct |
4 ms |
4956 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
3 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
5176 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
3 ms |
4956 KB |
Output is correct |
20 |
Correct |
3 ms |
4956 KB |
Output is correct |
21 |
Correct |
3 ms |
5180 KB |
Output is correct |
22 |
Correct |
3 ms |
5212 KB |
Output is correct |
23 |
Correct |
3 ms |
4956 KB |
Output is correct |
24 |
Correct |
3 ms |
4956 KB |
Output is correct |
25 |
Correct |
3 ms |
4956 KB |
Output is correct |
26 |
Correct |
3 ms |
5212 KB |
Output is correct |
27 |
Correct |
3 ms |
5212 KB |
Output is correct |
28 |
Correct |
3 ms |
5364 KB |
Output is correct |
29 |
Correct |
3 ms |
4956 KB |
Output is correct |
30 |
Correct |
3 ms |
4956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4956 KB |
Output is correct |
2 |
Correct |
3 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
3 ms |
5208 KB |
Output is correct |
6 |
Correct |
3 ms |
4976 KB |
Output is correct |
7 |
Correct |
3 ms |
5212 KB |
Output is correct |
8 |
Correct |
3 ms |
5212 KB |
Output is correct |
9 |
Correct |
3 ms |
4956 KB |
Output is correct |
10 |
Correct |
3 ms |
4956 KB |
Output is correct |
11 |
Correct |
3 ms |
4956 KB |
Output is correct |
12 |
Correct |
39 ms |
11044 KB |
Output is correct |
13 |
Correct |
46 ms |
14420 KB |
Output is correct |
14 |
Correct |
28 ms |
8404 KB |
Output is correct |
15 |
Correct |
38 ms |
8592 KB |
Output is correct |
16 |
Correct |
41 ms |
8284 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
3 ms |
5172 KB |
Output is correct |
19 |
Correct |
4 ms |
4956 KB |
Output is correct |
20 |
Correct |
2 ms |
4956 KB |
Output is correct |
21 |
Correct |
3 ms |
4956 KB |
Output is correct |
22 |
Correct |
2 ms |
5176 KB |
Output is correct |
23 |
Correct |
2 ms |
4956 KB |
Output is correct |
24 |
Correct |
3 ms |
4956 KB |
Output is correct |
25 |
Correct |
3 ms |
4956 KB |
Output is correct |
26 |
Correct |
3 ms |
5180 KB |
Output is correct |
27 |
Correct |
3 ms |
5212 KB |
Output is correct |
28 |
Correct |
3 ms |
4956 KB |
Output is correct |
29 |
Correct |
3 ms |
4956 KB |
Output is correct |
30 |
Correct |
3 ms |
4956 KB |
Output is correct |
31 |
Correct |
3 ms |
5212 KB |
Output is correct |
32 |
Correct |
3 ms |
5212 KB |
Output is correct |
33 |
Correct |
3 ms |
5364 KB |
Output is correct |
34 |
Correct |
3 ms |
4956 KB |
Output is correct |
35 |
Correct |
3 ms |
4956 KB |
Output is correct |
36 |
Correct |
50 ms |
12112 KB |
Output is correct |
37 |
Correct |
51 ms |
15600 KB |
Output is correct |
38 |
Correct |
35 ms |
9448 KB |
Output is correct |
39 |
Correct |
37 ms |
9536 KB |
Output is correct |
40 |
Correct |
36 ms |
9308 KB |
Output is correct |
41 |
Correct |
41 ms |
13916 KB |
Output is correct |
42 |
Correct |
43 ms |
14684 KB |
Output is correct |
43 |
Correct |
25 ms |
8920 KB |
Output is correct |
44 |
Correct |
44 ms |
9560 KB |
Output is correct |
45 |
Correct |
38 ms |
9296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
3 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
3 ms |
4956 KB |
Output is correct |
5 |
Correct |
3 ms |
4952 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
3 ms |
4956 KB |
Output is correct |
9 |
Correct |
3 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
3 ms |
5208 KB |
Output is correct |
13 |
Correct |
3 ms |
4976 KB |
Output is correct |
14 |
Correct |
3 ms |
5212 KB |
Output is correct |
15 |
Correct |
3 ms |
5212 KB |
Output is correct |
16 |
Correct |
3 ms |
4956 KB |
Output is correct |
17 |
Correct |
3 ms |
4956 KB |
Output is correct |
18 |
Correct |
3 ms |
4956 KB |
Output is correct |
19 |
Correct |
39 ms |
11044 KB |
Output is correct |
20 |
Correct |
46 ms |
14420 KB |
Output is correct |
21 |
Correct |
28 ms |
8404 KB |
Output is correct |
22 |
Correct |
38 ms |
8592 KB |
Output is correct |
23 |
Correct |
41 ms |
8284 KB |
Output is correct |
24 |
Correct |
2 ms |
4956 KB |
Output is correct |
25 |
Correct |
3 ms |
5172 KB |
Output is correct |
26 |
Correct |
4 ms |
4956 KB |
Output is correct |
27 |
Correct |
2 ms |
4956 KB |
Output is correct |
28 |
Correct |
3 ms |
4956 KB |
Output is correct |
29 |
Correct |
2 ms |
5176 KB |
Output is correct |
30 |
Correct |
2 ms |
4956 KB |
Output is correct |
31 |
Correct |
3 ms |
4956 KB |
Output is correct |
32 |
Correct |
3 ms |
4956 KB |
Output is correct |
33 |
Correct |
3 ms |
5180 KB |
Output is correct |
34 |
Correct |
3 ms |
5212 KB |
Output is correct |
35 |
Correct |
3 ms |
4956 KB |
Output is correct |
36 |
Correct |
3 ms |
4956 KB |
Output is correct |
37 |
Correct |
3 ms |
4956 KB |
Output is correct |
38 |
Correct |
3 ms |
5212 KB |
Output is correct |
39 |
Correct |
3 ms |
5212 KB |
Output is correct |
40 |
Correct |
3 ms |
5364 KB |
Output is correct |
41 |
Correct |
3 ms |
4956 KB |
Output is correct |
42 |
Correct |
3 ms |
4956 KB |
Output is correct |
43 |
Correct |
50 ms |
12112 KB |
Output is correct |
44 |
Correct |
51 ms |
15600 KB |
Output is correct |
45 |
Correct |
35 ms |
9448 KB |
Output is correct |
46 |
Correct |
37 ms |
9536 KB |
Output is correct |
47 |
Correct |
36 ms |
9308 KB |
Output is correct |
48 |
Correct |
41 ms |
13916 KB |
Output is correct |
49 |
Correct |
43 ms |
14684 KB |
Output is correct |
50 |
Correct |
25 ms |
8920 KB |
Output is correct |
51 |
Correct |
44 ms |
9560 KB |
Output is correct |
52 |
Correct |
38 ms |
9296 KB |
Output is correct |
53 |
Correct |
51 ms |
15444 KB |
Output is correct |
54 |
Correct |
49 ms |
14164 KB |
Output is correct |
55 |
Correct |
22 ms |
8408 KB |
Output is correct |
56 |
Correct |
38 ms |
12116 KB |
Output is correct |
57 |
Correct |
48 ms |
9548 KB |
Output is correct |
58 |
Correct |
43 ms |
9308 KB |
Output is correct |
59 |
Correct |
35 ms |
9436 KB |
Output is correct |
60 |
Correct |
37 ms |
9400 KB |
Output is correct |