#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 binp(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 sub(int a, int b) {
return a >= b ? a - b : a - b + mod;
}
int inv(int a) {
return binp(a, mod - 2);
}
vector<int> g[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
ll d;
cin >> n >> d;
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);
}
vector<bool> dp(n);
vector<int> cntLoosing(n), c(n);
function<void(int, int)> dfs1 = [&](int v, int p) {
dp[v] = false;
cntLoosing[v] = 0;
int total = 0, only_loosing = 0;
for (int to: g[v]) {
if (to != p) {
dfs1(to, v);
if (!dp[to]) {
only_loosing += c[to];
++cntLoosing[v];
dp[v] = true;
}
total += c[to];
}
}
if (cntLoosing[v] > 1) {
c[v] = 0;
} else if (cntLoosing[v] == 1) {
c[v] = only_loosing;
} else {
c[v] = total + 1;
}
};
function<void(int, int)> dfs2 = [&](int v, int p) {
int total = c[v];
// for (int to : g[v]) {
// if (to != p) {
// total += c[to];
// }
// }
dp[v] = cntLoosing[v];
for (int to: g[v]) {
if (to != p) {
bool new_dp = true;
if (dp[to]) {
new_dp = dp[v];
} else if (cntLoosing[v] == 1) {
new_dp = false;
}
cntLoosing[to] += !new_dp;
if (cntLoosing[to] > 1) {
c[to] = 0;
} else if (cntLoosing[to] == 1) {
if (!new_dp) {
c[to] = total - c[v] + dp[v];
}
} else {
c[to] = total;
}
dfs2(to, v);
}
}
};
dfs1(0, -1);
dfs2(0, -1);
int E = 0, L = 0;
for (int i = 0; i < n; ++i) {
if (dp[i]) {
E = add(E, c[i]);
} else {
L += 1;
E = sub(E, c[i]);
}
// cout << dp[i] << " ";
}
// cout << endl;
int a = mul(n, n);
int x = mul(L, mul(sub(binp(a, d), binp(E, d)), inv(sub(a, E))));
int ans;
if (dp[0]) {
ans = mul(x, c[0]);
} else {
ans = sub(binp(a, d), mul(x, c[0]));
}
cout << sub(binp(a, d), ans);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 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 |
2772 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 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 |
2772 KB |
Output is correct |
9 |
Correct |
2 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 |
75 ms |
12364 KB |
Output is correct |
13 |
Correct |
64 ms |
19020 KB |
Output is correct |
14 |
Correct |
44 ms |
6792 KB |
Output is correct |
15 |
Correct |
69 ms |
6656 KB |
Output is correct |
16 |
Correct |
61 ms |
6652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 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 |
2772 KB |
Output is correct |
9 |
Correct |
2 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 |
2 ms |
2644 KB |
Output is correct |
13 |
Incorrect |
3 ms |
2644 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 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 |
2772 KB |
Output is correct |
9 |
Correct |
2 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 |
75 ms |
12364 KB |
Output is correct |
13 |
Correct |
64 ms |
19020 KB |
Output is correct |
14 |
Correct |
44 ms |
6792 KB |
Output is correct |
15 |
Correct |
69 ms |
6656 KB |
Output is correct |
16 |
Correct |
61 ms |
6652 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Incorrect |
3 ms |
2644 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |