#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 100000, mod = 1e9 + 7;
int add(int a, int b) {
return a + b < mod ? a + b : a + b - mod;
}
int mul(int a, int b) {
return a * (ll) b % mod;
}
int binpow(int a, ll p) {
int ans = 1;
for (; p > 0; p >>= 1, a = mul(a, a)) {
if (p & 1) {
ans = mul(ans, a);
}
}
return ans;
}
int n;
ll d;
vector<int> g[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> d;
if (n == 2) {
cout << binpow(4, d);
return 0;
}
for (int i = 1; i < n; ++i) {
int a, b;
cin >> a >> b;
--a, --b;
g[a].push_back(b);
g[b].push_back(a);
}
if (d == 1) {
vector<bool> canWin(n), whatIf(n);
vector<int> cntLoosing(n);
function<void(int, int)> dfs1 = [&](int v, int p) {
canWin[v] = false;
for (int to: g[v]) {
if (to != p) {
dfs1(to, v);
if (!canWin[to]) {
canWin[v] = true;
}
}
}
};
dfs1(0, -1);
function<void(int, int)> dfs2 = [&](int v, int p) {
cntLoosing[v] = 0;
for (int to: g[v]) {
if (to != p) {
cntLoosing[v] += !canWin[to];
}
}
if (p == -1) {
whatIf[v] = true;
} else {
if (canWin[v]) {
if (!canWin[p]) {
whatIf[v] = whatIf[p];
}
} else {
if (cntLoosing[p] == 1) {
whatIf[v] = whatIf[p];
}
}
}
for (int to: g[v]) {
if (to != p) {
dfs2(to, v);
}
}
};
dfs2(0, -1);
auto sub = canWin;
function<void(int, int)> dfs3 = [&](int v, int p) {
cntLoosing[v] = 0;
for (int to: g[v]) {
if (to != p) {
cntLoosing[v] += !canWin[to];
} else if (!canWin[p] || cntLoosing[p] == 1 && !canWin[v]) {
cntLoosing[v] += 1;
}
}
canWin[v] = cntLoosing[v];
for (int to: g[v]) {
if (to != p) {
dfs3(to, v);
}
}
};
dfs3(0, -1);
int cntWin = count(canWin.begin(), canWin.end(), true);
int ans = canWin[0] * mul(cntWin, n);
if (sub[0]) {
for (int i = 0; i < n; ++i) {
if (!sub[i] && !whatIf[i] || sub[i]) {
ans = add(ans, n - cntWin);
}
}
} else {
for (int i = 0; i < n; ++i) {
if (!sub[i] && whatIf[i]) {
ans = add(ans, n - cntWin);
}
}
}
cout << ans;
}
return 0;
}
Compilation message
startrek.cpp: In lambda function:
startrek.cpp:101:61: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
101 | } else if (!canWin[p] || cntLoosing[p] == 1 && !canWin[v]) {
startrek.cpp: In function 'int main()':
startrek.cpp:119:29: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
119 | if (!sub[i] && !whatIf[i] || sub[i]) {
| ~~~~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2672 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2808 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2676 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2808 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2676 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
69 ms |
13064 KB |
Output is correct |
13 |
Correct |
74 ms |
19864 KB |
Output is correct |
14 |
Correct |
46 ms |
7380 KB |
Output is correct |
15 |
Correct |
68 ms |
7436 KB |
Output is correct |
16 |
Correct |
64 ms |
7372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2808 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2676 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2808 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2676 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
69 ms |
13064 KB |
Output is correct |
13 |
Correct |
74 ms |
19864 KB |
Output is correct |
14 |
Correct |
46 ms |
7380 KB |
Output is correct |
15 |
Correct |
68 ms |
7436 KB |
Output is correct |
16 |
Correct |
64 ms |
7372 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2672 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |