#include <bits/stdc++.h>
#define C 1
#define P 0
#define debug(x) cerr << #x << " " << x << "\n"
#define debugs(x) cerr << #x << " " << x << " "
using namespace std;
typedef long long ll;
typedef pair <ll, ll> pii;
typedef pair <long double, pii> muchie;
const ll NMAX = 100001;
const ll VMAX = 101;
const ll INF = (1LL << 60);
const ll MOD = 1000000007;
const ll BLOCK = 447;
const ll base = 117;
const ll nr_of_bits = 18;
const ll inv2 = 500000004;
vector <ll> v[NMAX];
ll col[NMAX];
ll children[NMAX][2];
ll maximal[NMAX];
ll rosii[NMAX];
ll criticals[NMAX];
ll adv[NMAX];
ll sumR[NMAX][2];
void DFS(ll node, ll p) {
col[node] = P;
for(auto x : v[node]) {
if(x == p) continue;
DFS(x, node);
children[node][col[x]]++;
}
if(children[node][C] == (ll)v[node].size() - (p != 0)) {
col[node] = P;
} else {
col[node] = C;
}
if(col[node] == P)
rosii[node] = 1;
for(auto x : v[node]) {
if(x == p) continue;
if(col[node] != col[x] && children[node][C] >= (ll)v[node].size() - (p != 0) - 1) {
rosii[node] += rosii[x];
}
sumR[node][col[x]] += rosii[x];
}
}
ll perdanti = 0;
void bagaR(int node, int p) {
rosii[node] = (col[node] == P);
if(children[node][C] >= (ll)v[node].size() - (p != 0) - 1) { /// mereu facem bagaR pentru root
rosii[node] += sumR[node][P + C - col[node]];
}
}
void Rerooting(ll node, ll p) {
adv[node] = col[node];
criticals[node] = rosii[node];
if(adv[node] == P)
perdanti++;
for(auto x : v[node]) {
if(x == p) continue;
ll cx = col[x], cnode = col[node];
children[node][col[x]]--;
sumR[node][col[x]] -= rosii[x];
if(children[node][C] == v[node].size() - 1) {
col[node] = P;
} else {
col[node] = C;
}
bagaR(node, x);
children[x][col[node]]++;
sumR[x][col[node]] += rosii[node];
if(children[x][C] == v[x].size()) {
col[x] = P;
} else {
col[x] = C;
}
bagaR(x, 0);
Rerooting(x, node);
children[x][col[node]]--;
sumR[x][col[node]] -= rosii[node];
col[x] = cx;
bagaR(x, node);
col[node] = cnode;
children[node][col[x]]++;
sumR[node][col[x]] += rosii[x];
bagaR(node, 0);
}
}
ll lgput(ll n, ll p) {
ll r = 1;
while(p) {
if(p % 2) {
r *= n;
r %= MOD;
}
n *= n;
n %= MOD;
p /= 2;
}
return r;
}
void add(ll &x, ll val) {
x += val;
if(x >= MOD)
x -= MOD;
if(x < 0)
x += MOD;
}
ll L[NMAX];
ll mat[2][2];
void inmulteste(ll p) {
p--;
ll cv[2][2];
cv[0][0] = mat[0][0];
cv[0][1] = mat[0][1];
cv[1][0] = mat[1][0];
cv[1][1] = mat[1][1];
while(p) {
if(p % 2) {
ll rez[2][2];
rez[0][0] = rez[0][1] = rez[1][0] = rez[1][1] = 0;
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
for(int k = 0; k < 2; k++) {
add(rez[i][j], (mat[i][k] * cv[k][j]) % MOD);
}
}
}
cv[0][0] = rez[0][0];
cv[0][1] = rez[0][1];
cv[1][0] = rez[1][0];
cv[1][1] = rez[1][1];
}
ll rez[2][2];
rez[0][0] = rez[0][1] = rez[1][0] = rez[1][1] = 0;
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
for(int k = 0; k < 2; k++) {
add(rez[i][j], (mat[i][k] * mat[k][j]) % MOD);
}
}
}
mat[0][0] = rez[0][0];
mat[0][1] = rez[0][1];
mat[1][0] = rez[1][0];
mat[1][1] = rez[1][1];
p /= 2;
}
mat[0][0] = cv[0][0];
mat[0][1] = cv[0][1];
mat[1][0] = cv[1][0];
mat[1][1] = cv[1][1];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll n, d, i, criticalDif = 0;
cin >> n >> d;
if(n == 2) {
cout << lgput(4, d);
return 0;
}
for(i = 1; i < n; i++) {
ll a, b;
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
DFS(2, 0);
Rerooting(2, 0);
for(i = 1; i <= n; i++) {
if(adv[i] == P) {
add(criticalDif, -criticals[i]);
} else {
add(criticalDif, criticals[i]);
}
}
ll B = criticalDif;
ll A = 1;
/// L0 = perdanti
/// L1 = A + L0 * B
/// Li = A + Li-1 * B
L[0] = perdanti;
A = 1;
mat[0][0] = B;
mat[0][1] = (n * n) % MOD * perdanti % MOD;
mat[1][0] = 0;
mat[1][1] = (n * n) % MOD;
ll LP = L[0];
if(d > 1){
inmulteste(d - 1);
LP = (mat[0][0] * L[0] % MOD + mat[0][1] * A) % MOD; /// Asta e L[d - 1]
}
ll schimba = (LP * criticals[1]) % MOD;
if(adv[1] == P) {
cout << schimba;
} else {
cout << (lgput(n, 2 * d) - schimba + MOD) % MOD;
}
return 0;
}
Compilation message
startrek.cpp: In function 'void Rerooting(ll, ll)':
startrek.cpp:72:30: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | if(children[node][C] == v[node].size() - 1) {
| ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
startrek.cpp:80:27: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | if(children[x][C] == v[x].size()) {
| ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2708 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2708 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2696 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
2 ms |
2772 KB |
Output is correct |
10 |
Correct |
3 ms |
2772 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2708 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2696 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
2 ms |
2772 KB |
Output is correct |
10 |
Correct |
3 ms |
2772 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 KB |
Output is correct |
12 |
Correct |
112 ms |
22196 KB |
Output is correct |
13 |
Correct |
103 ms |
20940 KB |
Output is correct |
14 |
Correct |
56 ms |
12444 KB |
Output is correct |
15 |
Correct |
74 ms |
12396 KB |
Output is correct |
16 |
Correct |
70 ms |
12544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2708 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2696 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
2 ms |
2772 KB |
Output is correct |
10 |
Correct |
3 ms |
2772 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 KB |
Output is correct |
12 |
Correct |
1 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2772 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
3 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
1 ms |
2644 KB |
Output is correct |
20 |
Correct |
1 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2772 KB |
Output is correct |
22 |
Correct |
2 ms |
2772 KB |
Output is correct |
23 |
Correct |
2 ms |
2772 KB |
Output is correct |
24 |
Correct |
2 ms |
2772 KB |
Output is correct |
25 |
Correct |
2 ms |
2772 KB |
Output is correct |
26 |
Correct |
2 ms |
2772 KB |
Output is correct |
27 |
Correct |
2 ms |
2868 KB |
Output is correct |
28 |
Correct |
2 ms |
2772 KB |
Output is correct |
29 |
Correct |
3 ms |
2772 KB |
Output is correct |
30 |
Correct |
2 ms |
2772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2708 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2696 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
2 ms |
2772 KB |
Output is correct |
10 |
Correct |
3 ms |
2772 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 KB |
Output is correct |
12 |
Correct |
112 ms |
22196 KB |
Output is correct |
13 |
Correct |
103 ms |
20940 KB |
Output is correct |
14 |
Correct |
56 ms |
12444 KB |
Output is correct |
15 |
Correct |
74 ms |
12396 KB |
Output is correct |
16 |
Correct |
70 ms |
12544 KB |
Output is correct |
17 |
Correct |
1 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2772 KB |
Output is correct |
19 |
Correct |
2 ms |
2644 KB |
Output is correct |
20 |
Correct |
2 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2644 KB |
Output is correct |
22 |
Correct |
3 ms |
2644 KB |
Output is correct |
23 |
Correct |
2 ms |
2644 KB |
Output is correct |
24 |
Correct |
1 ms |
2644 KB |
Output is correct |
25 |
Correct |
1 ms |
2644 KB |
Output is correct |
26 |
Correct |
2 ms |
2772 KB |
Output is correct |
27 |
Correct |
2 ms |
2772 KB |
Output is correct |
28 |
Correct |
2 ms |
2772 KB |
Output is correct |
29 |
Correct |
2 ms |
2772 KB |
Output is correct |
30 |
Correct |
2 ms |
2772 KB |
Output is correct |
31 |
Correct |
2 ms |
2772 KB |
Output is correct |
32 |
Correct |
2 ms |
2868 KB |
Output is correct |
33 |
Correct |
2 ms |
2772 KB |
Output is correct |
34 |
Correct |
3 ms |
2772 KB |
Output is correct |
35 |
Correct |
2 ms |
2772 KB |
Output is correct |
36 |
Correct |
112 ms |
22216 KB |
Output is correct |
37 |
Correct |
100 ms |
20840 KB |
Output is correct |
38 |
Correct |
67 ms |
12344 KB |
Output is correct |
39 |
Correct |
68 ms |
12420 KB |
Output is correct |
40 |
Correct |
69 ms |
12496 KB |
Output is correct |
41 |
Correct |
90 ms |
16748 KB |
Output is correct |
42 |
Correct |
106 ms |
22012 KB |
Output is correct |
43 |
Correct |
53 ms |
11256 KB |
Output is correct |
44 |
Correct |
67 ms |
12492 KB |
Output is correct |
45 |
Correct |
68 ms |
12340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2772 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2708 KB |
Output is correct |
9 |
Correct |
1 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
1 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2696 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Correct |
2 ms |
2772 KB |
Output is correct |
16 |
Correct |
2 ms |
2772 KB |
Output is correct |
17 |
Correct |
3 ms |
2772 KB |
Output is correct |
18 |
Correct |
2 ms |
2772 KB |
Output is correct |
19 |
Correct |
112 ms |
22196 KB |
Output is correct |
20 |
Correct |
103 ms |
20940 KB |
Output is correct |
21 |
Correct |
56 ms |
12444 KB |
Output is correct |
22 |
Correct |
74 ms |
12396 KB |
Output is correct |
23 |
Correct |
70 ms |
12544 KB |
Output is correct |
24 |
Correct |
1 ms |
2644 KB |
Output is correct |
25 |
Correct |
2 ms |
2772 KB |
Output is correct |
26 |
Correct |
2 ms |
2644 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
2 ms |
2644 KB |
Output is correct |
29 |
Correct |
3 ms |
2644 KB |
Output is correct |
30 |
Correct |
2 ms |
2644 KB |
Output is correct |
31 |
Correct |
1 ms |
2644 KB |
Output is correct |
32 |
Correct |
1 ms |
2644 KB |
Output is correct |
33 |
Correct |
2 ms |
2772 KB |
Output is correct |
34 |
Correct |
2 ms |
2772 KB |
Output is correct |
35 |
Correct |
2 ms |
2772 KB |
Output is correct |
36 |
Correct |
2 ms |
2772 KB |
Output is correct |
37 |
Correct |
2 ms |
2772 KB |
Output is correct |
38 |
Correct |
2 ms |
2772 KB |
Output is correct |
39 |
Correct |
2 ms |
2868 KB |
Output is correct |
40 |
Correct |
2 ms |
2772 KB |
Output is correct |
41 |
Correct |
3 ms |
2772 KB |
Output is correct |
42 |
Correct |
2 ms |
2772 KB |
Output is correct |
43 |
Correct |
112 ms |
22216 KB |
Output is correct |
44 |
Correct |
100 ms |
20840 KB |
Output is correct |
45 |
Correct |
67 ms |
12344 KB |
Output is correct |
46 |
Correct |
68 ms |
12420 KB |
Output is correct |
47 |
Correct |
69 ms |
12496 KB |
Output is correct |
48 |
Correct |
90 ms |
16748 KB |
Output is correct |
49 |
Correct |
106 ms |
22012 KB |
Output is correct |
50 |
Correct |
53 ms |
11256 KB |
Output is correct |
51 |
Correct |
67 ms |
12492 KB |
Output is correct |
52 |
Correct |
68 ms |
12340 KB |
Output is correct |
53 |
Correct |
76 ms |
19680 KB |
Output is correct |
54 |
Correct |
78 ms |
23116 KB |
Output is correct |
55 |
Correct |
41 ms |
11240 KB |
Output is correct |
56 |
Correct |
80 ms |
19204 KB |
Output is correct |
57 |
Correct |
84 ms |
13772 KB |
Output is correct |
58 |
Correct |
76 ms |
13812 KB |
Output is correct |
59 |
Correct |
67 ms |
13444 KB |
Output is correct |
60 |
Correct |
71 ms |
13520 KB |
Output is correct |