output
stringlengths 52
181k
| instruction
stringlengths 296
182k
|
---|---|
#include <bits/stdc++.h>
using namespace std;
long long ans;
int dp[300000 + 5][20], n;
vector<int> e[300000 + 5];
inline void upd(int &x, int y) {
if (x < y) x = y;
}
int dfs(int now, int fa) {
int mxdep = 0;
for (auto v : e[now])
if (v != fa) upd(mxdep, dfs(v, now));
mxdep++;
ans += mxdep;
dp[now][1] = n;
for (int m = 2; m <= 19; m++) {
vector<int> tmp;
for (auto v : e[now])
if (v != fa) tmp.push_back(dp[v][m - 1]);
sort(tmp.begin(), tmp.end(), greater<int>());
int t = 1;
while (t <= (int)tmp.size() && tmp[t - 1] >= t) t++;
dp[now][m] = t - 1;
upd(dp[now][m], 1);
}
return mxdep;
}
void dfs2(int now, int fa) {
for (auto v : e[now])
if (v != fa) {
dfs2(v, now);
for (int m = 1; m <= 19; m++) upd(dp[now][m], dp[v][m]);
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n - 1; i++) {
int x, y;
scanf("%d%d", &x, &y);
e[x].push_back(y);
e[y].push_back(x);
}
dfs(1, 0);
dfs2(1, 0);
for (int now = 1; now <= n; now++)
for (int m = 1; m <= 18; m++)
ans += 1ll * m * (dp[now][m] - dp[now][m + 1]);
printf("%lld\n", ans);
return 0;
}
| ### Prompt
Your task is to create a cpp solution to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long ans;
int dp[300000 + 5][20], n;
vector<int> e[300000 + 5];
inline void upd(int &x, int y) {
if (x < y) x = y;
}
int dfs(int now, int fa) {
int mxdep = 0;
for (auto v : e[now])
if (v != fa) upd(mxdep, dfs(v, now));
mxdep++;
ans += mxdep;
dp[now][1] = n;
for (int m = 2; m <= 19; m++) {
vector<int> tmp;
for (auto v : e[now])
if (v != fa) tmp.push_back(dp[v][m - 1]);
sort(tmp.begin(), tmp.end(), greater<int>());
int t = 1;
while (t <= (int)tmp.size() && tmp[t - 1] >= t) t++;
dp[now][m] = t - 1;
upd(dp[now][m], 1);
}
return mxdep;
}
void dfs2(int now, int fa) {
for (auto v : e[now])
if (v != fa) {
dfs2(v, now);
for (int m = 1; m <= 19; m++) upd(dp[now][m], dp[v][m]);
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n - 1; i++) {
int x, y;
scanf("%d%d", &x, &y);
e[x].push_back(y);
e[y].push_back(x);
}
dfs(1, 0);
dfs2(1, 0);
for (int now = 1; now <= n; now++)
for (int m = 1; m <= 18; m++)
ans += 1ll * m * (dp[now][m] - dp[now][m + 1]);
printf("%lld\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 300005;
vector<int> e[N];
int n, v, f[N], g[N], q[N];
long long sum;
void dfs(int x, int fa, int v) {
f[x] = 1;
g[x] = 1;
for (auto i : e[x])
if (i != fa) {
dfs(i, x, v);
g[x] = max(g[x], g[i]);
}
if (e[x].size() - (fa != 0) >= v) {
*q = 0;
for (auto i : e[x])
if (i != fa) q[++*q] = -f[i];
assert(*q >= v);
nth_element(q + 1, q + v, q + *q + 1);
f[x] = -q[v];
g[x] = max(g[x], ++f[x]);
}
sum += g[x];
}
void dfs2(int x, int fa, int v) {
f[x] = e[x].size() - (fa != 0);
g[x] = 0;
for (auto i : e[x])
if (i != fa) {
dfs2(i, x, v);
f[x] = max(f[x], f[i]);
g[x] = max(g[x], g[i]);
}
*q = 0;
for (auto i : e[x])
if (i != fa) q[++*q] = e[i].size() - 1;
sort(q + 1, q + *q + 1);
reverse(q + 1, q + *q + 1);
int tmp = 0;
for (; tmp < *q && q[tmp + 1] >= tmp + 1; ++tmp)
;
g[x] = max(g[x], tmp);
sum += n - v + 1;
if (f[x] >= v) sum += f[x] - v + 1;
if (g[x] >= v) sum += g[x] - v + 1;
}
int main() {
scanf("%d", &n);
for (int i = (int)(1); i <= (int)(n - 1); i++) {
int x, y;
scanf("%d%d", &x, &y);
e[x].push_back(y);
e[y].push_back(x);
}
for (v = 1; v * v * v <= n; v++) dfs(1, 0, v);
dfs2(1, 0, v);
printf("%lld\n", sum);
}
| ### Prompt
Generate a CPP solution to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 300005;
vector<int> e[N];
int n, v, f[N], g[N], q[N];
long long sum;
void dfs(int x, int fa, int v) {
f[x] = 1;
g[x] = 1;
for (auto i : e[x])
if (i != fa) {
dfs(i, x, v);
g[x] = max(g[x], g[i]);
}
if (e[x].size() - (fa != 0) >= v) {
*q = 0;
for (auto i : e[x])
if (i != fa) q[++*q] = -f[i];
assert(*q >= v);
nth_element(q + 1, q + v, q + *q + 1);
f[x] = -q[v];
g[x] = max(g[x], ++f[x]);
}
sum += g[x];
}
void dfs2(int x, int fa, int v) {
f[x] = e[x].size() - (fa != 0);
g[x] = 0;
for (auto i : e[x])
if (i != fa) {
dfs2(i, x, v);
f[x] = max(f[x], f[i]);
g[x] = max(g[x], g[i]);
}
*q = 0;
for (auto i : e[x])
if (i != fa) q[++*q] = e[i].size() - 1;
sort(q + 1, q + *q + 1);
reverse(q + 1, q + *q + 1);
int tmp = 0;
for (; tmp < *q && q[tmp + 1] >= tmp + 1; ++tmp)
;
g[x] = max(g[x], tmp);
sum += n - v + 1;
if (f[x] >= v) sum += f[x] - v + 1;
if (g[x] >= v) sum += g[x] - v + 1;
}
int main() {
scanf("%d", &n);
for (int i = (int)(1); i <= (int)(n - 1); i++) {
int x, y;
scanf("%d%d", &x, &y);
e[x].push_back(y);
e[y].push_back(x);
}
for (v = 1; v * v * v <= n; v++) dfs(1, 0, v);
dfs2(1, 0, v);
printf("%lld\n", sum);
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long N = 300005;
long long n, dp[N][20][2], ans, h[N];
vector<vector<long long> > gr;
priority_queue<long long> pq[N][20];
void dfs(long long u, long long par) {
h[u] = 1;
dp[u][1][0] = dp[u][1][1] = n;
for (long long i = 0; i < gr[u].size(); ++i) {
long long v = gr[u][i];
if (v != par) {
dfs(v, u);
h[u] = max(h[u], h[v] + 1);
for (long long j = 2; j <= 18; ++j) {
pq[u][j].push(dp[v][j - 1][0]);
dp[u][j][1] = max(dp[v][j][1], dp[u][j][1]);
}
}
}
vector<long long> x[20];
for (long long i = 2; i <= 18; ++i) {
while (!pq[u][i].empty()) {
x[i].push_back(pq[u][i].top());
pq[u][i].pop();
}
}
for (long long i = 2; i <= 18; ++i) {
if (x[i].size()) {
for (long long j = x[i].size() - 1; j >= 0; --j) {
if (x[i][j] >= j + 1) {
dp[u][i][0] = max(j + 1, dp[u][i][0]);
break;
}
}
dp[u][i][1] = max(dp[u][i][1], dp[u][i][0]);
}
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
gr.resize(n + 5, vector<long long>());
for (long long i = 0; i < n - 1; ++i) {
long long u, v;
cin >> u >> v;
gr[u].push_back(v);
gr[v].push_back(u);
}
dfs(1, 0);
for (long long i = 1; i <= n; ++i) ans += h[i];
for (long long u = 1; u <= n; ++u) {
for (long long i = 1; i <= 19; ++i) {
long long l = 2;
if (i <= 18) l = max(l, dp[u][i + 1][1] + 1);
long long r = dp[u][i][1];
if (l <= r) ans += (r - l + 1) * i;
}
}
cout << ans;
return 0;
}
| ### Prompt
Generate a CPP solution to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long N = 300005;
long long n, dp[N][20][2], ans, h[N];
vector<vector<long long> > gr;
priority_queue<long long> pq[N][20];
void dfs(long long u, long long par) {
h[u] = 1;
dp[u][1][0] = dp[u][1][1] = n;
for (long long i = 0; i < gr[u].size(); ++i) {
long long v = gr[u][i];
if (v != par) {
dfs(v, u);
h[u] = max(h[u], h[v] + 1);
for (long long j = 2; j <= 18; ++j) {
pq[u][j].push(dp[v][j - 1][0]);
dp[u][j][1] = max(dp[v][j][1], dp[u][j][1]);
}
}
}
vector<long long> x[20];
for (long long i = 2; i <= 18; ++i) {
while (!pq[u][i].empty()) {
x[i].push_back(pq[u][i].top());
pq[u][i].pop();
}
}
for (long long i = 2; i <= 18; ++i) {
if (x[i].size()) {
for (long long j = x[i].size() - 1; j >= 0; --j) {
if (x[i][j] >= j + 1) {
dp[u][i][0] = max(j + 1, dp[u][i][0]);
break;
}
}
dp[u][i][1] = max(dp[u][i][1], dp[u][i][0]);
}
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
gr.resize(n + 5, vector<long long>());
for (long long i = 0; i < n - 1; ++i) {
long long u, v;
cin >> u >> v;
gr[u].push_back(v);
gr[v].push_back(u);
}
dfs(1, 0);
for (long long i = 1; i <= n; ++i) ans += h[i];
for (long long u = 1; u <= n; ++u) {
for (long long i = 1; i <= 19; ++i) {
long long l = 2;
if (i <= 18) l = max(l, dp[u][i + 1][1] + 1);
long long r = dp[u][i][1];
if (l <= r) ans += (r - l + 1) * i;
}
}
cout << ans;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long int n;
vector<vector<int> > child(300001, vector<int>());
bool was[300001];
int DP3till[300001];
int DP2till[300001];
int k2[300001];
long long int toadd;
pair<int, int> dfs(int v, int k) {
multiset<int> ways;
int cn1 = 0;
int mx = 0;
for (int u : child[v]) {
auto curr = dfs(u, k);
mx = max(mx, curr.second);
if (curr.first != 1) {
ways.insert(curr.first);
} else {
cn1++;
}
}
int curr = 1;
if (child[v].size() >= k) {
curr = 2;
}
if (((int)ways.size()) >= k) {
auto it = ways.end();
for (int i = 0; i < k; i++) {
it--;
}
curr = *it + 1;
}
toadd += max(curr, mx);
return {curr, max(curr, mx)};
}
void dfs2(int v) {
DP2till[v] = k2[v] = child[v].size();
vector<int> till3;
for (int u : child[v]) {
dfs2(u);
till3.push_back(k2[u]);
DP2till[v] = max(DP2till[v], DP2till[u]);
DP3till[v] = max(DP3till[v], DP3till[u]);
}
if (child[v].size() >= 70) {
sort(till3.begin(), till3.end(), greater<int>());
int left = 70;
int right = till3.size();
while (left < right) {
int mid = (left + right) / 2;
if (left == right - 1) {
mid++;
}
if (till3[mid - 1] >= mid) {
left = mid;
} else {
right = mid - 1;
}
}
if (till3[left - 1] >= left) {
DP3till[v] = max(DP3till[v], left);
}
}
}
int main() {
scanf("%d", &n);
vector<vector<int> > graph(n + 1, vector<int>());
for (int i = 1; i < n; i++) {
int a, b;
scanf("%d %d", &a, &b);
graph[a].push_back(b);
graph[b].push_back(a);
}
stack<int> st;
st.push(1);
was[1] = true;
while (!st.empty()) {
int v = st.top();
st.pop();
for (int u : graph[v]) {
if (!was[u]) {
child[v].push_back(u);
st.push(u);
was[u] = true;
}
}
}
long long int ans = 0;
for (int i = 1; i <= n; i++) {
if (i == 70) {
dfs2(1);
ans += (n - i + 1) * n;
for (int v = 1; v <= n; v++) {
if (DP2till[v] >= 70) {
ans += DP2till[v] - 70 + 1;
}
if (DP3till[v] >= 70) {
ans += DP3till[v] - 70 + 1;
}
}
break;
}
toadd = 0;
dfs(1, i);
ans += toadd;
}
printf("%lld", ans);
return 0;
}
| ### Prompt
Generate a Cpp solution to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long int n;
vector<vector<int> > child(300001, vector<int>());
bool was[300001];
int DP3till[300001];
int DP2till[300001];
int k2[300001];
long long int toadd;
pair<int, int> dfs(int v, int k) {
multiset<int> ways;
int cn1 = 0;
int mx = 0;
for (int u : child[v]) {
auto curr = dfs(u, k);
mx = max(mx, curr.second);
if (curr.first != 1) {
ways.insert(curr.first);
} else {
cn1++;
}
}
int curr = 1;
if (child[v].size() >= k) {
curr = 2;
}
if (((int)ways.size()) >= k) {
auto it = ways.end();
for (int i = 0; i < k; i++) {
it--;
}
curr = *it + 1;
}
toadd += max(curr, mx);
return {curr, max(curr, mx)};
}
void dfs2(int v) {
DP2till[v] = k2[v] = child[v].size();
vector<int> till3;
for (int u : child[v]) {
dfs2(u);
till3.push_back(k2[u]);
DP2till[v] = max(DP2till[v], DP2till[u]);
DP3till[v] = max(DP3till[v], DP3till[u]);
}
if (child[v].size() >= 70) {
sort(till3.begin(), till3.end(), greater<int>());
int left = 70;
int right = till3.size();
while (left < right) {
int mid = (left + right) / 2;
if (left == right - 1) {
mid++;
}
if (till3[mid - 1] >= mid) {
left = mid;
} else {
right = mid - 1;
}
}
if (till3[left - 1] >= left) {
DP3till[v] = max(DP3till[v], left);
}
}
}
int main() {
scanf("%d", &n);
vector<vector<int> > graph(n + 1, vector<int>());
for (int i = 1; i < n; i++) {
int a, b;
scanf("%d %d", &a, &b);
graph[a].push_back(b);
graph[b].push_back(a);
}
stack<int> st;
st.push(1);
was[1] = true;
while (!st.empty()) {
int v = st.top();
st.pop();
for (int u : graph[v]) {
if (!was[u]) {
child[v].push_back(u);
st.push(u);
was[u] = true;
}
}
}
long long int ans = 0;
for (int i = 1; i <= n; i++) {
if (i == 70) {
dfs2(1);
ans += (n - i + 1) * n;
for (int v = 1; v <= n; v++) {
if (DP2till[v] >= 70) {
ans += DP2till[v] - 70 + 1;
}
if (DP3till[v] >= 70) {
ans += DP3till[v] - 70 + 1;
}
}
break;
}
toadd = 0;
dfs(1, i);
ans += toadd;
}
printf("%lld", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long n, fh[300069], dp[300069][18], z;
vector<long long> al[300069];
bitset<300069> vtd;
void bd(long long x) {
long long i, j, sz = al[x].size(), l, lh, rh, md, zz, c;
vector<long long> v;
vtd[x] = 1;
for (i = 0; i < sz; i++) {
l = al[x][i];
if (!vtd[l]) {
bd(l);
fh[x] = max(fh[x], fh[l] + 1);
v.push_back(l);
}
}
sz = v.size();
dp[x][0] = n;
for (i = 1; i < 18; i++) {
for (lh = 0, rh = n; lh <= rh;) {
md = (lh + rh) / 2;
c = 0;
for (j = 0; j < sz; j++) {
l = v[j];
c += md <= dp[l][i - 1];
}
if (c >= md) {
zz = md;
lh = md + 1;
} else {
rh = md - 1;
}
}
dp[x][i] = zz;
}
z += fh[x];
}
void bd2(long long x) {
long long i, j, sz = al[x].size(), l;
vtd[x] = 1;
for (i = 0; i < sz; i++) {
l = al[x][i];
if (!vtd[l]) {
bd2(l);
for (j = 0; j < 18; j++) {
dp[x][j] = max(dp[x][j], dp[l][j]);
}
}
}
l = 1;
for (i = 17; i + 1; i--) {
z += max(dp[x][i] - l, 0ll) * i;
l = max(l, dp[x][i]);
}
}
int main() {
long long i, k, l;
scanf("%lld", &n);
for (i = 0; i < n - 1; i++) {
scanf("%lld%lld", &k, &l);
al[k].push_back(l);
al[l].push_back(k);
}
z = n * n;
bd(1);
vtd.reset();
bd2(1);
printf("%lld\n", z);
}
| ### Prompt
Construct a Cpp code solution to the problem outlined:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long n, fh[300069], dp[300069][18], z;
vector<long long> al[300069];
bitset<300069> vtd;
void bd(long long x) {
long long i, j, sz = al[x].size(), l, lh, rh, md, zz, c;
vector<long long> v;
vtd[x] = 1;
for (i = 0; i < sz; i++) {
l = al[x][i];
if (!vtd[l]) {
bd(l);
fh[x] = max(fh[x], fh[l] + 1);
v.push_back(l);
}
}
sz = v.size();
dp[x][0] = n;
for (i = 1; i < 18; i++) {
for (lh = 0, rh = n; lh <= rh;) {
md = (lh + rh) / 2;
c = 0;
for (j = 0; j < sz; j++) {
l = v[j];
c += md <= dp[l][i - 1];
}
if (c >= md) {
zz = md;
lh = md + 1;
} else {
rh = md - 1;
}
}
dp[x][i] = zz;
}
z += fh[x];
}
void bd2(long long x) {
long long i, j, sz = al[x].size(), l;
vtd[x] = 1;
for (i = 0; i < sz; i++) {
l = al[x][i];
if (!vtd[l]) {
bd2(l);
for (j = 0; j < 18; j++) {
dp[x][j] = max(dp[x][j], dp[l][j]);
}
}
}
l = 1;
for (i = 17; i + 1; i--) {
z += max(dp[x][i] - l, 0ll) * i;
l = max(l, dp[x][i]);
}
}
int main() {
long long i, k, l;
scanf("%lld", &n);
for (i = 0; i < n - 1; i++) {
scanf("%lld%lld", &k, &l);
al[k].push_back(l);
al[l].push_back(k);
}
z = n * n;
bd(1);
vtd.reset();
bd2(1);
printf("%lld\n", z);
}
``` |
#include <bits/stdc++.h>
using namespace std;
int const M = 3e5 + 100, inf = 1e9 + 20, mod = 1e9 + 7;
int n, a[M], dp[M], st[M], fin[M], mark[M];
vector<pair<int, int> > cand;
vector<int> ve;
vector<int> adj[M];
vector<pair<int, int> > good;
int par[M][30];
long long ans;
vector<int> hist;
bool seg[M * 4];
int pw(int x, int y) {
if (y == 0) return 1;
int tmp = pw(x, y / 2);
if (y % 2 == 0) return (tmp * tmp) % mod;
return ((tmp * tmp) % mod * x) % mod;
}
void dfs_lca(int v, int l) {
par[v][0] = l;
ve.push_back(v);
st[v] = (int)ve.size() - 1;
for (int i = 1; i <= 20; i++) par[v][i] = par[par[v][i - 1]][i - 1];
for (int i = 0; i < adj[v].size(); i++) {
int u = adj[v][i];
if (u != l) dfs_lca(u, v);
}
fin[v] = (int)ve.size();
}
bool ch(int x, int v, int ind, int base) {
int cn = 0;
for (int i = 0; i <= ind; i++) {
int u = adj[v][i];
if (u == par[v][0]) continue;
if (dp[u] >= x) cn++;
}
if (cn >= base) return 1;
return 0;
}
void dfs(int v, int base) {
mark[v] = base;
dp[v] = 1;
int cn = 0, mx = 0, ind;
for (int i = 0; i < adj[v].size(); i++) {
int u = adj[v][i];
if ((int)adj[u].size() - 1 < base) break;
if (u == par[v][0]) continue;
if (mark[u] != base) dfs(u, base);
cn++;
mx = max(mx, dp[u]);
ind = i;
}
if (cn >= base) {
int lo = 0, hi = mx;
while (hi > lo + 1) {
int mid = (lo + hi) / 2;
if (ch(mid, v, ind, base))
lo = mid;
else
hi = mid - 1;
}
if (ch(hi, v, ind, base))
dp[v] = hi + 1;
else
dp[v] = lo + 1;
}
cand.push_back(make_pair(dp[v], v));
}
bool get(int l, int r, int st, int en, int node) {
if (st <= l && r <= en) {
return seg[node];
}
if (st >= r || l >= en) return 0;
int mid = (l + r) >> 1;
return get(l, mid, st, en, node * 2) | get(mid, r, st, en, node * 2 + 1);
}
bool bad(int v) { return get(0, ve.size(), st[v], fin[v], 1); }
void update(int l, int r, int ind, int node) {
seg[node] = 1;
hist.push_back(node);
if (r - l == 1) {
return;
}
int mid = (l + r) >> 1;
if (ind < mid)
update(l, mid, ind, node * 2);
else
update(mid, r, ind, node * 2 + 1);
}
void check(int v) {
if (bad(v)) return;
int now = v;
int cnt = 1;
for (int i = 20; i >= 0; i--) {
if (par[now][i] == 0) continue;
if (!bad(par[now][i])) now = par[now][i], cnt += (1LL << i);
}
ans += (long long)cnt * (long long)dp[v];
update(0, ve.size(), st[v], 1);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n - 1; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
adj[1].push_back(0);
dfs_lca(1, 0);
for (int i = 1; i <= n; i++) {
good.push_back(make_pair((int)adj[i].size() - 1, i));
vector<pair<int, int> > hlp;
for (int j = 0; j < adj[i].size(); j++) {
int u = adj[i][j];
int tmp = (int)adj[u].size() - 1;
hlp.push_back(make_pair(tmp, u));
}
sort(hlp.begin(), hlp.end());
reverse(hlp.begin(), hlp.end());
adj[i].clear();
for (int j = 0; j < hlp.size(); j++) adj[i].push_back(hlp[j].second);
}
ans = (long long)n * (long long)n;
sort(good.begin(), good.end());
reverse(good.begin(), good.end());
for (int i = 1; i <= n; i++) {
for (int j = 0; j < good.size(); j++) {
if (good[j].first < i) break;
dfs(good[j].second, i);
}
sort(cand.begin(), cand.end());
reverse(cand.begin(), cand.end());
for (int j = 0; j < cand.size(); j++) {
int now = cand[j].second;
check(now);
}
cand.clear();
for (int j = 0; j < hist.size(); j++) seg[hist[j]] = 0;
hist.clear();
}
cout << ans;
}
| ### Prompt
In CPP, your task is to solve the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int const M = 3e5 + 100, inf = 1e9 + 20, mod = 1e9 + 7;
int n, a[M], dp[M], st[M], fin[M], mark[M];
vector<pair<int, int> > cand;
vector<int> ve;
vector<int> adj[M];
vector<pair<int, int> > good;
int par[M][30];
long long ans;
vector<int> hist;
bool seg[M * 4];
int pw(int x, int y) {
if (y == 0) return 1;
int tmp = pw(x, y / 2);
if (y % 2 == 0) return (tmp * tmp) % mod;
return ((tmp * tmp) % mod * x) % mod;
}
void dfs_lca(int v, int l) {
par[v][0] = l;
ve.push_back(v);
st[v] = (int)ve.size() - 1;
for (int i = 1; i <= 20; i++) par[v][i] = par[par[v][i - 1]][i - 1];
for (int i = 0; i < adj[v].size(); i++) {
int u = adj[v][i];
if (u != l) dfs_lca(u, v);
}
fin[v] = (int)ve.size();
}
bool ch(int x, int v, int ind, int base) {
int cn = 0;
for (int i = 0; i <= ind; i++) {
int u = adj[v][i];
if (u == par[v][0]) continue;
if (dp[u] >= x) cn++;
}
if (cn >= base) return 1;
return 0;
}
void dfs(int v, int base) {
mark[v] = base;
dp[v] = 1;
int cn = 0, mx = 0, ind;
for (int i = 0; i < adj[v].size(); i++) {
int u = adj[v][i];
if ((int)adj[u].size() - 1 < base) break;
if (u == par[v][0]) continue;
if (mark[u] != base) dfs(u, base);
cn++;
mx = max(mx, dp[u]);
ind = i;
}
if (cn >= base) {
int lo = 0, hi = mx;
while (hi > lo + 1) {
int mid = (lo + hi) / 2;
if (ch(mid, v, ind, base))
lo = mid;
else
hi = mid - 1;
}
if (ch(hi, v, ind, base))
dp[v] = hi + 1;
else
dp[v] = lo + 1;
}
cand.push_back(make_pair(dp[v], v));
}
bool get(int l, int r, int st, int en, int node) {
if (st <= l && r <= en) {
return seg[node];
}
if (st >= r || l >= en) return 0;
int mid = (l + r) >> 1;
return get(l, mid, st, en, node * 2) | get(mid, r, st, en, node * 2 + 1);
}
bool bad(int v) { return get(0, ve.size(), st[v], fin[v], 1); }
void update(int l, int r, int ind, int node) {
seg[node] = 1;
hist.push_back(node);
if (r - l == 1) {
return;
}
int mid = (l + r) >> 1;
if (ind < mid)
update(l, mid, ind, node * 2);
else
update(mid, r, ind, node * 2 + 1);
}
void check(int v) {
if (bad(v)) return;
int now = v;
int cnt = 1;
for (int i = 20; i >= 0; i--) {
if (par[now][i] == 0) continue;
if (!bad(par[now][i])) now = par[now][i], cnt += (1LL << i);
}
ans += (long long)cnt * (long long)dp[v];
update(0, ve.size(), st[v], 1);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n - 1; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
adj[1].push_back(0);
dfs_lca(1, 0);
for (int i = 1; i <= n; i++) {
good.push_back(make_pair((int)adj[i].size() - 1, i));
vector<pair<int, int> > hlp;
for (int j = 0; j < adj[i].size(); j++) {
int u = adj[i][j];
int tmp = (int)adj[u].size() - 1;
hlp.push_back(make_pair(tmp, u));
}
sort(hlp.begin(), hlp.end());
reverse(hlp.begin(), hlp.end());
adj[i].clear();
for (int j = 0; j < hlp.size(); j++) adj[i].push_back(hlp[j].second);
}
ans = (long long)n * (long long)n;
sort(good.begin(), good.end());
reverse(good.begin(), good.end());
for (int i = 1; i <= n; i++) {
for (int j = 0; j < good.size(); j++) {
if (good[j].first < i) break;
dfs(good[j].second, i);
}
sort(cand.begin(), cand.end());
reverse(cand.begin(), cand.end());
for (int j = 0; j < cand.size(); j++) {
int now = cand[j].second;
check(now);
}
cand.clear();
for (int j = 0; j < hist.size(); j++) seg[hist[j]] = 0;
hist.clear();
}
cout << ans;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int read() {
int x = 0;
bool flg = false;
char ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-') flg = true;
for (; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ 48);
return flg ? -x : x;
}
int n;
long long ans;
struct Edge {
int to, nxt;
} edge[600010];
int cnt = 1, last[300010];
inline void addedge(int x, int y) {
edge[++cnt] = (Edge){y, last[x]}, last[x] = cnt;
edge[++cnt] = (Edge){x, last[y]}, last[y] = cnt;
}
int f[300010][19];
int bin[300010];
int dep[300010], maxdep[300010];
void dfs1(int cur, int fat, int dep) {
int chtot = 0;
::dep[cur] = maxdep[cur] = dep;
for (int i = last[cur]; i; i = edge[i].nxt) {
if (edge[i].to == fat) continue;
dfs1(edge[i].to, cur, dep + 1);
maxdep[cur] = max(maxdep[cur], maxdep[edge[i].to]);
chtot++;
}
for (int i = 18; i >= 2; i--) {
(*bin) = 0;
for (int j = last[cur]; j; j = edge[j].nxt)
if (edge[j].to ^ fat) bin[++*bin] = f[edge[j].to][i - 1];
sort(bin + 1, bin + (*bin) + 1);
int l = 1, r = chtot, mid;
if (l > r || !(bin[(*bin) - l + 1] >= l)) continue;
while (l < r) {
mid = l + r + 1 >> 1;
if (bin[(*bin) - mid + 1] >= mid)
l = mid;
else
r = mid - 1;
}
f[cur][i] = l;
}
f[cur][1] = n;
}
void dfs2(int cur, int fat) {
for (int i = last[cur]; i; i = edge[i].nxt)
if (edge[i].to ^ fat) {
dfs2(edge[i].to, cur);
for (int j = 1; j <= 18; j++)
f[cur][j] = max(f[cur][j], f[edge[i].to][j]);
}
ans += maxdep[cur] - dep[cur] + 1;
for (int j = 18; j; j--) {
if (f[cur][j] < 2) continue;
if (j == 18 || f[cur][j + 1] < 2)
ans += 1LL * j * (f[cur][j] - 1);
else
ans += 1LL * j * (f[cur][j] - f[cur][j + 1]);
}
}
int main() {
n = read();
for (int i = 1; i < n; i++) addedge(read(), read());
dfs1(1, 0, 1);
dfs2(1, 0);
printf("%I64d\n", ans);
return 0;
}
| ### Prompt
Create a solution in Cpp for the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int read() {
int x = 0;
bool flg = false;
char ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-') flg = true;
for (; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ 48);
return flg ? -x : x;
}
int n;
long long ans;
struct Edge {
int to, nxt;
} edge[600010];
int cnt = 1, last[300010];
inline void addedge(int x, int y) {
edge[++cnt] = (Edge){y, last[x]}, last[x] = cnt;
edge[++cnt] = (Edge){x, last[y]}, last[y] = cnt;
}
int f[300010][19];
int bin[300010];
int dep[300010], maxdep[300010];
void dfs1(int cur, int fat, int dep) {
int chtot = 0;
::dep[cur] = maxdep[cur] = dep;
for (int i = last[cur]; i; i = edge[i].nxt) {
if (edge[i].to == fat) continue;
dfs1(edge[i].to, cur, dep + 1);
maxdep[cur] = max(maxdep[cur], maxdep[edge[i].to]);
chtot++;
}
for (int i = 18; i >= 2; i--) {
(*bin) = 0;
for (int j = last[cur]; j; j = edge[j].nxt)
if (edge[j].to ^ fat) bin[++*bin] = f[edge[j].to][i - 1];
sort(bin + 1, bin + (*bin) + 1);
int l = 1, r = chtot, mid;
if (l > r || !(bin[(*bin) - l + 1] >= l)) continue;
while (l < r) {
mid = l + r + 1 >> 1;
if (bin[(*bin) - mid + 1] >= mid)
l = mid;
else
r = mid - 1;
}
f[cur][i] = l;
}
f[cur][1] = n;
}
void dfs2(int cur, int fat) {
for (int i = last[cur]; i; i = edge[i].nxt)
if (edge[i].to ^ fat) {
dfs2(edge[i].to, cur);
for (int j = 1; j <= 18; j++)
f[cur][j] = max(f[cur][j], f[edge[i].to][j]);
}
ans += maxdep[cur] - dep[cur] + 1;
for (int j = 18; j; j--) {
if (f[cur][j] < 2) continue;
if (j == 18 || f[cur][j + 1] < 2)
ans += 1LL * j * (f[cur][j] - 1);
else
ans += 1LL * j * (f[cur][j] - f[cur][j + 1]);
}
}
int main() {
n = read();
for (int i = 1; i < n; i++) addedge(read(), read());
dfs1(1, 0, 1);
dfs2(1, 0);
printf("%I64d\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
inline char gc() {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
? EOF
: *p1++;
}
inline int read() {
int x = 0;
char ch = getchar();
bool positive = 1;
for (; !isdigit(ch); ch = getchar())
if (ch == '-') positive = 0;
for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
return positive ? x : -x;
}
inline void write(int a) {
if (a >= 10) write(a / 10);
putchar('0' + a % 10);
}
inline void writeln(int a) {
if (a < 0) {
a = -a;
putchar('-');
}
write(a);
puts("");
}
const int N = 300005, K = 19;
long long ans;
int n, tp[N], q[N], dp[N][K];
vector<int> v[N];
inline bool cmp(int a, int b) { return a > b; }
void dfs(int p, int fa) {
for (unsigned i = 0; i < v[p].size(); i++)
if (v[p][i] != fa) {
dfs(v[p][i], p);
tp[p] = max(tp[v[p][i]], tp[p]);
}
ans += ++tp[p];
dp[p][1] = n;
for (int i = 2; i < K; i++) {
int tot = 0;
for (unsigned j = 0; j < v[p].size(); j++)
if (dp[v[p][j]][i - 1]) q[++tot] = dp[v[p][j]][i - 1];
sort(&q[1], &q[tot + 1], cmp);
for (int j = tot; j > 1; j--) {
if (q[j] >= j) {
dp[p][i] = j;
break;
}
}
}
}
void solve(int p, int fa) {
for (unsigned i = 0; i < v[p].size(); i++)
if (v[p][i] != fa) {
solve(v[p][i], p);
for (int j = 0; j < K; j++) dp[p][j] = max(dp[p][j], dp[v[p][i]][j]);
}
for (int i = 1; i < K; i++) {
ans += ((long long)dp[p][i] - (i + 1 == K ? 0 : dp[p][i + 1])) * i;
if (i + 1 == K || dp[p][i + 1] == 0) {
ans -= i;
break;
}
}
}
int main() {
n = read();
for (int i = 1; i < n; i++) {
int s = read(), t = read();
v[s].push_back(t);
v[t].push_back(s);
}
dfs(1, 0);
solve(1, 0);
cout << ans << endl;
}
| ### Prompt
Generate a CPP solution to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline char gc() {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
? EOF
: *p1++;
}
inline int read() {
int x = 0;
char ch = getchar();
bool positive = 1;
for (; !isdigit(ch); ch = getchar())
if (ch == '-') positive = 0;
for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
return positive ? x : -x;
}
inline void write(int a) {
if (a >= 10) write(a / 10);
putchar('0' + a % 10);
}
inline void writeln(int a) {
if (a < 0) {
a = -a;
putchar('-');
}
write(a);
puts("");
}
const int N = 300005, K = 19;
long long ans;
int n, tp[N], q[N], dp[N][K];
vector<int> v[N];
inline bool cmp(int a, int b) { return a > b; }
void dfs(int p, int fa) {
for (unsigned i = 0; i < v[p].size(); i++)
if (v[p][i] != fa) {
dfs(v[p][i], p);
tp[p] = max(tp[v[p][i]], tp[p]);
}
ans += ++tp[p];
dp[p][1] = n;
for (int i = 2; i < K; i++) {
int tot = 0;
for (unsigned j = 0; j < v[p].size(); j++)
if (dp[v[p][j]][i - 1]) q[++tot] = dp[v[p][j]][i - 1];
sort(&q[1], &q[tot + 1], cmp);
for (int j = tot; j > 1; j--) {
if (q[j] >= j) {
dp[p][i] = j;
break;
}
}
}
}
void solve(int p, int fa) {
for (unsigned i = 0; i < v[p].size(); i++)
if (v[p][i] != fa) {
solve(v[p][i], p);
for (int j = 0; j < K; j++) dp[p][j] = max(dp[p][j], dp[v[p][i]][j]);
}
for (int i = 1; i < K; i++) {
ans += ((long long)dp[p][i] - (i + 1 == K ? 0 : dp[p][i + 1])) * i;
if (i + 1 == K || dp[p][i + 1] == 0) {
ans -= i;
break;
}
}
}
int main() {
n = read();
for (int i = 1; i < n; i++) {
int s = read(), t = read();
v[s].push_back(t);
v[t].push_back(s);
}
dfs(1, 0);
solve(1, 0);
cout << ans << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 10;
const int MAX_LOG = 20;
vector<int> vertex[MAXN];
vector<int> e;
int h[MAXN];
int z[MAXN];
int dp[MAXN];
bool check[MAXN];
long long res;
void dfs(int v) {
check[v] = true;
h[v] = 1;
e.clear();
for (int i = 0; i < vertex[v].size(); i++) {
if (!check[vertex[v][i]]) {
e.push_back(dp[vertex[v][i]]);
}
}
e.push_back(0);
sort(e.begin(), e.end());
dp[v] = 0;
int w = 0;
for (int i = (long long)(e.size()) - 1; i >= 0; i--) {
int k = i;
if (w >= e[i]) {
dp[v] = w;
break;
}
while (k >= 0 && e[k] > 0 && e[k] == e[i]) {
w++;
k--;
}
if (w >= e[i]) {
dp[v] = e[i];
break;
}
i = k + 1;
}
if (dp[v] == 0) {
dp[v] = w;
}
for (int i = 0; i < vertex[v].size(); i++) {
if (!check[vertex[v][i]]) {
dfs(vertex[v][i]);
h[v] = max(h[v], h[vertex[v][i]] + 1);
}
}
}
void calc(int v) {
check[v] = true;
z[v] = dp[v];
for (int i = 0; i < vertex[v].size(); i++) {
if (!check[vertex[v][i]]) {
calc(vertex[v][i]);
z[v] = max(z[vertex[v][i]], z[v]);
}
}
res += max(0, z[v] - 1);
}
int main() {
ios_base ::sync_with_stdio(false);
cin.tie(0);
long long n;
cin >> n;
for (int i = 0; i < (n - 1); i++) {
int v, u;
cin >> v >> u;
vertex[v].push_back(u);
vertex[u].push_back(v);
}
for (int i = 1; i <= n; i++) {
dp[i] = n;
}
res = (n - 1) * n;
for (int i = 1; i < MAX_LOG; i++) {
fill(check, check + MAXN, false);
dfs(1);
fill(check, check + MAXN, false);
calc(1);
}
for (int i = 1; i <= n; i++) {
res += h[i];
}
cout << res;
}
| ### Prompt
Your challenge is to write a cpp solution to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 10;
const int MAX_LOG = 20;
vector<int> vertex[MAXN];
vector<int> e;
int h[MAXN];
int z[MAXN];
int dp[MAXN];
bool check[MAXN];
long long res;
void dfs(int v) {
check[v] = true;
h[v] = 1;
e.clear();
for (int i = 0; i < vertex[v].size(); i++) {
if (!check[vertex[v][i]]) {
e.push_back(dp[vertex[v][i]]);
}
}
e.push_back(0);
sort(e.begin(), e.end());
dp[v] = 0;
int w = 0;
for (int i = (long long)(e.size()) - 1; i >= 0; i--) {
int k = i;
if (w >= e[i]) {
dp[v] = w;
break;
}
while (k >= 0 && e[k] > 0 && e[k] == e[i]) {
w++;
k--;
}
if (w >= e[i]) {
dp[v] = e[i];
break;
}
i = k + 1;
}
if (dp[v] == 0) {
dp[v] = w;
}
for (int i = 0; i < vertex[v].size(); i++) {
if (!check[vertex[v][i]]) {
dfs(vertex[v][i]);
h[v] = max(h[v], h[vertex[v][i]] + 1);
}
}
}
void calc(int v) {
check[v] = true;
z[v] = dp[v];
for (int i = 0; i < vertex[v].size(); i++) {
if (!check[vertex[v][i]]) {
calc(vertex[v][i]);
z[v] = max(z[vertex[v][i]], z[v]);
}
}
res += max(0, z[v] - 1);
}
int main() {
ios_base ::sync_with_stdio(false);
cin.tie(0);
long long n;
cin >> n;
for (int i = 0; i < (n - 1); i++) {
int v, u;
cin >> v >> u;
vertex[v].push_back(u);
vertex[u].push_back(v);
}
for (int i = 1; i <= n; i++) {
dp[i] = n;
}
res = (n - 1) * n;
for (int i = 1; i < MAX_LOG; i++) {
fill(check, check + MAXN, false);
dfs(1);
fill(check, check + MAXN, false);
calc(1);
}
for (int i = 1; i <= n; i++) {
res += h[i];
}
cout << res;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3fffffff;
const int SINF = 0x7fffffff;
const long long LINF = 0x3fffffffffffffff;
const long long SLINF = 0x7fffffffffffffff;
const int MAXN = 300007;
int n;
long long ans;
vector<int> ch[MAXN];
vector<int> dp[MAXN];
vector<int> tmp[MAXN];
void init();
void input();
void work();
void dfs(int now, int fa);
int getans(int now, vector<int> &cd);
void uni(vector<int> &a, int &va, vector<int> &b, int &vb);
int main() {
init();
input();
work();
}
void init() { ios::sync_with_stdio(false); }
void input() {
scanf("%d", &n);
int u, v;
for (int i = 1; i < n; ++i) {
scanf("%d%d", &u, &v);
ch[u].push_back(v), ch[v].push_back(u);
}
}
void work() {
dfs(1, -1);
ans = static_cast<long long>(n) * n;
vector<int> tt;
getans(1, tt);
cout << ans << endl;
}
void dfs(int now, int fa) {
for (int i = 0; i < ch[now].size(); ++i) {
if (ch[now][i] == fa) {
ch[now].erase(ch[now].begin() + i);
break;
}
}
int nd = ch[now].size();
for (int i = 0; i < nd; ++i) dfs(ch[now][i], now);
dp[now].resize(nd + 1);
for (int i = 1; i <= nd; ++i) tmp[i].clear();
for (int i = 0; i < nd; ++i) {
int v = ch[now][i];
for (int j = 1; j <= ch[v].size(); ++j) {
tmp[j].push_back(dp[v][j]);
}
}
for (int i = 1; i <= nd; ++i) {
if (tmp[i].size() >= i) {
nth_element(tmp[i].begin(), tmp[i].end() - i, tmp[i].end());
dp[now][i] = (*(tmp[i].end() - i)) + 1;
} else
dp[now][i] = 1;
}
}
int getans(int now, vector<int> &cd) {
vector<int> vd;
int nd = ch[now].size();
cd.resize(nd + 1);
int ca = 0, va;
for (int i = 1; i <= nd; ++i) cd[i] = dp[now][i], ca += cd[i];
for (int i = 0; i < nd; ++i) {
vd.clear();
va = getans(ch[now][i], vd);
uni(cd, ca, vd, va);
}
ans += ca;
return ca;
}
void uni(vector<int> &a, int &va, vector<int> &b, int &vb) {
if (a.size() < b.size()) {
uni(b, vb, a, va);
a.swap(b);
swap(va, vb);
return;
}
int bs = b.size();
for (int i = 1; i < bs; ++i) {
if (b[i] > a[i]) va += b[i] - a[i], a[i] = b[i];
}
}
| ### Prompt
Please create a solution in Cpp to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3fffffff;
const int SINF = 0x7fffffff;
const long long LINF = 0x3fffffffffffffff;
const long long SLINF = 0x7fffffffffffffff;
const int MAXN = 300007;
int n;
long long ans;
vector<int> ch[MAXN];
vector<int> dp[MAXN];
vector<int> tmp[MAXN];
void init();
void input();
void work();
void dfs(int now, int fa);
int getans(int now, vector<int> &cd);
void uni(vector<int> &a, int &va, vector<int> &b, int &vb);
int main() {
init();
input();
work();
}
void init() { ios::sync_with_stdio(false); }
void input() {
scanf("%d", &n);
int u, v;
for (int i = 1; i < n; ++i) {
scanf("%d%d", &u, &v);
ch[u].push_back(v), ch[v].push_back(u);
}
}
void work() {
dfs(1, -1);
ans = static_cast<long long>(n) * n;
vector<int> tt;
getans(1, tt);
cout << ans << endl;
}
void dfs(int now, int fa) {
for (int i = 0; i < ch[now].size(); ++i) {
if (ch[now][i] == fa) {
ch[now].erase(ch[now].begin() + i);
break;
}
}
int nd = ch[now].size();
for (int i = 0; i < nd; ++i) dfs(ch[now][i], now);
dp[now].resize(nd + 1);
for (int i = 1; i <= nd; ++i) tmp[i].clear();
for (int i = 0; i < nd; ++i) {
int v = ch[now][i];
for (int j = 1; j <= ch[v].size(); ++j) {
tmp[j].push_back(dp[v][j]);
}
}
for (int i = 1; i <= nd; ++i) {
if (tmp[i].size() >= i) {
nth_element(tmp[i].begin(), tmp[i].end() - i, tmp[i].end());
dp[now][i] = (*(tmp[i].end() - i)) + 1;
} else
dp[now][i] = 1;
}
}
int getans(int now, vector<int> &cd) {
vector<int> vd;
int nd = ch[now].size();
cd.resize(nd + 1);
int ca = 0, va;
for (int i = 1; i <= nd; ++i) cd[i] = dp[now][i], ca += cd[i];
for (int i = 0; i < nd; ++i) {
vd.clear();
va = getans(ch[now][i], vd);
uni(cd, ca, vd, va);
}
ans += ca;
return ca;
}
void uni(vector<int> &a, int &va, vector<int> &b, int &vb) {
if (a.size() < b.size()) {
uni(b, vb, a, va);
a.swap(b);
swap(va, vb);
return;
}
int bs = b.size();
for (int i = 1; i < bs; ++i) {
if (b[i] > a[i]) va += b[i] - a[i], a[i] = b[i];
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x;
char c;
while ((c = getchar()) < '0' || c > '9')
;
for (x = c - '0'; (c = getchar()) >= '0' && c <= '9';) x = x * 10 + c - '0';
return x;
}
struct edge {
int nx, t;
} e[300000 * 2 + 5];
int n, k, h[300000 + 5], en, c[300000 + 5], cc[300000 + 5], d[300000 + 5],
f[300000 + 5], ff[300000 + 5], l[300000 + 5], cnt, fa[300000 + 5];
vector<int> v[300000 + 5];
long long ans;
inline void ins(int x, int y) {
e[++en] = (edge){h[x], y};
h[x] = en;
e[++en] = (edge){h[y], x};
h[y] = en;
}
void dfs(int x) {
l[++cnt] = x;
for (int i = h[x]; i; i = e[i].nx)
if (e[i].t != fa[x]) fa[e[i].t] = x, dfs(e[i].t);
}
void dp2(int x, int fa) {
f[x] = 0;
v[x].clear();
for (int i = h[x]; i; i = e[i].nx)
if (e[i].t != fa) {
++c[x];
dp2(e[i].t, x);
if (c[e[i].t] > 66) ++cc[x];
v[x].push_back(c[e[i].t]);
f[x] = max(f[x], f[e[i].t]);
ff[x] = max(ff[x], ff[e[i].t]);
}
f[x] = max(f[x], c[x]);
ans += max(0, f[x] - min(n, 66));
if (cc[x] > 66) {
sort(v[x].begin(), v[x].end());
for (int i = min(n, 66); ++i <= v[x].size();)
if (v[x][v[x].size() - i] < i)
break;
else
ff[x] = max(ff[x], i);
}
ans += max(0, ff[x] - min(n, 66));
}
int main() {
int* x = new int;
srand(*x);
n = read();
for (int i = 1; i < n; ++i) ins(read(), read());
dfs(1);
for (k = 1; k <= 66 && k <= n; ++k)
for (int i = n; i; --i) {
int x = l[i];
f[x] = 0;
for (int i = h[x], cnt = 0; i; i = e[i].nx)
if (e[i].t != fa[x]) {
if (k == 1)
v[x].push_back(d[e[i].t]);
else
v[x][cnt++] = d[e[i].t];
f[x] = max(f[x], f[e[i].t]);
}
if (v[x].size() < k)
d[x] = 1;
else
nth_element(v[x].begin(), v[x].end() - k, v[x].end()),
d[x] = v[x][v[x].size() - k] + 1;
ans += f[x] = max(f[x], d[x]);
}
ans += 1LL * (n - min(n, 66)) * n;
dp2(1, 0);
printf("%I64d", ans);
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x;
char c;
while ((c = getchar()) < '0' || c > '9')
;
for (x = c - '0'; (c = getchar()) >= '0' && c <= '9';) x = x * 10 + c - '0';
return x;
}
struct edge {
int nx, t;
} e[300000 * 2 + 5];
int n, k, h[300000 + 5], en, c[300000 + 5], cc[300000 + 5], d[300000 + 5],
f[300000 + 5], ff[300000 + 5], l[300000 + 5], cnt, fa[300000 + 5];
vector<int> v[300000 + 5];
long long ans;
inline void ins(int x, int y) {
e[++en] = (edge){h[x], y};
h[x] = en;
e[++en] = (edge){h[y], x};
h[y] = en;
}
void dfs(int x) {
l[++cnt] = x;
for (int i = h[x]; i; i = e[i].nx)
if (e[i].t != fa[x]) fa[e[i].t] = x, dfs(e[i].t);
}
void dp2(int x, int fa) {
f[x] = 0;
v[x].clear();
for (int i = h[x]; i; i = e[i].nx)
if (e[i].t != fa) {
++c[x];
dp2(e[i].t, x);
if (c[e[i].t] > 66) ++cc[x];
v[x].push_back(c[e[i].t]);
f[x] = max(f[x], f[e[i].t]);
ff[x] = max(ff[x], ff[e[i].t]);
}
f[x] = max(f[x], c[x]);
ans += max(0, f[x] - min(n, 66));
if (cc[x] > 66) {
sort(v[x].begin(), v[x].end());
for (int i = min(n, 66); ++i <= v[x].size();)
if (v[x][v[x].size() - i] < i)
break;
else
ff[x] = max(ff[x], i);
}
ans += max(0, ff[x] - min(n, 66));
}
int main() {
int* x = new int;
srand(*x);
n = read();
for (int i = 1; i < n; ++i) ins(read(), read());
dfs(1);
for (k = 1; k <= 66 && k <= n; ++k)
for (int i = n; i; --i) {
int x = l[i];
f[x] = 0;
for (int i = h[x], cnt = 0; i; i = e[i].nx)
if (e[i].t != fa[x]) {
if (k == 1)
v[x].push_back(d[e[i].t]);
else
v[x][cnt++] = d[e[i].t];
f[x] = max(f[x], f[e[i].t]);
}
if (v[x].size() < k)
d[x] = 1;
else
nth_element(v[x].begin(), v[x].end() - k, v[x].end()),
d[x] = v[x][v[x].size() - k] + 1;
ans += f[x] = max(f[x], d[x]);
}
ans += 1LL * (n - min(n, 66)) * n;
dp2(1, 0);
printf("%I64d", ans);
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <class T1, class T2>
inline void upd1(T1& a, T2 b) {
a > b ? a = b : 0;
}
template <class T1, class T2>
inline void upd2(T1& a, T2 b) {
a < b ? a = b : 0;
}
struct ano {
operator long long() {
long long x = 0, y = 0, c = getchar();
while (c < 48) y = c == 45, c = getchar();
while (c > 47) x = x * 10 + c - 48, c = getchar();
return y ? -x : x;
}
} buf;
const int N = 3e5 + 5;
vector<int> t[N];
long long s = 0;
int n, f[20][N], g[20][N];
int dfs(int u, int p) {
int l = 1;
for (int v : t[u])
if (v != p) upd2(l, dfs(v, u) + 1);
s += l;
f[1][u] = n;
g[1][u] = n;
for (int i = 2;; ++i) {
vector<int> a;
for (int v : t[u])
if (v != p) a.push_back(-f[i - 1][v]);
sort(a.begin(), a.end());
f[i][u] = 1;
for (int k = a.size(); k > 1; --k)
if (-a[k - 1] >= k) {
f[i][u] = k;
break;
}
if (f[i][u] == 1) break;
}
for (int i = 2;; ++i) {
for (int v : t[u])
if (v != p) upd2(g[i][u], g[i][v]);
upd2(g[i][u], f[i][u]);
s += (i - 1) * (g[i - 1][u] - g[i][u]);
if (g[i][u] == 1) break;
}
return l;
}
int main() {
n = buf;
for (int i = 2; i <= n; ++i) {
int u = buf, v = buf;
t[u].push_back(v);
t[v].push_back(u);
}
dfs(1, 0);
printf("%lld\n", s);
}
| ### Prompt
Please create a solution in Cpp to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <class T1, class T2>
inline void upd1(T1& a, T2 b) {
a > b ? a = b : 0;
}
template <class T1, class T2>
inline void upd2(T1& a, T2 b) {
a < b ? a = b : 0;
}
struct ano {
operator long long() {
long long x = 0, y = 0, c = getchar();
while (c < 48) y = c == 45, c = getchar();
while (c > 47) x = x * 10 + c - 48, c = getchar();
return y ? -x : x;
}
} buf;
const int N = 3e5 + 5;
vector<int> t[N];
long long s = 0;
int n, f[20][N], g[20][N];
int dfs(int u, int p) {
int l = 1;
for (int v : t[u])
if (v != p) upd2(l, dfs(v, u) + 1);
s += l;
f[1][u] = n;
g[1][u] = n;
for (int i = 2;; ++i) {
vector<int> a;
for (int v : t[u])
if (v != p) a.push_back(-f[i - 1][v]);
sort(a.begin(), a.end());
f[i][u] = 1;
for (int k = a.size(); k > 1; --k)
if (-a[k - 1] >= k) {
f[i][u] = k;
break;
}
if (f[i][u] == 1) break;
}
for (int i = 2;; ++i) {
for (int v : t[u])
if (v != p) upd2(g[i][u], g[i][v]);
upd2(g[i][u], f[i][u]);
s += (i - 1) * (g[i - 1][u] - g[i][u]);
if (g[i][u] == 1) break;
}
return l;
}
int main() {
n = buf;
for (int i = 2; i <= n; ++i) {
int u = buf, v = buf;
t[u].push_back(v);
t[v].push_back(u);
}
dfs(1, 0);
printf("%lld\n", s);
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, sl, fh, cnt, s[1000010], fa[1000010], dp[1000010], mxd[1000010],
f[1000010][20];
long long res, ans;
int t, h[1000010];
struct Tre {
int to, nxt;
} e[1000010 << 1];
vector<pair<int, int> > vt[1000010];
int rd() {
sl = 0;
fh = 1;
char ch = getchar();
while (ch < '0' || '9' < ch) {
if (ch == '-') fh = -1;
ch = getchar();
}
while ('0' <= ch && ch <= '9') sl = sl * 10 + ch - '0', ch = getchar();
return sl * fh;
}
void add(int u, int v) {
e[++t] = (Tre){v, h[u]};
h[u] = t;
e[++t] = (Tre){u, h[v]};
h[v] = t;
}
void dfs(int u) {
int v;
f[u][1] = n;
mxd[u] = 1;
for (int i = h[u]; i; i = e[i].nxt)
if ((v = e[i].to) != fa[u])
fa[v] = u, dfs(v), mxd[u] = max(mxd[u], mxd[v] + 1);
ans += mxd[u];
for (int i = 2; i < 20; ++i) {
cnt = 0;
for (int j = h[u]; j; j = e[j].nxt)
if ((v = e[j].to) != fa[u]) s[++cnt] = f[v][i - 1];
sort(s + 1, s + cnt + 1, [&](const int &x, const int &y) { return x > y; });
for (v = 0; v < cnt && s[v + 1] > v; ++v)
;
f[u][i] = v;
vt[v].push_back(make_pair(u, i));
}
}
void upd(int x, int v) {
for (; x && dp[x] < v; x = fa[x]) res += v - dp[x], dp[x] = v;
}
int main() {
n = rd();
int x, y;
for (int i = 1; i < n; ++i) x = rd(), y = rd(), add(x, y);
dfs(1);
for (int i = 1; i <= n; ++i) dp[i] = 1;
res = n;
for (int k = n; k > 1; --k) {
for (auto i : vt[k]) upd(i.first, i.second);
ans += res;
}
printf("%lld\n", ans);
return 0;
}
| ### Prompt
Please create a solution in CPP to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, sl, fh, cnt, s[1000010], fa[1000010], dp[1000010], mxd[1000010],
f[1000010][20];
long long res, ans;
int t, h[1000010];
struct Tre {
int to, nxt;
} e[1000010 << 1];
vector<pair<int, int> > vt[1000010];
int rd() {
sl = 0;
fh = 1;
char ch = getchar();
while (ch < '0' || '9' < ch) {
if (ch == '-') fh = -1;
ch = getchar();
}
while ('0' <= ch && ch <= '9') sl = sl * 10 + ch - '0', ch = getchar();
return sl * fh;
}
void add(int u, int v) {
e[++t] = (Tre){v, h[u]};
h[u] = t;
e[++t] = (Tre){u, h[v]};
h[v] = t;
}
void dfs(int u) {
int v;
f[u][1] = n;
mxd[u] = 1;
for (int i = h[u]; i; i = e[i].nxt)
if ((v = e[i].to) != fa[u])
fa[v] = u, dfs(v), mxd[u] = max(mxd[u], mxd[v] + 1);
ans += mxd[u];
for (int i = 2; i < 20; ++i) {
cnt = 0;
for (int j = h[u]; j; j = e[j].nxt)
if ((v = e[j].to) != fa[u]) s[++cnt] = f[v][i - 1];
sort(s + 1, s + cnt + 1, [&](const int &x, const int &y) { return x > y; });
for (v = 0; v < cnt && s[v + 1] > v; ++v)
;
f[u][i] = v;
vt[v].push_back(make_pair(u, i));
}
}
void upd(int x, int v) {
for (; x && dp[x] < v; x = fa[x]) res += v - dp[x], dp[x] = v;
}
int main() {
n = rd();
int x, y;
for (int i = 1; i < n; ++i) x = rd(), y = rd(), add(x, y);
dfs(1);
for (int i = 1; i <= n; ++i) dp[i] = 1;
res = n;
for (int k = n; k > 1; --k) {
for (auto i : vt[k]) upd(i.first, i.second);
ans += res;
}
printf("%lld\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int N;
vector<int> G[300005];
int PosRes[22][300005], Pos[22][300005];
int V[22][300005];
int DP[300005];
vector<int> Who;
long long ans;
int Cnt[30];
void Read() {
scanf("%d", &N);
for (int i = 1; i < N; i++) {
int x, y;
scanf("%d%d", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
}
}
void DFS(int node, int father) {
Pos[1][node] = PosRes[1][node] = N;
for (int j = 2; (1 << (j - 1)) <= N; j++) Pos[j][node] = PosRes[j][node] = 1;
DP[node] = 1;
for (int i = 0; i < G[node].size(); i++) {
int neighb = G[node][i];
if (neighb == father) continue;
DFS(neighb, node);
DP[node] = max(DP[node], DP[neighb] + 1);
}
for (int j = 1; (1 << (j - 1)) <= N; j++) Cnt[j] = 0;
for (int i = 0; i < G[node].size(); i++) {
int neighb = G[node][i];
if (neighb == father) continue;
for (int j = 1; (1 << (j - 1)) <= N; j++) {
PosRes[j][node] = max(PosRes[j][node], PosRes[j][neighb]);
V[j][++Cnt[j]] = Pos[j][neighb];
}
}
for (int j = 1; (1 << (j - 1)) <= N; j++) {
sort(V[j] + 1, V[j] + Cnt[j] + 1);
}
for (int j = 2; (1 << (j - 1)) <= N; j++) {
int ind = Cnt[j - 1] - Pos[j][node] + 1;
while (Pos[j][node] <= N) {
ind--;
if (ind <= 0) break;
if (Pos[j][node] + 1 <= V[j - 1][ind]) {
++Pos[j][node];
} else
break;
}
PosRes[j][node] = max(PosRes[j][node], Pos[j][node]);
}
for (int j = 1; (1 << (j - 1)) <= N; j++) {
int next = (1 << j) > N ? 1 : PosRes[j + 1][node];
ans += 1LL * (PosRes[j][node] - next) * j;
}
}
int main() {
Read();
DFS(1, 0);
for (int i = 1; i <= N; i++) ans += DP[i];
printf("%I64d\n", ans);
return 0;
}
| ### Prompt
Create a solution in Cpp for the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int N;
vector<int> G[300005];
int PosRes[22][300005], Pos[22][300005];
int V[22][300005];
int DP[300005];
vector<int> Who;
long long ans;
int Cnt[30];
void Read() {
scanf("%d", &N);
for (int i = 1; i < N; i++) {
int x, y;
scanf("%d%d", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
}
}
void DFS(int node, int father) {
Pos[1][node] = PosRes[1][node] = N;
for (int j = 2; (1 << (j - 1)) <= N; j++) Pos[j][node] = PosRes[j][node] = 1;
DP[node] = 1;
for (int i = 0; i < G[node].size(); i++) {
int neighb = G[node][i];
if (neighb == father) continue;
DFS(neighb, node);
DP[node] = max(DP[node], DP[neighb] + 1);
}
for (int j = 1; (1 << (j - 1)) <= N; j++) Cnt[j] = 0;
for (int i = 0; i < G[node].size(); i++) {
int neighb = G[node][i];
if (neighb == father) continue;
for (int j = 1; (1 << (j - 1)) <= N; j++) {
PosRes[j][node] = max(PosRes[j][node], PosRes[j][neighb]);
V[j][++Cnt[j]] = Pos[j][neighb];
}
}
for (int j = 1; (1 << (j - 1)) <= N; j++) {
sort(V[j] + 1, V[j] + Cnt[j] + 1);
}
for (int j = 2; (1 << (j - 1)) <= N; j++) {
int ind = Cnt[j - 1] - Pos[j][node] + 1;
while (Pos[j][node] <= N) {
ind--;
if (ind <= 0) break;
if (Pos[j][node] + 1 <= V[j - 1][ind]) {
++Pos[j][node];
} else
break;
}
PosRes[j][node] = max(PosRes[j][node], Pos[j][node]);
}
for (int j = 1; (1 << (j - 1)) <= N; j++) {
int next = (1 << j) > N ? 1 : PosRes[j + 1][node];
ans += 1LL * (PosRes[j][node] - next) * j;
}
}
int main() {
Read();
DFS(1, 0);
for (int i = 1; i <= N; i++) ans += DP[i];
printf("%I64d\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, head[300005], num, x, y, dep[300005], Max[300005][21], own[300005][21];
long long ans;
struct edge {
int to, nxt;
} a[300005 << 1];
void add(int x, int y) { a[++num] = (edge){y, head[x]}, head[x] = num; }
bool check(int x, int f, int id, int y) {
int res = 0;
for (int e = head[x]; e; e = a[e].nxt) {
int k = a[e].to;
if (k == f) continue;
res += (own[k][id - 1] >= y);
}
return res >= y;
}
void dfs(int x, int f) {
dep[x] = 1;
own[x][1] = Max[x][1] = n;
for (int e = head[x]; e; e = a[e].nxt) {
int k = a[e].to;
if (k == f) continue;
dfs(k, x);
dep[x] = max(dep[x], dep[k] + 1);
for (int j = 2; j <= 20; ++j) {
Max[x][j] = max(Max[x][j], Max[k][j]);
}
}
int last = n;
for (int j = 2; j <= 20; ++j) {
int l = 1, r = last, mid;
while (l < r) {
mid = (l + r + 1) >> 1;
if (check(x, f, j, mid))
l = mid;
else
r = mid - 1;
}
own[x][j] = l;
Max[x][j] = max(Max[x][j], l);
last = l;
}
ans += dep[x];
for (int j = 1; j < 20; ++j) {
ans += j * (Max[x][j] - Max[x][j + 1]);
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; ++i) {
scanf("%d%d", &x, &y);
add(x, y), add(y, x);
}
dfs(1, 0);
printf("%I64d\n", ans);
return 0;
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, head[300005], num, x, y, dep[300005], Max[300005][21], own[300005][21];
long long ans;
struct edge {
int to, nxt;
} a[300005 << 1];
void add(int x, int y) { a[++num] = (edge){y, head[x]}, head[x] = num; }
bool check(int x, int f, int id, int y) {
int res = 0;
for (int e = head[x]; e; e = a[e].nxt) {
int k = a[e].to;
if (k == f) continue;
res += (own[k][id - 1] >= y);
}
return res >= y;
}
void dfs(int x, int f) {
dep[x] = 1;
own[x][1] = Max[x][1] = n;
for (int e = head[x]; e; e = a[e].nxt) {
int k = a[e].to;
if (k == f) continue;
dfs(k, x);
dep[x] = max(dep[x], dep[k] + 1);
for (int j = 2; j <= 20; ++j) {
Max[x][j] = max(Max[x][j], Max[k][j]);
}
}
int last = n;
for (int j = 2; j <= 20; ++j) {
int l = 1, r = last, mid;
while (l < r) {
mid = (l + r + 1) >> 1;
if (check(x, f, j, mid))
l = mid;
else
r = mid - 1;
}
own[x][j] = l;
Max[x][j] = max(Max[x][j], l);
last = l;
}
ans += dep[x];
for (int j = 1; j < 20; ++j) {
ans += j * (Max[x][j] - Max[x][j + 1]);
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; ++i) {
scanf("%d%d", &x, &y);
add(x, y), add(y, x);
}
dfs(1, 0);
printf("%I64d\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &x) {
x = 0;
char c = getchar(), f = 0;
for (; c < 48 || c > 57; c = getchar())
if (!(c ^ 45)) f = 1;
for (; c >= 48 && c <= 57; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
f ? x = -x : x;
}
const int N = 300005;
struct edge {
int to, nxt;
} e[N << 1];
int et, head[N];
int n, ln[N], dp[N][20];
long long rs = 0;
inline void adde(int x, int y) { e[++et] = (edge){y, head[x]}, head[x] = et; }
inline void dfs0(int x, int fa) {
ln[x] = 1;
for (int i = head[x]; i; i = e[i].nxt)
if (e[i].to != fa) dfs0(e[i].to, x), ln[x] = max(ln[e[i].to] + 1, ln[x]);
vector<int> v;
dp[x][1] = n, rs += ln[x];
for (int k = 2; k < 20; k++) {
v.clear();
for (int i = head[x]; i; i = e[i].nxt)
if (e[i].to != fa) v.push_back(dp[e[i].to][k - 1]);
sort(v.begin(), v.end(), greater<int>());
int id = 0;
for (; id < (int)v.size() && v[id] >= id + 1; id++)
;
dp[x][k] = id;
}
}
inline void dfs1(int x, int fa) {
for (int i = head[x]; i; i = e[i].nxt)
if (e[i].to != fa) dfs1(e[i].to, x);
for (int i = head[x]; i; i = e[i].nxt)
if (e[i].to != fa)
for (int k = 1; k < 20; k++) dp[x][k] = max(dp[x][k], dp[e[i].to][k]);
}
int main() {
read(n);
for (int i = 1, x, y; i < n; i++) read(x), read(y), adde(x, y), adde(y, x);
dfs0(1, 0), dfs1(1, 0);
for (int i = 1; i <= n; i++)
for (int j = 1; j < 20; j++) rs += max(dp[i][j] - 1, 0);
return printf("%lld\n", rs), 0;
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &x) {
x = 0;
char c = getchar(), f = 0;
for (; c < 48 || c > 57; c = getchar())
if (!(c ^ 45)) f = 1;
for (; c >= 48 && c <= 57; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
f ? x = -x : x;
}
const int N = 300005;
struct edge {
int to, nxt;
} e[N << 1];
int et, head[N];
int n, ln[N], dp[N][20];
long long rs = 0;
inline void adde(int x, int y) { e[++et] = (edge){y, head[x]}, head[x] = et; }
inline void dfs0(int x, int fa) {
ln[x] = 1;
for (int i = head[x]; i; i = e[i].nxt)
if (e[i].to != fa) dfs0(e[i].to, x), ln[x] = max(ln[e[i].to] + 1, ln[x]);
vector<int> v;
dp[x][1] = n, rs += ln[x];
for (int k = 2; k < 20; k++) {
v.clear();
for (int i = head[x]; i; i = e[i].nxt)
if (e[i].to != fa) v.push_back(dp[e[i].to][k - 1]);
sort(v.begin(), v.end(), greater<int>());
int id = 0;
for (; id < (int)v.size() && v[id] >= id + 1; id++)
;
dp[x][k] = id;
}
}
inline void dfs1(int x, int fa) {
for (int i = head[x]; i; i = e[i].nxt)
if (e[i].to != fa) dfs1(e[i].to, x);
for (int i = head[x]; i; i = e[i].nxt)
if (e[i].to != fa)
for (int k = 1; k < 20; k++) dp[x][k] = max(dp[x][k], dp[e[i].to][k]);
}
int main() {
read(n);
for (int i = 1, x, y; i < n; i++) read(x), read(y), adde(x, y), adde(y, x);
dfs0(1, 0), dfs1(1, 0);
for (int i = 1; i <= n; i++)
for (int j = 1; j < 20; j++) rs += max(dp[i][j] - 1, 0);
return printf("%lld\n", rs), 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void in(T &x) {
T c = getchar();
while (((c < 48) || (c > 57)) && (c != '-')) c = getchar();
bool neg = false;
if (c == '-') neg = true;
x = 0;
for (; c < 48 || c > 57; c = getchar())
;
for (; c > 47 && c < 58; c = getchar()) x = (x * 10) + (c - 48);
if (neg) x = -x;
}
const int MAXN = 3e5 + 10;
int n;
vector<int> adj[MAXN];
vector<vector<int> > values[MAXN];
vector<int> dp[MAXN];
int sub[MAXN];
void walk(int r, int p) {
values[r].resize(adj[r].size() + 1);
sub[r] = 1;
int sons = 0;
for (int c : adj[r]) {
if (c == p) continue;
walk(c, r), sons++;
sub[r] += sub[c];
}
dp[r].resize(sons + 1);
for (int i = 1; i < dp[r].size(); i++) {
if (values[r][i].size() >= i) {
sort(values[r][i].begin(), values[r][i].end(), greater<int>());
dp[r][i] = values[r][i][i - 1] + 1;
} else
dp[r][i] = 2;
if (p != -1 and values[p].size() > i) values[p][i].push_back(dp[r][i]);
}
}
set<pair<int, int> > data[MAXN];
long long ans = 0, curr = 0;
bool big[MAXN];
void add(int r, int p) {
for (int k = 1; k < dp[r].size(); k++) {
curr -= data[k].rbegin()->first;
data[k].insert({dp[r][k], r});
curr += data[k].rbegin()->first;
}
for (int c : adj[r]) {
if (c != p and big[c] == false) {
add(c, r);
}
}
}
void rem(int r, int p) {
for (int k = 1; k < dp[r].size(); k++) {
curr -= data[k].rbegin()->first;
data[k].erase({dp[r][k], r});
curr += data[k].rbegin()->first;
}
for (int c : adj[r]) {
if (c != p) {
rem(c, r);
}
}
}
void go(int r, int p, bool keep) {
int maxim = 0, bc = -1, sons = 0;
for (int c : adj[r]) {
if (c != p) {
if (maxim < sub[c]) maxim = sub[c], bc = c;
sons++;
}
}
if (bc != -1) big[bc] = true;
for (int c : adj[r]) {
if (c != p and big[c] == false) {
go(c, r, false);
}
}
if (bc != -1) go(bc, r, true);
add(r, p);
ans += curr;
if (bc != -1) big[bc] = false;
if (!keep) rem(r, p);
}
int main() {
int a, b;
in(n);
for (int i = 1; i < n; i++) {
in(a), in(b);
adj[a].push_back(b), adj[b].push_back(a);
}
walk(1, -1);
for (int i = 1; i <= n; i++) {
data[i].insert({1, -1});
}
curr = n;
go(1, -1, true);
printf("%lld\n", ans);
return 0;
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void in(T &x) {
T c = getchar();
while (((c < 48) || (c > 57)) && (c != '-')) c = getchar();
bool neg = false;
if (c == '-') neg = true;
x = 0;
for (; c < 48 || c > 57; c = getchar())
;
for (; c > 47 && c < 58; c = getchar()) x = (x * 10) + (c - 48);
if (neg) x = -x;
}
const int MAXN = 3e5 + 10;
int n;
vector<int> adj[MAXN];
vector<vector<int> > values[MAXN];
vector<int> dp[MAXN];
int sub[MAXN];
void walk(int r, int p) {
values[r].resize(adj[r].size() + 1);
sub[r] = 1;
int sons = 0;
for (int c : adj[r]) {
if (c == p) continue;
walk(c, r), sons++;
sub[r] += sub[c];
}
dp[r].resize(sons + 1);
for (int i = 1; i < dp[r].size(); i++) {
if (values[r][i].size() >= i) {
sort(values[r][i].begin(), values[r][i].end(), greater<int>());
dp[r][i] = values[r][i][i - 1] + 1;
} else
dp[r][i] = 2;
if (p != -1 and values[p].size() > i) values[p][i].push_back(dp[r][i]);
}
}
set<pair<int, int> > data[MAXN];
long long ans = 0, curr = 0;
bool big[MAXN];
void add(int r, int p) {
for (int k = 1; k < dp[r].size(); k++) {
curr -= data[k].rbegin()->first;
data[k].insert({dp[r][k], r});
curr += data[k].rbegin()->first;
}
for (int c : adj[r]) {
if (c != p and big[c] == false) {
add(c, r);
}
}
}
void rem(int r, int p) {
for (int k = 1; k < dp[r].size(); k++) {
curr -= data[k].rbegin()->first;
data[k].erase({dp[r][k], r});
curr += data[k].rbegin()->first;
}
for (int c : adj[r]) {
if (c != p) {
rem(c, r);
}
}
}
void go(int r, int p, bool keep) {
int maxim = 0, bc = -1, sons = 0;
for (int c : adj[r]) {
if (c != p) {
if (maxim < sub[c]) maxim = sub[c], bc = c;
sons++;
}
}
if (bc != -1) big[bc] = true;
for (int c : adj[r]) {
if (c != p and big[c] == false) {
go(c, r, false);
}
}
if (bc != -1) go(bc, r, true);
add(r, p);
ans += curr;
if (bc != -1) big[bc] = false;
if (!keep) rem(r, p);
}
int main() {
int a, b;
in(n);
for (int i = 1; i < n; i++) {
in(a), in(b);
adj[a].push_back(b), adj[b].push_back(a);
}
walk(1, -1);
for (int i = 1; i <= n; i++) {
data[i].insert({1, -1});
}
curr = n;
go(1, -1, true);
printf("%lld\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
const long long linf = 1ll * inf * inf;
const int N = 300000 + 7;
const int M = 20;
const int multipleTest = 0;
vector<int> adj[N];
int n;
long long ans = 0;
int maxLen[N];
int maxK[N][M];
int maxP[N][M];
bool check(int u, int len, int k, int r) {
int cnt = 0;
for (int v : adj[u])
if (v != r) {
if (maxK[v][len - 1] >= k) ++cnt;
}
return cnt >= k;
}
void dfs(int u, int r) {
maxLen[u] = 1;
for (int v : adj[u])
if (v != r) {
dfs(v, u);
maxLen[u] = max(maxLen[v] + 1, maxLen[u]);
}
ans += maxLen[u];
for (int len = M - 1; len > 1; --len) {
int lo = (len == M - 1) ? 2 : maxK[u][len + 1] + 1;
int hi = n;
int res = lo - 1;
while (lo <= hi) {
int mid = (lo + hi) >> 1;
if (check(u, len, mid, r)) {
res = mid;
lo = mid + 1;
} else
hi = mid - 1;
}
maxK[u][len] = res;
}
maxK[u][1] = n;
for (int len = M - 1; len > 0; --len) maxP[u][len] = maxK[u][len];
for (int len = M - 1; len > 0; --len) {
for (int v : adj[u])
if (v != r) {
maxP[u][len] = max(maxP[u][len], maxP[v][len]);
}
if (len == M - 1)
ans += (maxP[u][len] - 1) * len;
else
ans += (maxP[u][len] - maxP[u][len + 1]) * len;
}
}
void solve() {
cin >> n;
for (int i = (1), _b = (n); i < _b; ++i) {
int u, v;
scanf("%d%d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, -1);
cout << ans << '\n';
}
int main() {
int Test = 1;
if (multipleTest) {
cin >> Test;
}
for (int i = 0; i < Test; ++i) {
solve();
}
}
| ### Prompt
Construct a Cpp code solution to the problem outlined:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
const long long linf = 1ll * inf * inf;
const int N = 300000 + 7;
const int M = 20;
const int multipleTest = 0;
vector<int> adj[N];
int n;
long long ans = 0;
int maxLen[N];
int maxK[N][M];
int maxP[N][M];
bool check(int u, int len, int k, int r) {
int cnt = 0;
for (int v : adj[u])
if (v != r) {
if (maxK[v][len - 1] >= k) ++cnt;
}
return cnt >= k;
}
void dfs(int u, int r) {
maxLen[u] = 1;
for (int v : adj[u])
if (v != r) {
dfs(v, u);
maxLen[u] = max(maxLen[v] + 1, maxLen[u]);
}
ans += maxLen[u];
for (int len = M - 1; len > 1; --len) {
int lo = (len == M - 1) ? 2 : maxK[u][len + 1] + 1;
int hi = n;
int res = lo - 1;
while (lo <= hi) {
int mid = (lo + hi) >> 1;
if (check(u, len, mid, r)) {
res = mid;
lo = mid + 1;
} else
hi = mid - 1;
}
maxK[u][len] = res;
}
maxK[u][1] = n;
for (int len = M - 1; len > 0; --len) maxP[u][len] = maxK[u][len];
for (int len = M - 1; len > 0; --len) {
for (int v : adj[u])
if (v != r) {
maxP[u][len] = max(maxP[u][len], maxP[v][len]);
}
if (len == M - 1)
ans += (maxP[u][len] - 1) * len;
else
ans += (maxP[u][len] - maxP[u][len + 1]) * len;
}
}
void solve() {
cin >> n;
for (int i = (1), _b = (n); i < _b; ++i) {
int u, v;
scanf("%d%d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, -1);
cout << ans << '\n';
}
int main() {
int Test = 1;
if (multipleTest) {
cin >> Test;
}
for (int i = 0; i < Test; ++i) {
solve();
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5, M = 20;
long long ans, sum;
int n, h[N], edge[N << 1], nxt[N << 1], cnt, f[N][M], g[N], d[N], dep[N], p, q,
fa[N], a[N];
bool vis[N];
vector<pair<int, int> > hep[N];
inline void add(int x, int y) {
edge[++cnt] = y;
nxt[cnt] = h[x];
h[x] = cnt;
}
void dfs(int x) {
f[x][1] = n;
g[x] = 1;
for (int i = h[x]; i; i = nxt[i])
if (edge[i] != fa[x])
fa[edge[i]] = x, dfs(edge[i]), g[x] = max(g[x], g[edge[i]] + 1);
ans += g[x];
for (int j = 2; j < M; j++) {
int cnt = 0, i;
for (i = h[x]; i; i = nxt[i])
if (edge[i] != fa[x] && f[edge[i]][j - 1] > 0)
a[++cnt] = f[edge[i]][j - 1];
if (cnt == 0) break;
sort(a + 1, a + cnt + 1);
for (i = cnt; i > 0 && cnt - i + 1 <= a[i]; i--)
;
if (i == 0 || cnt - i + 1 > a[i]) i++;
f[x][j] = cnt - i + 1;
hep[f[x][j]].push_back(make_pair(x, j));
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
add(x, y);
add(y, x);
}
dfs(1);
sum = n;
for (int i = 1; i <= n; i++) g[i] = 1;
for (int k = n; k > 1; k--) {
vector<pair<int, int> >::iterator it;
for (it = hep[k].begin(); it != hep[k].end(); it++) {
pair<int, int> tmp = *it;
for (int i = tmp.first; i && g[i] < tmp.second; i = fa[i]) {
sum += tmp.second - g[i];
g[i] = tmp.second;
}
}
ans += sum;
}
printf("%lld\n", ans);
return 0;
}
| ### Prompt
Please formulate a Cpp solution to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5, M = 20;
long long ans, sum;
int n, h[N], edge[N << 1], nxt[N << 1], cnt, f[N][M], g[N], d[N], dep[N], p, q,
fa[N], a[N];
bool vis[N];
vector<pair<int, int> > hep[N];
inline void add(int x, int y) {
edge[++cnt] = y;
nxt[cnt] = h[x];
h[x] = cnt;
}
void dfs(int x) {
f[x][1] = n;
g[x] = 1;
for (int i = h[x]; i; i = nxt[i])
if (edge[i] != fa[x])
fa[edge[i]] = x, dfs(edge[i]), g[x] = max(g[x], g[edge[i]] + 1);
ans += g[x];
for (int j = 2; j < M; j++) {
int cnt = 0, i;
for (i = h[x]; i; i = nxt[i])
if (edge[i] != fa[x] && f[edge[i]][j - 1] > 0)
a[++cnt] = f[edge[i]][j - 1];
if (cnt == 0) break;
sort(a + 1, a + cnt + 1);
for (i = cnt; i > 0 && cnt - i + 1 <= a[i]; i--)
;
if (i == 0 || cnt - i + 1 > a[i]) i++;
f[x][j] = cnt - i + 1;
hep[f[x][j]].push_back(make_pair(x, j));
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
add(x, y);
add(y, x);
}
dfs(1);
sum = n;
for (int i = 1; i <= n; i++) g[i] = 1;
for (int k = n; k > 1; k--) {
vector<pair<int, int> >::iterator it;
for (it = hep[k].begin(); it != hep[k].end(); it++) {
pair<int, int> tmp = *it;
for (int i = tmp.first; i && g[i] < tmp.second; i = fa[i]) {
sum += tmp.second - g[i];
g[i] = tmp.second;
}
}
ans += sum;
}
printf("%lld\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5;
int n, dp[N][19];
long long ans;
vector<int> g[N];
int intial(int u, int p) {
int ret = 0;
for (auto i : g[u])
if (i != p) ret = max(ret, intial(i, u));
ans += ret + 1;
return ret + 1;
}
void dfs(int u, int p) {
bool leaf = 1;
dp[u][1] = n;
for (auto i : g[u])
if (i != p) {
leaf = 0;
dfs(i, u);
}
if (leaf) return;
for (int i = 2; i < 19; i++) {
vector<int> v;
for (auto j : g[u])
if (j != p && dp[j][i - 1] != -1) v.push_back(dp[j][i - 1]);
sort(v.rbegin(), v.rend());
for (int j = 0; j < v.size(); j++) {
if (v[j] <= j) break;
dp[u][i] = j + 1;
}
}
}
void find(int u, int p) {
for (auto i : g[u])
if (i != p) {
find(i, u);
for (int j = 0; j < 19; j++) dp[u][j] = max(dp[u][j], dp[i][j]);
}
int did = 1;
for (int i = 18; i; i--)
if (dp[u][i] != -1 && dp[u][i] > did) {
ans += i * 1LL * (dp[u][i] - did);
did = dp[u][i];
}
}
int main() {
scanf("%d", &n);
memset(dp, -1, sizeof dp);
for (int i = 1, a, b; i < n; i++) {
scanf("%d%d", &a, &b);
g[--a].push_back(--b);
g[b].push_back(a);
}
intial(0, -1);
dfs(0, -1);
find(0, -1);
printf("%lld\n", ans);
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5;
int n, dp[N][19];
long long ans;
vector<int> g[N];
int intial(int u, int p) {
int ret = 0;
for (auto i : g[u])
if (i != p) ret = max(ret, intial(i, u));
ans += ret + 1;
return ret + 1;
}
void dfs(int u, int p) {
bool leaf = 1;
dp[u][1] = n;
for (auto i : g[u])
if (i != p) {
leaf = 0;
dfs(i, u);
}
if (leaf) return;
for (int i = 2; i < 19; i++) {
vector<int> v;
for (auto j : g[u])
if (j != p && dp[j][i - 1] != -1) v.push_back(dp[j][i - 1]);
sort(v.rbegin(), v.rend());
for (int j = 0; j < v.size(); j++) {
if (v[j] <= j) break;
dp[u][i] = j + 1;
}
}
}
void find(int u, int p) {
for (auto i : g[u])
if (i != p) {
find(i, u);
for (int j = 0; j < 19; j++) dp[u][j] = max(dp[u][j], dp[i][j]);
}
int did = 1;
for (int i = 18; i; i--)
if (dp[u][i] != -1 && dp[u][i] > did) {
ans += i * 1LL * (dp[u][i] - did);
did = dp[u][i];
}
}
int main() {
scanf("%d", &n);
memset(dp, -1, sizeof dp);
for (int i = 1, a, b; i < n; i++) {
scanf("%d%d", &a, &b);
g[--a].push_back(--b);
g[b].push_back(a);
}
intial(0, -1);
dfs(0, -1);
find(0, -1);
printf("%lld\n", ans);
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long N = 3e5 + 2;
const long long inf = 1e9 + 7;
long long dp[N][22], f[N], max1[N][22];
long long ans = 0;
vector<long long> adj[N];
void dfs(long long x, long long p) {
vector<long long> temp[22];
long long i, j, lef, rig, mid;
for (i = 0; i < adj[x].size(); i++) {
if (adj[x][i] != p) {
dfs(adj[x][i], x);
f[x] = max(f[x], f[adj[x][i]] + 1);
for (j = 1; j <= 20; j++) {
if (dp[adj[x][i]][j - 1] != 0) {
temp[j].push_back(dp[adj[x][i]][j - 1]);
}
max1[x][j] = max(max1[x][j], max1[adj[x][i]][j]);
}
dp[x][1]++;
}
}
for (i = 2; i <= 20; i++) {
sort(temp[i].begin(), temp[i].end(),
[&](long long x, long long y) { return x > y; });
rig = temp[i].size();
lef = 1;
while (rig > lef) {
mid = (lef + rig + 1) / 2;
if (temp[i][mid - 1] >= mid) {
lef = mid;
} else {
rig = mid - 1;
}
}
dp[x][i] = rig;
}
for (i = 1; i <= 20; i++) {
max1[x][i] = max(max1[x][i], dp[x][i]);
}
for (i = 1; i <= 20; i++) {
j = 2;
if (i != 20) {
j = max(j, max1[x][i + 1] + 1);
}
if (max1[x][i] >= j) {
ans += (max1[x][i] - j + 1) * i;
}
}
ans += f[x];
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
long long n, i, j, k, l;
cin >> n;
for (i = 1; i < n; i++) {
cin >> j >> k;
adj[j].push_back(k);
adj[k].push_back(j);
}
dfs(1, 1);
cout << ans + n * n;
}
| ### Prompt
In cpp, your task is to solve the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long N = 3e5 + 2;
const long long inf = 1e9 + 7;
long long dp[N][22], f[N], max1[N][22];
long long ans = 0;
vector<long long> adj[N];
void dfs(long long x, long long p) {
vector<long long> temp[22];
long long i, j, lef, rig, mid;
for (i = 0; i < adj[x].size(); i++) {
if (adj[x][i] != p) {
dfs(adj[x][i], x);
f[x] = max(f[x], f[adj[x][i]] + 1);
for (j = 1; j <= 20; j++) {
if (dp[adj[x][i]][j - 1] != 0) {
temp[j].push_back(dp[adj[x][i]][j - 1]);
}
max1[x][j] = max(max1[x][j], max1[adj[x][i]][j]);
}
dp[x][1]++;
}
}
for (i = 2; i <= 20; i++) {
sort(temp[i].begin(), temp[i].end(),
[&](long long x, long long y) { return x > y; });
rig = temp[i].size();
lef = 1;
while (rig > lef) {
mid = (lef + rig + 1) / 2;
if (temp[i][mid - 1] >= mid) {
lef = mid;
} else {
rig = mid - 1;
}
}
dp[x][i] = rig;
}
for (i = 1; i <= 20; i++) {
max1[x][i] = max(max1[x][i], dp[x][i]);
}
for (i = 1; i <= 20; i++) {
j = 2;
if (i != 20) {
j = max(j, max1[x][i + 1] + 1);
}
if (max1[x][i] >= j) {
ans += (max1[x][i] - j + 1) * i;
}
}
ans += f[x];
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
long long n, i, j, k, l;
cin >> n;
for (i = 1; i < n; i++) {
cin >> j >> k;
adj[j].push_back(k);
adj[k].push_back(j);
}
dfs(1, 1);
cout << ans + n * n;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 3e5 + 10, LGMAX = 19;
int N;
int MaxLevel;
int64_t answer;
int DP[LGMAX][NMAX], father[NMAX], val[NMAX];
vector<int> T[NMAX], LevelNodes[NMAX];
vector<pair<int, int>> GoUp[NMAX];
int DFS(int node, int from, int level) {
father[node] = from;
MaxLevel = max(MaxLevel, level);
LevelNodes[level].push_back(node);
if (from != -1 && T[node].size() == 1) {
++answer;
return 1;
}
int height = 1;
for (int next : T[node]) {
if (next == from) continue;
++DP[2][node];
height = max(height, 1 + DFS(next, node, level + 1));
}
GoUp[DP[2][node]].push_back({2, node});
answer += height;
return height;
}
int main() {
scanf("%d", &N);
for (int i = 0; i < N - 1; ++i) {
int x, y;
scanf("%d %d", &x, &y);
T[x].push_back(y);
T[y].push_back(x);
}
DFS(1, -1, 0);
for (int i = 3; i < LGMAX; ++i) {
for (int j = MaxLevel; j >= 0; --j) {
for (int node : LevelNodes[j]) {
vector<int> values;
for (int next : T[node]) {
if (next == father[node]) continue;
values.push_back(DP[i - 1][next]);
}
sort(values.begin(), values.end(), greater<int>());
int k = 0;
while (k < (int)values.size() && k + 1 <= values[k]) ++k;
if (k > 0 && k <= values[k - 1]) {
DP[i][node] = k;
GoUp[DP[i][node]].push_back({i, node});
}
}
}
}
int64_t value = N;
for (int i = 1; i <= N; ++i) val[i] = 1;
for (int i = N; i >= 2; --i) {
for (auto det : GoUp[i]) {
int depth = det.first;
int node = det.second;
while (node != -1 && depth > val[node]) {
value += depth - val[node];
val[node] = depth;
node = father[node];
}
}
answer += value;
}
cout << answer << '\n';
return 0;
}
| ### Prompt
Generate a CPP solution to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 3e5 + 10, LGMAX = 19;
int N;
int MaxLevel;
int64_t answer;
int DP[LGMAX][NMAX], father[NMAX], val[NMAX];
vector<int> T[NMAX], LevelNodes[NMAX];
vector<pair<int, int>> GoUp[NMAX];
int DFS(int node, int from, int level) {
father[node] = from;
MaxLevel = max(MaxLevel, level);
LevelNodes[level].push_back(node);
if (from != -1 && T[node].size() == 1) {
++answer;
return 1;
}
int height = 1;
for (int next : T[node]) {
if (next == from) continue;
++DP[2][node];
height = max(height, 1 + DFS(next, node, level + 1));
}
GoUp[DP[2][node]].push_back({2, node});
answer += height;
return height;
}
int main() {
scanf("%d", &N);
for (int i = 0; i < N - 1; ++i) {
int x, y;
scanf("%d %d", &x, &y);
T[x].push_back(y);
T[y].push_back(x);
}
DFS(1, -1, 0);
for (int i = 3; i < LGMAX; ++i) {
for (int j = MaxLevel; j >= 0; --j) {
for (int node : LevelNodes[j]) {
vector<int> values;
for (int next : T[node]) {
if (next == father[node]) continue;
values.push_back(DP[i - 1][next]);
}
sort(values.begin(), values.end(), greater<int>());
int k = 0;
while (k < (int)values.size() && k + 1 <= values[k]) ++k;
if (k > 0 && k <= values[k - 1]) {
DP[i][node] = k;
GoUp[DP[i][node]].push_back({i, node});
}
}
}
}
int64_t value = N;
for (int i = 1; i <= N; ++i) val[i] = 1;
for (int i = N; i >= 2; --i) {
for (auto det : GoUp[i]) {
int depth = det.first;
int node = det.second;
while (node != -1 && depth > val[node]) {
value += depth - val[node];
val[node] = depth;
node = father[node];
}
}
answer += value;
}
cout << answer << '\n';
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
bool cmax(T& a, const U& b) {
return a < b ? a = b, 1 : 0;
}
template <class T, class U>
bool cmin(T& a, const U& b) {
return b < a ? a = b, 1 : 0;
}
void _BG(const char* s) { cerr << s << endl; };
template <class T, class... TT>
void _BG(const char* s, T a, TT... b) {
for (int c = 0; *s && (c || *s != ','); ++s) {
cerr << *s;
for (char x : "([{") c += *s == x;
for (char x : ")]}") c -= *s == x;
}
cerr << " = " << a;
if (sizeof...(b)) {
cerr << ", ";
++s;
}
_BG(s, b...);
}
bool RD() { return 1; }
bool RD(char& a) { return scanf(" %c", &a) == 1; }
bool RD(char* a) { return scanf("%s", a) == 1; }
bool RD(double& a) { return scanf("%lf", &a) == 1; }
bool RD(int& a) { return scanf("%d", &a) == 1; }
bool RD(long long& a) { return scanf("%lld", &a) == 1; }
template <class T, class... TT>
bool RD(T& a, TT&... b) {
return RD(a) && RD(b...);
}
void PT(const char& a) { putchar(a); }
void PT(char const* const& a) { fputs(a, stdout); }
void PT(const double& a) { printf("%.16f", a); }
void PT(const int& a) { printf("%d", a); }
void PT(const long long& a) { printf("%lld", a); }
template <char s = ' ', char e = '\n'>
void PL() {
if (e) PT(e);
}
template <char s = ' ', char e = '\n', class T, class... TT>
void PL(const T& a, const TT&... b) {
PT(a);
if (sizeof...(b) && s) PT(s);
PL<s, e>(b...);
}
const int N = 3e5 + 87;
int n;
struct dp {
vector<int> v;
long long s;
void upd(int i, int x) {
if (((int)(v).size()) <= i) v.resize(i + 1);
int y = max(v[i], x);
s += y - v[i];
v[i] = y;
}
void join(dp& x) {
if (x.v.size() > v.size()) {
s = x.s;
v.swap(x.v);
}
for (int i(0); i < (((int)(x.v).size())); ++i) upd(i, x.v[i]);
}
long long sum() { return s + n - ((int)(v).size()); }
void dbg(int u) {
for (int i(0); i < (((int)(v).size())); ++i)
cerr << "dp[" << i + 1 << "][" << u << "]=" << v[i] << endl;
for (int i(((int)(v).size())); i < (n); ++i)
cerr << "dp[" << i + 1 << "][" << u << "]=" << 1 << endl;
}
} d[N];
vector<int> g[N], ans[N];
long long dfs(int p, int u) {
int k = ((int)(g[u]).size()) + (p ? -1 : 0);
vector<priority_queue<int, vector<int>, greater<int>>> pq(k);
long long sum = 0;
for (int v : g[u])
if (v != p) {
sum += dfs(u, v);
d[u].join(d[v]);
for (int i(0); i < (min(k, ((int)(ans[v]).size()))); ++i) {
pq[i].push(ans[v][i]);
if (((int)(pq[i]).size()) == i + 2) pq[i].pop();
}
}
ans[u].resize(k);
for (int i(0); i < (k); ++i)
d[u].upd(i,
ans[u][i] = ((int)(pq[i]).size()) == i + 1 ? pq[i].top() + 1 : 2);
return sum + d[u].sum();
}
int main() {
RD(n);
for (int __i(0); __i < (n - 1); ++__i) {
int u, v;
RD(u, v);
g[u].push_back(v);
g[v].push_back(u);
}
PL(dfs(0, 1));
}
| ### Prompt
In CPP, your task is to solve the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
bool cmax(T& a, const U& b) {
return a < b ? a = b, 1 : 0;
}
template <class T, class U>
bool cmin(T& a, const U& b) {
return b < a ? a = b, 1 : 0;
}
void _BG(const char* s) { cerr << s << endl; };
template <class T, class... TT>
void _BG(const char* s, T a, TT... b) {
for (int c = 0; *s && (c || *s != ','); ++s) {
cerr << *s;
for (char x : "([{") c += *s == x;
for (char x : ")]}") c -= *s == x;
}
cerr << " = " << a;
if (sizeof...(b)) {
cerr << ", ";
++s;
}
_BG(s, b...);
}
bool RD() { return 1; }
bool RD(char& a) { return scanf(" %c", &a) == 1; }
bool RD(char* a) { return scanf("%s", a) == 1; }
bool RD(double& a) { return scanf("%lf", &a) == 1; }
bool RD(int& a) { return scanf("%d", &a) == 1; }
bool RD(long long& a) { return scanf("%lld", &a) == 1; }
template <class T, class... TT>
bool RD(T& a, TT&... b) {
return RD(a) && RD(b...);
}
void PT(const char& a) { putchar(a); }
void PT(char const* const& a) { fputs(a, stdout); }
void PT(const double& a) { printf("%.16f", a); }
void PT(const int& a) { printf("%d", a); }
void PT(const long long& a) { printf("%lld", a); }
template <char s = ' ', char e = '\n'>
void PL() {
if (e) PT(e);
}
template <char s = ' ', char e = '\n', class T, class... TT>
void PL(const T& a, const TT&... b) {
PT(a);
if (sizeof...(b) && s) PT(s);
PL<s, e>(b...);
}
const int N = 3e5 + 87;
int n;
struct dp {
vector<int> v;
long long s;
void upd(int i, int x) {
if (((int)(v).size()) <= i) v.resize(i + 1);
int y = max(v[i], x);
s += y - v[i];
v[i] = y;
}
void join(dp& x) {
if (x.v.size() > v.size()) {
s = x.s;
v.swap(x.v);
}
for (int i(0); i < (((int)(x.v).size())); ++i) upd(i, x.v[i]);
}
long long sum() { return s + n - ((int)(v).size()); }
void dbg(int u) {
for (int i(0); i < (((int)(v).size())); ++i)
cerr << "dp[" << i + 1 << "][" << u << "]=" << v[i] << endl;
for (int i(((int)(v).size())); i < (n); ++i)
cerr << "dp[" << i + 1 << "][" << u << "]=" << 1 << endl;
}
} d[N];
vector<int> g[N], ans[N];
long long dfs(int p, int u) {
int k = ((int)(g[u]).size()) + (p ? -1 : 0);
vector<priority_queue<int, vector<int>, greater<int>>> pq(k);
long long sum = 0;
for (int v : g[u])
if (v != p) {
sum += dfs(u, v);
d[u].join(d[v]);
for (int i(0); i < (min(k, ((int)(ans[v]).size()))); ++i) {
pq[i].push(ans[v][i]);
if (((int)(pq[i]).size()) == i + 2) pq[i].pop();
}
}
ans[u].resize(k);
for (int i(0); i < (k); ++i)
d[u].upd(i,
ans[u][i] = ((int)(pq[i]).size()) == i + 1 ? pq[i].top() + 1 : 2);
return sum + d[u].sum();
}
int main() {
RD(n);
for (int __i(0); __i < (n - 1); ++__i) {
int u, v;
RD(u, v);
g[u].push_back(v);
g[v].push_back(u);
}
PL(dfs(0, 1));
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int a, long long int b) {
return (b == 0LL ? a : gcd(b, a % b));
}
long double dist(long double x, long double arayikhalatyan, long double x2,
long double y2) {
return sqrt((x - x2) * (x - x2) +
(arayikhalatyan - y2) * (arayikhalatyan - y2));
}
long long int S(long long int a) { return (a * (a - 1LL)) / 2; }
mt19937 rnd(363542);
char vow[] = {'a', 'e', 'i', 'o', 'u'};
int dx[] = {0, -1, 0, 1, -1, -1, 1, 1, 0};
int dy[] = {-1, 0, 1, 0, -1, 1, -1, 1, 0};
const int N = 3e5 + 30;
const long long int mod = 1e15 + 7;
const long double pi = acos(-1);
const int T = 550;
long long int bp(long long int a, long long int b = mod - 2LL) {
long long int ret = 1;
while (b) {
if (b & 1) ret *= a, ret %= mod;
a *= a;
a %= mod;
b >>= 1;
}
return ret;
}
ostream& operator<<(ostream& c, pair<int, int> a) {
c << a.first << " " << a.second;
return c;
}
void maxi(long long int& a, long long int b) { a = max(a, b); }
int n;
vector<int> g[N];
int a[N][101], b[N][101];
long long int pat;
long long int dfs(int v, int par) {
long long int mx = 0, qn = 0;
for (auto p : g[v]) {
if (p == par) continue;
qn++;
mx = max(mx, dfs(p, v));
for (int i = 1; i <= min(n, 70); i++) b[v][i] = max(b[v][i], b[p][i]);
}
mx = max(mx, qn);
if (n >= 71) {
if (mx >= 71)
pat += 2LL * (mx - 70) + n - mx;
else
pat += (long long int)(n - 70);
}
for (int i = 1; i <= min(n, 70); i++) {
if (qn < i) {
a[v][i] = 1;
b[v][i] = max(b[v][i], a[v][i]);
pat += b[v][i];
continue;
}
vector<int> fp;
for (auto p : g[v]) {
if (p == par) continue;
fp.push_back(-a[p][i]);
}
nth_element(fp.begin(), fp.begin() + i - 1, fp.end());
a[v][i] = -fp[i - 1] + 1;
b[v][i] = max(b[v][i], a[v][i]);
pat += b[v][i];
}
return mx;
}
long long int dfs1(int v, int par) {
long long int mx = 0;
vector<int> fp;
for (auto p : g[v]) {
if (p == par) continue;
mx = max(mx, dfs1(p, v));
fp.push_back(g[p].size() - 1);
}
sort((fp).begin(), (fp).end());
reverse((fp).begin(), (fp).end());
bool bl = 0;
for (int i = 0; i < fp.size(); i++) {
if (fp[i] < i + 1) {
bl = 1;
mx = max(mx, (long long int)i);
break;
}
}
if (!bl) mx = max(mx, (long long int)fp.size());
pat += max(0LL, mx - 70);
return mx;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
cin >> n;
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
dfs(1, 1);
dfs1(1, 1);
cout << pat << endl;
return 0;
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int a, long long int b) {
return (b == 0LL ? a : gcd(b, a % b));
}
long double dist(long double x, long double arayikhalatyan, long double x2,
long double y2) {
return sqrt((x - x2) * (x - x2) +
(arayikhalatyan - y2) * (arayikhalatyan - y2));
}
long long int S(long long int a) { return (a * (a - 1LL)) / 2; }
mt19937 rnd(363542);
char vow[] = {'a', 'e', 'i', 'o', 'u'};
int dx[] = {0, -1, 0, 1, -1, -1, 1, 1, 0};
int dy[] = {-1, 0, 1, 0, -1, 1, -1, 1, 0};
const int N = 3e5 + 30;
const long long int mod = 1e15 + 7;
const long double pi = acos(-1);
const int T = 550;
long long int bp(long long int a, long long int b = mod - 2LL) {
long long int ret = 1;
while (b) {
if (b & 1) ret *= a, ret %= mod;
a *= a;
a %= mod;
b >>= 1;
}
return ret;
}
ostream& operator<<(ostream& c, pair<int, int> a) {
c << a.first << " " << a.second;
return c;
}
void maxi(long long int& a, long long int b) { a = max(a, b); }
int n;
vector<int> g[N];
int a[N][101], b[N][101];
long long int pat;
long long int dfs(int v, int par) {
long long int mx = 0, qn = 0;
for (auto p : g[v]) {
if (p == par) continue;
qn++;
mx = max(mx, dfs(p, v));
for (int i = 1; i <= min(n, 70); i++) b[v][i] = max(b[v][i], b[p][i]);
}
mx = max(mx, qn);
if (n >= 71) {
if (mx >= 71)
pat += 2LL * (mx - 70) + n - mx;
else
pat += (long long int)(n - 70);
}
for (int i = 1; i <= min(n, 70); i++) {
if (qn < i) {
a[v][i] = 1;
b[v][i] = max(b[v][i], a[v][i]);
pat += b[v][i];
continue;
}
vector<int> fp;
for (auto p : g[v]) {
if (p == par) continue;
fp.push_back(-a[p][i]);
}
nth_element(fp.begin(), fp.begin() + i - 1, fp.end());
a[v][i] = -fp[i - 1] + 1;
b[v][i] = max(b[v][i], a[v][i]);
pat += b[v][i];
}
return mx;
}
long long int dfs1(int v, int par) {
long long int mx = 0;
vector<int> fp;
for (auto p : g[v]) {
if (p == par) continue;
mx = max(mx, dfs1(p, v));
fp.push_back(g[p].size() - 1);
}
sort((fp).begin(), (fp).end());
reverse((fp).begin(), (fp).end());
bool bl = 0;
for (int i = 0; i < fp.size(); i++) {
if (fp[i] < i + 1) {
bl = 1;
mx = max(mx, (long long int)i);
break;
}
}
if (!bl) mx = max(mx, (long long int)fp.size());
pat += max(0LL, mx - 70);
return mx;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
cin >> n;
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
dfs(1, 1);
dfs1(1, 1);
cout << pat << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:66777216")
using namespace std;
const int N = 300000 + 10, M = 20 + 2;
int n;
vector<int> V[N];
int depth[N];
int d[M][N], d_max[M][N];
void dfs(int v, int parent) {
depth[v] = 1;
for (int i = 0; i < (((int)((V[v]).size()))); ++i) {
int to = V[v][i];
if (to == parent) continue;
dfs(to, v);
depth[v] = max(depth[v], depth[to] + 1);
}
for (int i = (int)(2); i <= (int)(M - 1); ++i) {
d[i][v] = 1;
int b = 1, e = n;
while (b < e) {
int m = (b + e + 1) / 2;
int cnt = 0;
for (int j = 0; j < (((int)((V[v]).size()))); ++j) {
int to = V[v][j];
if (to == parent) continue;
if (d[i - 1][to] >= m) cnt++;
}
if (cnt >= m)
b = m;
else
e = m - 1;
}
d[i][v] = max(d[i][v], b);
}
}
void dfs2(int v, int parent) {
for (int j = 0; j < (M); ++j) d_max[j][v] = d[j][v];
for (int i = 0; i < (((int)((V[v]).size()))); ++i) {
int to = V[v][i];
if (to == parent) continue;
dfs2(to, v);
for (int j = 0; j < (M); ++j) d_max[j][v] = max(d_max[j][v], d_max[j][to]);
}
}
void sol() {
scanf("%d", &n);
for (int i = 0; i < (n - 1); ++i) {
int x, y;
scanf("%d%d", &x, &y);
V[x].push_back(y);
V[y].push_back(x);
}
for (int i = 1; i <= (int)(n); ++i) d[1][i] = n;
dfs(1, -1);
dfs2(1, -1);
long long ans = 0;
for (int i = 1; i <= (int)(n); ++i) {
ans += depth[i];
for (int m = 1; m <= (int)(M - 1); ++m) ans += max(0, d_max[m][i] - 1);
}
cout << ans << endl;
}
void clear() {}
void once() {}
void solve() {
int T;
T = 1;
once();
for (int i = 1; i <= (int)(T); ++i) {
clear();
sol();
}
}
void testgen() {
FILE* f = fopen("input.txt", "w");
int n = 2000, m = 1000000000;
fprintf(f, "1\n", n);
fprintf(f, "%d %d\n", n, m);
srand(time(NULL));
for (int i = 0; i < (n); ++i) fprintf(f, "%d\n", rand() % 2000);
fclose(f);
}
int main() {
cout << fixed;
cout.precision(10);
cerr << fixed;
cerr.precision(3);
solve();
return 0;
}
| ### Prompt
Develop a solution in Cpp to the problem described below:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:66777216")
using namespace std;
const int N = 300000 + 10, M = 20 + 2;
int n;
vector<int> V[N];
int depth[N];
int d[M][N], d_max[M][N];
void dfs(int v, int parent) {
depth[v] = 1;
for (int i = 0; i < (((int)((V[v]).size()))); ++i) {
int to = V[v][i];
if (to == parent) continue;
dfs(to, v);
depth[v] = max(depth[v], depth[to] + 1);
}
for (int i = (int)(2); i <= (int)(M - 1); ++i) {
d[i][v] = 1;
int b = 1, e = n;
while (b < e) {
int m = (b + e + 1) / 2;
int cnt = 0;
for (int j = 0; j < (((int)((V[v]).size()))); ++j) {
int to = V[v][j];
if (to == parent) continue;
if (d[i - 1][to] >= m) cnt++;
}
if (cnt >= m)
b = m;
else
e = m - 1;
}
d[i][v] = max(d[i][v], b);
}
}
void dfs2(int v, int parent) {
for (int j = 0; j < (M); ++j) d_max[j][v] = d[j][v];
for (int i = 0; i < (((int)((V[v]).size()))); ++i) {
int to = V[v][i];
if (to == parent) continue;
dfs2(to, v);
for (int j = 0; j < (M); ++j) d_max[j][v] = max(d_max[j][v], d_max[j][to]);
}
}
void sol() {
scanf("%d", &n);
for (int i = 0; i < (n - 1); ++i) {
int x, y;
scanf("%d%d", &x, &y);
V[x].push_back(y);
V[y].push_back(x);
}
for (int i = 1; i <= (int)(n); ++i) d[1][i] = n;
dfs(1, -1);
dfs2(1, -1);
long long ans = 0;
for (int i = 1; i <= (int)(n); ++i) {
ans += depth[i];
for (int m = 1; m <= (int)(M - 1); ++m) ans += max(0, d_max[m][i] - 1);
}
cout << ans << endl;
}
void clear() {}
void once() {}
void solve() {
int T;
T = 1;
once();
for (int i = 1; i <= (int)(T); ++i) {
clear();
sol();
}
}
void testgen() {
FILE* f = fopen("input.txt", "w");
int n = 2000, m = 1000000000;
fprintf(f, "1\n", n);
fprintf(f, "%d %d\n", n, m);
srand(time(NULL));
for (int i = 0; i < (n); ++i) fprintf(f, "%d\n", rand() % 2000);
fclose(f);
}
int main() {
cout << fixed;
cout.precision(10);
cerr << fixed;
cerr.precision(3);
solve();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long ans;
int dp[300000 + 5][20], n;
vector<int> e[300000 + 5];
void upd(int &x, int y) {
if (x < y) x = y;
}
int dfs(int now, int fa) {
int mxdep = 0;
for (auto v : e[now])
if (v != fa) upd(mxdep, dfs(v, now));
mxdep++;
ans += mxdep;
dp[now][1] = n;
for (int m = 2; m <= 19; m++) {
vector<int> tmp;
for (auto v : e[now])
if (v != fa) tmp.push_back(dp[v][m - 1]);
sort(tmp.begin(), tmp.end(), greater<int>());
int t = 1;
while (t <= (int)tmp.size() && tmp[t - 1] >= t) t++;
dp[now][m] = t - 1;
upd(dp[now][m], 1);
}
return mxdep;
}
void dfs2(int now, int fa) {
for (auto v : e[now])
if (v != fa) {
dfs2(v, now);
for (int m = 1; m <= 19; m++) upd(dp[now][m], dp[v][m]);
}
for (int m = 1; m <= 18; m++) ans += 1ll * m * (dp[now][m] - dp[now][m + 1]);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n - 1; i++) {
int x, y;
scanf("%d%d", &x, &y);
e[x].push_back(y);
e[y].push_back(x);
}
dfs(1, 0);
dfs2(1, 0);
printf("%lld\n", ans);
return 0;
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long ans;
int dp[300000 + 5][20], n;
vector<int> e[300000 + 5];
void upd(int &x, int y) {
if (x < y) x = y;
}
int dfs(int now, int fa) {
int mxdep = 0;
for (auto v : e[now])
if (v != fa) upd(mxdep, dfs(v, now));
mxdep++;
ans += mxdep;
dp[now][1] = n;
for (int m = 2; m <= 19; m++) {
vector<int> tmp;
for (auto v : e[now])
if (v != fa) tmp.push_back(dp[v][m - 1]);
sort(tmp.begin(), tmp.end(), greater<int>());
int t = 1;
while (t <= (int)tmp.size() && tmp[t - 1] >= t) t++;
dp[now][m] = t - 1;
upd(dp[now][m], 1);
}
return mxdep;
}
void dfs2(int now, int fa) {
for (auto v : e[now])
if (v != fa) {
dfs2(v, now);
for (int m = 1; m <= 19; m++) upd(dp[now][m], dp[v][m]);
}
for (int m = 1; m <= 18; m++) ans += 1ll * m * (dp[now][m] - dp[now][m + 1]);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n - 1; i++) {
int x, y;
scanf("%d%d", &x, &y);
e[x].push_back(y);
e[y].push_back(x);
}
dfs(1, 0);
dfs2(1, 0);
printf("%lld\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n;
const int MaxN = 3e5;
vector<int> adj[MaxN];
vector<int> prec[MaxN];
void dfs(const int u, const int p) {
prec[u] = {1, n};
adj[u].erase(remove(adj[u].begin(), adj[u].end(), p), adj[u].end());
for (auto &&v : adj[u]) {
dfs(v, u);
prec[u][0] = max(prec[u][0], 1 + prec[v][0]);
}
for (int i = 2;; ++i) {
int lo = 1;
int hi = adj[u].size();
while (lo < hi) {
int mid = (lo + hi + 1) >> 1;
int count = 0;
for (auto &&v : adj[u])
count += i <= prec[v].size() && prec[v][i - 1] >= mid;
if (count >= mid)
lo = mid;
else
hi = mid - 1;
}
if (lo <= 1) break;
prec[u].push_back(lo);
}
}
void dfs2(const int u, long long &ans) {
for (auto &&v : adj[u]) {
dfs2(v, ans);
if (prec[u].size() < prec[v].size()) prec[u].resize(prec[v].size(), 0);
for (int i = 2; i < prec[v].size(); ++i)
prec[u][i] = max(prec[u][i], prec[v][i]);
}
ans -= prec[u].size() - 1;
for (auto &&x : prec[u]) ans += x;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
--u, --v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(0, -1);
long long ans = 0;
dfs2(0, ans);
cout << ans;
return 0;
}
| ### Prompt
Your task is to create a CPP solution to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n;
const int MaxN = 3e5;
vector<int> adj[MaxN];
vector<int> prec[MaxN];
void dfs(const int u, const int p) {
prec[u] = {1, n};
adj[u].erase(remove(adj[u].begin(), adj[u].end(), p), adj[u].end());
for (auto &&v : adj[u]) {
dfs(v, u);
prec[u][0] = max(prec[u][0], 1 + prec[v][0]);
}
for (int i = 2;; ++i) {
int lo = 1;
int hi = adj[u].size();
while (lo < hi) {
int mid = (lo + hi + 1) >> 1;
int count = 0;
for (auto &&v : adj[u])
count += i <= prec[v].size() && prec[v][i - 1] >= mid;
if (count >= mid)
lo = mid;
else
hi = mid - 1;
}
if (lo <= 1) break;
prec[u].push_back(lo);
}
}
void dfs2(const int u, long long &ans) {
for (auto &&v : adj[u]) {
dfs2(v, ans);
if (prec[u].size() < prec[v].size()) prec[u].resize(prec[v].size(), 0);
for (int i = 2; i < prec[v].size(); ++i)
prec[u][i] = max(prec[u][i], prec[v][i]);
}
ans -= prec[u].size() - 1;
for (auto &&x : prec[u]) ans += x;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
--u, --v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(0, -1);
long long ans = 0;
dfs2(0, ans);
cout << ans;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int head[524288], last[1048576], to[1048576], cnt = 0;
void add(int u, int v) {
cnt++;
last[cnt] = head[u];
head[u] = cnt;
to[cnt] = v;
}
int dp[524288];
int a[524288];
long long answer = 0;
int n;
void dfs1(int u, int f, int k) {
for (int i = head[u]; i; i = last[i]) {
int v = to[i];
if (v == f) {
continue;
}
dfs1(v, u, k);
}
int cnt = 0;
for (int i = head[u]; i; i = last[i]) {
int v = to[i];
if (v == f) {
continue;
}
a[cnt] = dp[v];
cnt++;
}
if (cnt < k) {
dp[u] = 1;
return;
}
sort(a, a + cnt);
dp[u] = a[cnt - k] + 1;
}
int sol1(int u, int f) {
int ans = dp[u];
for (int i = head[u]; i; i = last[i]) {
int v = to[i];
if (v == f) {
continue;
}
int ans2 = sol1(v, u);
if (ans2 > ans) {
ans = ans2;
}
}
answer += ans;
return ans;
}
void dfs2(int u, int f, int a) {
if (a == 1) {
dp[u] = n;
} else {
int l = 8, r = dp[u];
while (l < r) {
int mid = (l + r + 1) >> 1;
int cnt = 0;
for (int i = head[u]; i; i = last[i]) {
int v = to[i];
if (v == f) {
continue;
}
if (dp[v] >= mid) {
cnt++;
}
}
if (cnt >= mid) {
l = mid;
} else {
r = mid - 1;
}
}
dp[u] = l;
}
for (int i = head[u]; i; i = last[i]) {
int v = to[i];
if (v == f) {
continue;
}
dfs2(v, u, a);
}
}
int sol2(int u, int f) {
int ans = dp[u];
for (int i = head[u]; i; i = last[i]) {
int v = to[i];
if (v == f) {
continue;
}
int ans2 = sol2(v, u);
if (ans2 > ans) {
ans = ans2;
}
}
answer += ans - 8;
return ans;
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
add(u, v);
add(v, u);
}
if (n <= 8) {
for (int i = 1; i <= n; i++) {
dfs1(1, 0, i);
sol1(1, 0);
}
printf("%lld\n", answer);
return 0;
}
for (int i = 1; i <= 8; i++) {
dfs1(1, 0, i);
sol1(1, 0);
}
for (int i = 1; i <= 8; i++) {
dfs2(1, 0, i);
sol2(1, 0);
}
printf("%lld\n", answer);
return 0;
}
| ### Prompt
Please formulate a cpp solution to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int head[524288], last[1048576], to[1048576], cnt = 0;
void add(int u, int v) {
cnt++;
last[cnt] = head[u];
head[u] = cnt;
to[cnt] = v;
}
int dp[524288];
int a[524288];
long long answer = 0;
int n;
void dfs1(int u, int f, int k) {
for (int i = head[u]; i; i = last[i]) {
int v = to[i];
if (v == f) {
continue;
}
dfs1(v, u, k);
}
int cnt = 0;
for (int i = head[u]; i; i = last[i]) {
int v = to[i];
if (v == f) {
continue;
}
a[cnt] = dp[v];
cnt++;
}
if (cnt < k) {
dp[u] = 1;
return;
}
sort(a, a + cnt);
dp[u] = a[cnt - k] + 1;
}
int sol1(int u, int f) {
int ans = dp[u];
for (int i = head[u]; i; i = last[i]) {
int v = to[i];
if (v == f) {
continue;
}
int ans2 = sol1(v, u);
if (ans2 > ans) {
ans = ans2;
}
}
answer += ans;
return ans;
}
void dfs2(int u, int f, int a) {
if (a == 1) {
dp[u] = n;
} else {
int l = 8, r = dp[u];
while (l < r) {
int mid = (l + r + 1) >> 1;
int cnt = 0;
for (int i = head[u]; i; i = last[i]) {
int v = to[i];
if (v == f) {
continue;
}
if (dp[v] >= mid) {
cnt++;
}
}
if (cnt >= mid) {
l = mid;
} else {
r = mid - 1;
}
}
dp[u] = l;
}
for (int i = head[u]; i; i = last[i]) {
int v = to[i];
if (v == f) {
continue;
}
dfs2(v, u, a);
}
}
int sol2(int u, int f) {
int ans = dp[u];
for (int i = head[u]; i; i = last[i]) {
int v = to[i];
if (v == f) {
continue;
}
int ans2 = sol2(v, u);
if (ans2 > ans) {
ans = ans2;
}
}
answer += ans - 8;
return ans;
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
add(u, v);
add(v, u);
}
if (n <= 8) {
for (int i = 1; i <= n; i++) {
dfs1(1, 0, i);
sol1(1, 0);
}
printf("%lld\n", answer);
return 0;
}
for (int i = 1; i <= 8; i++) {
dfs1(1, 0, i);
sol1(1, 0);
}
for (int i = 1; i <= 8; i++) {
dfs2(1, 0, i);
sol2(1, 0);
}
printf("%lld\n", answer);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
const long long linf = 1ll * inf * inf;
const int N = 300000 + 7;
const int M = 19;
const int multipleTest = 0;
vector<int> adj[N];
int n;
long long ans = 0;
int maxLen[N];
int maxK[N][M];
int maxP[N][M];
bool check(int u, int len, int k, int r) {
int cnt = 0;
for (int v : adj[u])
if (v != r) {
if (maxK[v][len - 1] >= k) ++cnt;
}
return cnt >= k;
}
void dfs(int u, int r) {
maxLen[u] = 1;
for (int v : adj[u])
if (v != r) {
dfs(v, u);
maxLen[u] = max(maxLen[v] + 1, maxLen[u]);
}
ans += maxLen[u];
for (int len = M - 1; len > 1; --len) {
int lo = (len == M - 1) ? 2 : maxK[u][len + 1] + 1;
int hi = n;
int res = lo - 1;
while (lo <= hi) {
int mid = (lo + hi) >> 1;
if (check(u, len, mid, r)) {
res = mid;
lo = mid + 1;
} else
hi = mid - 1;
}
maxK[u][len] = res;
}
maxK[u][1] = n;
for (int len = M - 1; len > 0; --len) maxP[u][len] = maxK[u][len];
for (int len = M - 1; len > 0; --len) {
for (int v : adj[u])
if (v != r) {
maxP[u][len] = max(maxP[u][len], maxP[v][len]);
}
if (len == M - 1)
ans += (maxP[u][len] - 1) * len;
else
ans += (maxP[u][len] - maxP[u][len + 1]) * len;
}
}
void solve() {
cin >> n;
for (int i = (1), _b = (n); i < _b; ++i) {
int u, v;
scanf("%d%d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, -1);
cout << ans << '\n';
}
int main() {
int Test = 1;
if (multipleTest) {
cin >> Test;
}
for (int i = 0; i < Test; ++i) {
solve();
}
}
| ### Prompt
Please create a solution in CPP to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
const long long linf = 1ll * inf * inf;
const int N = 300000 + 7;
const int M = 19;
const int multipleTest = 0;
vector<int> adj[N];
int n;
long long ans = 0;
int maxLen[N];
int maxK[N][M];
int maxP[N][M];
bool check(int u, int len, int k, int r) {
int cnt = 0;
for (int v : adj[u])
if (v != r) {
if (maxK[v][len - 1] >= k) ++cnt;
}
return cnt >= k;
}
void dfs(int u, int r) {
maxLen[u] = 1;
for (int v : adj[u])
if (v != r) {
dfs(v, u);
maxLen[u] = max(maxLen[v] + 1, maxLen[u]);
}
ans += maxLen[u];
for (int len = M - 1; len > 1; --len) {
int lo = (len == M - 1) ? 2 : maxK[u][len + 1] + 1;
int hi = n;
int res = lo - 1;
while (lo <= hi) {
int mid = (lo + hi) >> 1;
if (check(u, len, mid, r)) {
res = mid;
lo = mid + 1;
} else
hi = mid - 1;
}
maxK[u][len] = res;
}
maxK[u][1] = n;
for (int len = M - 1; len > 0; --len) maxP[u][len] = maxK[u][len];
for (int len = M - 1; len > 0; --len) {
for (int v : adj[u])
if (v != r) {
maxP[u][len] = max(maxP[u][len], maxP[v][len]);
}
if (len == M - 1)
ans += (maxP[u][len] - 1) * len;
else
ans += (maxP[u][len] - maxP[u][len + 1]) * len;
}
}
void solve() {
cin >> n;
for (int i = (1), _b = (n); i < _b; ++i) {
int u, v;
scanf("%d%d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, -1);
cout << ans << '\n';
}
int main() {
int Test = 1;
if (multipleTest) {
cin >> Test;
}
for (int i = 0; i < Test; ++i) {
solve();
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
inline char gc() {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
? EOF
: *p1++;
}
inline int read() {
int x = 0;
char ch = getchar();
bool positive = 1;
for (; !isdigit(ch); ch = getchar())
if (ch == '-') positive = 0;
for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
return positive ? x : -x;
}
inline void write(int a) {
if (a >= 10) write(a / 10);
putchar('0' + a % 10);
}
inline void writeln(int a) {
if (a < 0) {
a = -a;
putchar('-');
}
write(a);
puts("");
}
const int N = 300005, B = 600;
int BBB, n, quan, q[N], ycl[N];
unsigned char dp[N][B];
long long ans;
vector<int> v[N];
int dfs(int p, int fa) {
for (vector<int>::iterator it = v[p].begin(); it != v[p].end(); it++)
if (*it == fa) {
v[p].erase(it);
break;
}
int son = v[p].size(), t = v[p].size();
for (unsigned i = 0; i < v[p].size(); i++) {
t = max(t, dfs(v[p][i], p));
ycl[p] = max(ycl[p], ycl[v[p][i]]);
}
ans += ++ycl[p];
for (int i = 1; i * i <= n; i++)
if (v[p].size() >= i) {
for (unsigned j = 0; j < v[p].size(); j++) {
q[j] = dp[v[p][j]][i];
}
nth_element(&q[0], q + (int)v[p].size() - i, &q[v[p].size()]);
dp[p][i] = q[v[p].size() - i] + 1;
} else {
dp[p][i] = 1;
}
ans += max(t - BBB, 0) * 2 + (n - max(t, BBB));
return t;
}
void solve(int p) {
for (unsigned i = 0; i < v[p].size(); i++) {
solve(v[p][i]);
for (int j = 1; j * j <= n; j++) dp[p][j] = max(dp[p][j], dp[v[p][i]][j]);
}
for (int i = 2; i * i <= n; i++) ans += dp[p][i];
}
int main() {
n = read();
BBB = sqrt(n);
for (int i = 1; i < n; i++) {
int s = read(), t = read();
v[s].push_back(t);
v[t].push_back(s);
}
dfs(1, 0);
solve(1);
cout << ans << endl;
}
| ### Prompt
Please create a solution in cpp to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline char gc() {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
? EOF
: *p1++;
}
inline int read() {
int x = 0;
char ch = getchar();
bool positive = 1;
for (; !isdigit(ch); ch = getchar())
if (ch == '-') positive = 0;
for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
return positive ? x : -x;
}
inline void write(int a) {
if (a >= 10) write(a / 10);
putchar('0' + a % 10);
}
inline void writeln(int a) {
if (a < 0) {
a = -a;
putchar('-');
}
write(a);
puts("");
}
const int N = 300005, B = 600;
int BBB, n, quan, q[N], ycl[N];
unsigned char dp[N][B];
long long ans;
vector<int> v[N];
int dfs(int p, int fa) {
for (vector<int>::iterator it = v[p].begin(); it != v[p].end(); it++)
if (*it == fa) {
v[p].erase(it);
break;
}
int son = v[p].size(), t = v[p].size();
for (unsigned i = 0; i < v[p].size(); i++) {
t = max(t, dfs(v[p][i], p));
ycl[p] = max(ycl[p], ycl[v[p][i]]);
}
ans += ++ycl[p];
for (int i = 1; i * i <= n; i++)
if (v[p].size() >= i) {
for (unsigned j = 0; j < v[p].size(); j++) {
q[j] = dp[v[p][j]][i];
}
nth_element(&q[0], q + (int)v[p].size() - i, &q[v[p].size()]);
dp[p][i] = q[v[p].size() - i] + 1;
} else {
dp[p][i] = 1;
}
ans += max(t - BBB, 0) * 2 + (n - max(t, BBB));
return t;
}
void solve(int p) {
for (unsigned i = 0; i < v[p].size(); i++) {
solve(v[p][i]);
for (int j = 1; j * j <= n; j++) dp[p][j] = max(dp[p][j], dp[v[p][i]][j]);
}
for (int i = 2; i * i <= n; i++) ans += dp[p][i];
}
int main() {
n = read();
BBB = sqrt(n);
for (int i = 1; i < n; i++) {
int s = read(), t = read();
v[s].push_back(t);
v[t].push_back(s);
}
dfs(1, 0);
solve(1);
cout << ans << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3E5 + 77;
const int log2maxn = 23;
struct EDGE {
int to, next;
} edges[maxn * 2];
int cEdge = 1, head[maxn];
void addEdge(int from, int to) {
edges[cEdge] = (EDGE){to, head[from]};
head[from] = cEdge++;
}
int N, V[maxn][log2maxn];
int X[maxn], Y[maxn], ce = 0, C[maxn], K[maxn];
void dfs1(int u, int f) {
for (int k = head[u]; k; k = edges[k].next) {
int v = edges[k].to;
if (v == f) continue;
X[ce] = u;
Y[ce] = v;
++ce;
++C[u];
dfs1(v, u);
}
}
void dfs2(int u) {
for (int d = 1; d < log2maxn; ++d) V[u][d] = max(V[u][d], 1);
for (int k = head[u]; k; k = edges[k].next) {
int v = edges[k].to;
dfs2(v);
for (int d = 1; d < log2maxn; ++d) V[u][d] = max(V[u][d], V[v][d]);
}
}
int dfs3(int u, long long &ans) {
int d = 1;
for (int k = head[u]; k; k = edges[k].next) {
int v = edges[k].to;
d = max(d, dfs3(v, ans) + 1);
}
ans += d;
return d;
}
int main() {
scanf("%d", &N);
for (int i = 1; i <= N; ++i) V[i][1] = N;
for (int i = 1; i < N; ++i) {
int u, v;
scanf("%d%d", &u, &v);
addEdge(u, v);
addEdge(v, u);
}
dfs1(1, 0);
cEdge = 1;
memset(head, 0, sizeof(head));
for (int i = 0; i < ce; ++i) addEdge(X[i], Y[i]);
for (int d = 2; d < log2maxn; ++d)
for (int u = 1; u <= N; ++u) {
for (int k = head[u]; k; k = edges[k].next) {
int v = edges[k].to, w = V[v][d - 1];
if (w >= C[u]) w = C[u];
K[w]++;
}
for (int i = C[u] - 1; i >= 0; --i) K[i] += K[i + 1];
for (int i = C[u]; i; --i)
if (K[i] >= i) {
V[u][d] = i;
break;
}
for (int i = 0; i <= C[u]; ++i) K[i] = 0;
}
dfs2(1);
long long ans = 0;
for (int u = 1; u <= N; ++u) {
for (int d = 1; d < log2maxn - 1; ++d) {
if (V[d][d] < V[d][d + 1]) cerr << "??" << endl;
ans += d * (V[u][d] - V[u][d + 1]);
}
}
dfs3(1, ans);
cout << ans << endl;
}
| ### Prompt
Please create a solution in Cpp to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3E5 + 77;
const int log2maxn = 23;
struct EDGE {
int to, next;
} edges[maxn * 2];
int cEdge = 1, head[maxn];
void addEdge(int from, int to) {
edges[cEdge] = (EDGE){to, head[from]};
head[from] = cEdge++;
}
int N, V[maxn][log2maxn];
int X[maxn], Y[maxn], ce = 0, C[maxn], K[maxn];
void dfs1(int u, int f) {
for (int k = head[u]; k; k = edges[k].next) {
int v = edges[k].to;
if (v == f) continue;
X[ce] = u;
Y[ce] = v;
++ce;
++C[u];
dfs1(v, u);
}
}
void dfs2(int u) {
for (int d = 1; d < log2maxn; ++d) V[u][d] = max(V[u][d], 1);
for (int k = head[u]; k; k = edges[k].next) {
int v = edges[k].to;
dfs2(v);
for (int d = 1; d < log2maxn; ++d) V[u][d] = max(V[u][d], V[v][d]);
}
}
int dfs3(int u, long long &ans) {
int d = 1;
for (int k = head[u]; k; k = edges[k].next) {
int v = edges[k].to;
d = max(d, dfs3(v, ans) + 1);
}
ans += d;
return d;
}
int main() {
scanf("%d", &N);
for (int i = 1; i <= N; ++i) V[i][1] = N;
for (int i = 1; i < N; ++i) {
int u, v;
scanf("%d%d", &u, &v);
addEdge(u, v);
addEdge(v, u);
}
dfs1(1, 0);
cEdge = 1;
memset(head, 0, sizeof(head));
for (int i = 0; i < ce; ++i) addEdge(X[i], Y[i]);
for (int d = 2; d < log2maxn; ++d)
for (int u = 1; u <= N; ++u) {
for (int k = head[u]; k; k = edges[k].next) {
int v = edges[k].to, w = V[v][d - 1];
if (w >= C[u]) w = C[u];
K[w]++;
}
for (int i = C[u] - 1; i >= 0; --i) K[i] += K[i + 1];
for (int i = C[u]; i; --i)
if (K[i] >= i) {
V[u][d] = i;
break;
}
for (int i = 0; i <= C[u]; ++i) K[i] = 0;
}
dfs2(1);
long long ans = 0;
for (int u = 1; u <= N; ++u) {
for (int d = 1; d < log2maxn - 1; ++d) {
if (V[d][d] < V[d][d + 1]) cerr << "??" << endl;
ans += d * (V[u][d] - V[u][d + 1]);
}
}
dfs3(1, ans);
cout << ans << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int m, k;
long long ans;
const int N = 330000;
int h[N], dp[N], mdp[N], cnt[N], par[N], chd[N];
vector<int> adj[N];
vector<int> V;
void dfs(int u, int p) {
V.push_back(u);
par[u] = p;
for (int v : adj[u]) {
if (v == p) continue;
dfs(v, u);
}
chd[u] = adj[u].size() - (u != 1);
for (int v : adj[u]) {
if (v == p) continue;
h[u] = max(h[u], h[v] + 1);
chd[u] = max(chd[u], chd[v]);
}
ans += h[u];
}
int from[N], cc[N];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
m = 0;
ans = 1LL * n * n;
m = 1;
dfs(1, 0);
reverse(V.begin(), V.end());
int sum = 0;
int _m = 1;
for (m = 2; 1LL * m * (m + 1) + 1 <= n; m++) {
long long s = 1, t = 1;
k = 0;
while (s <= n) t *= m, s += t, k++;
for (int i = 1; i <= n; i++) mdp[i] = 0;
sum += k;
if (k >= 4) {
for (int u : V) {
for (int v : adj[u]) {
if (v == par[u]) continue;
mdp[u] = max(mdp[u], mdp[v]);
}
int c = adj[u].size() - (u != 1);
dp[u] = 0;
if (mdp[u] < k - 1) {
if (c >= m) {
for (int j = 0; j < k; j++) cnt[j] = 0;
for (int v : adj[u]) {
if (v == par[u]) continue;
cnt[dp[v]]++;
}
for (int j = k - 2; j >= 0; j--) {
cnt[j] += cnt[j + 1];
if (cnt[j] >= m) {
dp[u] = j + 1;
break;
}
}
mdp[u] = max(mdp[u], dp[u]);
}
}
ans += mdp[u];
}
_m = m;
} else {
for (int u : V) {
cc[u] = adj[u].size() - (u != 1);
from[u] = 0;
int cn = 0;
for (int v : adj[u]) {
if (v == par[u]) continue;
cnt[cn++] = cc[v];
from[u] = max(from[u], from[v]);
}
sort(cnt, cnt + cn);
int t = 0;
for (int j = cn - 1; j >= 0; j--) {
if (cnt[j] < cn - j) break;
t = max(t, cn - j);
}
from[u] = max(from[u], t);
if (from[u] >= m) ans += from[u] - m + 1;
}
break;
}
}
for (int i = 0; i <= n; i++) cnt[i] = 0;
for (int i = 1; i <= n; i++) cnt[chd[i]]++;
for (int i = n - 1; i >= 0; i--) cnt[i] += cnt[i + 1];
for (m = _m + 1; m <= n; m++) ans += cnt[m];
cout << ans << endl;
return 0;
}
| ### Prompt
Please create a solution in CPP to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int m, k;
long long ans;
const int N = 330000;
int h[N], dp[N], mdp[N], cnt[N], par[N], chd[N];
vector<int> adj[N];
vector<int> V;
void dfs(int u, int p) {
V.push_back(u);
par[u] = p;
for (int v : adj[u]) {
if (v == p) continue;
dfs(v, u);
}
chd[u] = adj[u].size() - (u != 1);
for (int v : adj[u]) {
if (v == p) continue;
h[u] = max(h[u], h[v] + 1);
chd[u] = max(chd[u], chd[v]);
}
ans += h[u];
}
int from[N], cc[N];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
m = 0;
ans = 1LL * n * n;
m = 1;
dfs(1, 0);
reverse(V.begin(), V.end());
int sum = 0;
int _m = 1;
for (m = 2; 1LL * m * (m + 1) + 1 <= n; m++) {
long long s = 1, t = 1;
k = 0;
while (s <= n) t *= m, s += t, k++;
for (int i = 1; i <= n; i++) mdp[i] = 0;
sum += k;
if (k >= 4) {
for (int u : V) {
for (int v : adj[u]) {
if (v == par[u]) continue;
mdp[u] = max(mdp[u], mdp[v]);
}
int c = adj[u].size() - (u != 1);
dp[u] = 0;
if (mdp[u] < k - 1) {
if (c >= m) {
for (int j = 0; j < k; j++) cnt[j] = 0;
for (int v : adj[u]) {
if (v == par[u]) continue;
cnt[dp[v]]++;
}
for (int j = k - 2; j >= 0; j--) {
cnt[j] += cnt[j + 1];
if (cnt[j] >= m) {
dp[u] = j + 1;
break;
}
}
mdp[u] = max(mdp[u], dp[u]);
}
}
ans += mdp[u];
}
_m = m;
} else {
for (int u : V) {
cc[u] = adj[u].size() - (u != 1);
from[u] = 0;
int cn = 0;
for (int v : adj[u]) {
if (v == par[u]) continue;
cnt[cn++] = cc[v];
from[u] = max(from[u], from[v]);
}
sort(cnt, cnt + cn);
int t = 0;
for (int j = cn - 1; j >= 0; j--) {
if (cnt[j] < cn - j) break;
t = max(t, cn - j);
}
from[u] = max(from[u], t);
if (from[u] >= m) ans += from[u] - m + 1;
}
break;
}
}
for (int i = 0; i <= n; i++) cnt[i] = 0;
for (int i = 1; i <= n; i++) cnt[chd[i]]++;
for (int i = n - 1; i >= 0; i--) cnt[i] += cnt[i + 1];
for (m = _m + 1; m <= n; m++) ans += cnt[m];
cout << ans << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int mod1 = 1e9 + 7, mod2 = 998244353, maxn = 3e5 + 5, maxlog = 20, K = 26;
const long long infll = 1e18;
const double pi = acos(-1);
int n, h[maxn], f[maxn][20], g[maxn][20];
long long res = 0;
vector<int> gr[maxn];
void dfs(int u, int pa) {
for (auto v : gr[u]) {
if (v != pa) {
dfs(v, u);
h[u] = max(h[u], h[v] + 1);
}
}
f[u][1] = g[u][1] = n;
for (long long dep = 2; dep <= 19; ++dep) {
int lo = 0, hi = n;
while (lo < hi) {
int mid = (lo + hi + 1) >> 1;
int cnt = 0;
for (auto v : gr[u]) {
if (v != pa) {
if (g[v][dep - 1] >= mid) {
cnt++;
}
}
}
if (cnt >= mid)
lo = mid;
else
hi = mid - 1;
}
g[u][dep] = lo;
for (auto v : gr[u]) {
if (v != pa) f[u][dep] = max(f[u][dep], f[v][dep]);
}
f[u][dep] = max(f[u][dep], g[u][dep]);
}
}
void solve() {
cin >> n;
for (long long i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
gr[u].push_back(v);
gr[v].push_back(u);
}
dfs(1, 0);
for (long long u = 1; u <= n; ++u) {
res += h[u];
}
for (long long u = 1; u <= n; ++u) {
for (long long dep = 1; dep <= 19; ++dep) {
int lo = 2;
if (dep < 19) lo = max(lo, f[u][dep + 1] + 1);
int hi = f[u][dep];
if (lo <= hi) {
res += 1ll * (hi - lo + 1) * (dep - 1);
}
}
}
res += 1ll * n * n;
cout << res;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int mod1 = 1e9 + 7, mod2 = 998244353, maxn = 3e5 + 5, maxlog = 20, K = 26;
const long long infll = 1e18;
const double pi = acos(-1);
int n, h[maxn], f[maxn][20], g[maxn][20];
long long res = 0;
vector<int> gr[maxn];
void dfs(int u, int pa) {
for (auto v : gr[u]) {
if (v != pa) {
dfs(v, u);
h[u] = max(h[u], h[v] + 1);
}
}
f[u][1] = g[u][1] = n;
for (long long dep = 2; dep <= 19; ++dep) {
int lo = 0, hi = n;
while (lo < hi) {
int mid = (lo + hi + 1) >> 1;
int cnt = 0;
for (auto v : gr[u]) {
if (v != pa) {
if (g[v][dep - 1] >= mid) {
cnt++;
}
}
}
if (cnt >= mid)
lo = mid;
else
hi = mid - 1;
}
g[u][dep] = lo;
for (auto v : gr[u]) {
if (v != pa) f[u][dep] = max(f[u][dep], f[v][dep]);
}
f[u][dep] = max(f[u][dep], g[u][dep]);
}
}
void solve() {
cin >> n;
for (long long i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
gr[u].push_back(v);
gr[v].push_back(u);
}
dfs(1, 0);
for (long long u = 1; u <= n; ++u) {
res += h[u];
}
for (long long u = 1; u <= n; ++u) {
for (long long dep = 1; dep <= 19; ++dep) {
int lo = 2;
if (dep < 19) lo = max(lo, f[u][dep + 1] + 1);
int hi = f[u][dep];
if (lo <= hi) {
res += 1ll * (hi - lo + 1) * (dep - 1);
}
}
}
res += 1ll * n * n;
cout << res;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int oo = 0x3f3f3f3f;
const long long ooo = 9223372036854775807ll;
const int _cnt = 1000 * 1000 + 7;
const int _p = 1000 * 1000 * 1000 + 7;
const int N = 300005;
const double PI = acos(-1.0);
const double eps = 1e-9;
int o(int x) { return x % _p; }
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
void file_put() {
freopen("filename.in", "r", stdin);
freopen("filename.out", "w", stdout);
}
vector<int> V[N];
int n, dp[N][21], h[N][21], tmp[N], len[N], tot, x, y;
long long ans = 0;
inline bool cmp(int x, int y) { return x > y; }
void dfs(int x, int f) {
for (__typeof((V[x]).begin()) it = (V[x]).begin(); it != (V[x]).end(); it++)
if (*it != f)
dfs(*it, x), len[x] = ((len[x]) > (len[*it]) ? (len[x]) : (len[*it]));
ans += ++len[x];
dp[x][1] = h[x][1] = n;
for (int p = (2); p <= (20); ++p) {
tot = 0;
for (__typeof((V[x]).begin()) it = (V[x]).begin(); it != (V[x]).end(); it++)
if (*it != f) tmp[++tot] = dp[*it][p - 1];
sort(tmp + 1, tmp + 1 + tot, cmp);
for (int k = (tot); k >= (2); --k)
if (tmp[k] >= k) {
h[x][p] = dp[x][p] = k;
break;
}
for (__typeof((V[x]).begin()) it = (V[x]).begin(); it != (V[x]).end(); it++)
if (*it != f)
h[x][p] = ((h[x][p]) > (h[*it][p]) ? (h[x][p]) : (h[*it][p]));
}
for (int i = (1); i <= (20); ++i) {
ans += (long long)i * (h[x][i] - h[x][i + 1]);
if (!h[x][i + 1]) {
ans -= i;
break;
}
}
}
int main() {
scanf("%d", &n);
for (int i = (1); i <= (n - 1); ++i)
scanf("%d%d", &x, &y), V[x].push_back(y), V[y].push_back(x);
dfs(1, 0);
printf("%I64d\n", ans);
return 0;
}
| ### Prompt
Create a solution in Cpp for the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int oo = 0x3f3f3f3f;
const long long ooo = 9223372036854775807ll;
const int _cnt = 1000 * 1000 + 7;
const int _p = 1000 * 1000 * 1000 + 7;
const int N = 300005;
const double PI = acos(-1.0);
const double eps = 1e-9;
int o(int x) { return x % _p; }
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
void file_put() {
freopen("filename.in", "r", stdin);
freopen("filename.out", "w", stdout);
}
vector<int> V[N];
int n, dp[N][21], h[N][21], tmp[N], len[N], tot, x, y;
long long ans = 0;
inline bool cmp(int x, int y) { return x > y; }
void dfs(int x, int f) {
for (__typeof((V[x]).begin()) it = (V[x]).begin(); it != (V[x]).end(); it++)
if (*it != f)
dfs(*it, x), len[x] = ((len[x]) > (len[*it]) ? (len[x]) : (len[*it]));
ans += ++len[x];
dp[x][1] = h[x][1] = n;
for (int p = (2); p <= (20); ++p) {
tot = 0;
for (__typeof((V[x]).begin()) it = (V[x]).begin(); it != (V[x]).end(); it++)
if (*it != f) tmp[++tot] = dp[*it][p - 1];
sort(tmp + 1, tmp + 1 + tot, cmp);
for (int k = (tot); k >= (2); --k)
if (tmp[k] >= k) {
h[x][p] = dp[x][p] = k;
break;
}
for (__typeof((V[x]).begin()) it = (V[x]).begin(); it != (V[x]).end(); it++)
if (*it != f)
h[x][p] = ((h[x][p]) > (h[*it][p]) ? (h[x][p]) : (h[*it][p]));
}
for (int i = (1); i <= (20); ++i) {
ans += (long long)i * (h[x][i] - h[x][i + 1]);
if (!h[x][i + 1]) {
ans -= i;
break;
}
}
}
int main() {
scanf("%d", &n);
for (int i = (1); i <= (n - 1); ++i)
scanf("%d%d", &x, &y), V[x].push_back(y), V[y].push_back(x);
dfs(1, 0);
printf("%I64d\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long int Maxn3 = 1e3 + 10;
const long long int Maxn4 = 1e4 + 10;
const long long int Maxn5 = 1e5 + 10;
const long long int Maxn6 = 1e6 + 10;
const long long int Maxn7 = 1e7 + 10;
const long long int Maxn8 = 1e8 + 10;
const long long int Maxn9 = 1e9 + 10;
const long long int Maxn18 = 1e18 + 10;
const long long int Mod1 = 1e7 + 7;
const long long int Mod2 = 1e9 + 7;
const long long int LLMax = LLONG_MAX;
const long long int LLMin = LLONG_MIN;
const long long int INTMax = INT_MAX;
const long long int INTMin = INT_MIN;
long long int mn = LLMax, mx = LLMin;
vector<long long int> g[3 * Maxn5];
long long int f[3 * Maxn5][32], w[3 * Maxn5], a[3 * Maxn5], m, n;
void dfs(long long int x, long long int p) {
w[x] = 1;
for (long long int y : g[x])
if (y != p) dfs(y, x), ((w[x]) = max((w[x]), (1 + w[y])));
f[x][1] = n;
for (long long int i = 2; i < 21; i++) {
long long int m = 0;
for (long long int y : g[x])
if (y != p) a[m++] = f[y][i - 1];
sort(a, a + m);
reverse(a, a + m);
f[x][i] = 0;
while (f[x][i] < m and a[f[x][i]] > f[x][i]) f[x][i]++;
((f[x][i]) = max((f[x][i]), (1ll)));
}
f[x][21] = 1;
}
void dfs2(long long int x, long long int p) {
for (long long int y : g[x])
if (y != p) {
dfs2(y, x);
for (long long int i = 1; i < 21; i++)
((f[x][i]) = max((f[x][i]), (f[y][i])));
}
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
scanf("%lld", &n);
for (long long int i = 1; i < n; i++) {
long long int u, v;
scanf("%lld %lld", &u, &v);
u--;
v--;
g[u].push_back(v), g[v].push_back(u);
}
dfs(0ll, -1ll);
dfs2(0ll, -1ll);
long long int res = 0;
for (long long int i = 0; i < n; i++) {
res += w[i];
for (long long int j = 1; j < 21; j++) res += j * (f[i][j] - f[i][j + 1]);
}
return cout << res, 0;
}
| ### Prompt
In Cpp, your task is to solve the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long int Maxn3 = 1e3 + 10;
const long long int Maxn4 = 1e4 + 10;
const long long int Maxn5 = 1e5 + 10;
const long long int Maxn6 = 1e6 + 10;
const long long int Maxn7 = 1e7 + 10;
const long long int Maxn8 = 1e8 + 10;
const long long int Maxn9 = 1e9 + 10;
const long long int Maxn18 = 1e18 + 10;
const long long int Mod1 = 1e7 + 7;
const long long int Mod2 = 1e9 + 7;
const long long int LLMax = LLONG_MAX;
const long long int LLMin = LLONG_MIN;
const long long int INTMax = INT_MAX;
const long long int INTMin = INT_MIN;
long long int mn = LLMax, mx = LLMin;
vector<long long int> g[3 * Maxn5];
long long int f[3 * Maxn5][32], w[3 * Maxn5], a[3 * Maxn5], m, n;
void dfs(long long int x, long long int p) {
w[x] = 1;
for (long long int y : g[x])
if (y != p) dfs(y, x), ((w[x]) = max((w[x]), (1 + w[y])));
f[x][1] = n;
for (long long int i = 2; i < 21; i++) {
long long int m = 0;
for (long long int y : g[x])
if (y != p) a[m++] = f[y][i - 1];
sort(a, a + m);
reverse(a, a + m);
f[x][i] = 0;
while (f[x][i] < m and a[f[x][i]] > f[x][i]) f[x][i]++;
((f[x][i]) = max((f[x][i]), (1ll)));
}
f[x][21] = 1;
}
void dfs2(long long int x, long long int p) {
for (long long int y : g[x])
if (y != p) {
dfs2(y, x);
for (long long int i = 1; i < 21; i++)
((f[x][i]) = max((f[x][i]), (f[y][i])));
}
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
scanf("%lld", &n);
for (long long int i = 1; i < n; i++) {
long long int u, v;
scanf("%lld %lld", &u, &v);
u--;
v--;
g[u].push_back(v), g[v].push_back(u);
}
dfs(0ll, -1ll);
dfs2(0ll, -1ll);
long long int res = 0;
for (long long int i = 0; i < n; i++) {
res += w[i];
for (long long int j = 1; j < 21; j++) res += j * (f[i][j] - f[i][j + 1]);
}
return cout << res, 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void maxify(T& x, const T& y) {
y > x && (x = y);
}
typedef const int& ci;
struct node {
int v, nxt;
__inline__ __attribute__((always_inline)) node() {}
__inline__ __attribute__((always_inline)) node(ci _v, ci _nxt)
: v(_v), nxt(_nxt) {}
};
struct graph {
int head[300000 + 1], cnt;
node edge[300000 << 1];
__inline__ __attribute__((always_inline)) void build() {
memset(head, 0, sizeof(head)), cnt = 0;
}
__inline__ __attribute__((always_inline)) void insert(ci u, ci v) {
push(u, v);
push(v, u);
}
__inline__ __attribute__((always_inline)) void push(ci u, ci v) {
edge[++cnt] = node(v, head[u]);
head[u] = cnt;
}
};
graph g;
int dp1[300000 + 1][20 + 1], dp2[300000 + 1][20 + 1], dp3[300000 + 1];
int son[300000 + 1], tmp[300000 + 1];
int n;
long long ans = 0;
void dfs(ci u, ci p = 0) {
int x = 0;
for (int i = g.head[u], v; i; i = g.edge[i].nxt)
if ((v = g.edge[i].v) != p) {
dfs(v, u);
maxify(x, dp3[v]);
}
ans += dp3[u] = x + 1;
int* cnt = son;
for (int i = g.head[u], v; i; i = g.edge[i].nxt)
if ((v = g.edge[i].v) != p) *++cnt = v;
dp1[u][1] = dp2[u][1] = n;
if (cnt == son) {
ans += n - 1;
memset(dp1[0] + 1, 0, ((20 - 1) << 2));
memset(dp2[0] + 1, 0, ((20 - 1) << 2));
return;
}
for (int k = 1, *i, *tp, kk = 1; k < 20;
k = kk, maxify(dp2[u][kk], dp1[u][kk] = tp - i)) {
tp = tmp, ++kk;
for (int *i = son, &t = dp2[u][kk]; ++i <= cnt;
*++tp = dp1[*i][k], maxify(t, dp2[*i][kk]))
;
sort(tmp + 1, tp + 1), i = tp;
for (int* x = tp + 1; i > tmp && x - i <= *i; --i)
;
}
for (int k = 1; k < 20; ++k) {
int x = dp2[u][k], y = dp2[u][k + 1];
if (!y) y = 1;
ans += k * (long long)(x - y);
if (y == 1) break;
}
}
int main() {
scanf("%d", &n), g.build();
for (int i = n, u, v; --i; g.insert(u, v)) scanf("%d%d", &u, &v);
dfs(1);
printf("%lld\n", ans);
return 0;
}
| ### Prompt
Generate a cpp solution to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void maxify(T& x, const T& y) {
y > x && (x = y);
}
typedef const int& ci;
struct node {
int v, nxt;
__inline__ __attribute__((always_inline)) node() {}
__inline__ __attribute__((always_inline)) node(ci _v, ci _nxt)
: v(_v), nxt(_nxt) {}
};
struct graph {
int head[300000 + 1], cnt;
node edge[300000 << 1];
__inline__ __attribute__((always_inline)) void build() {
memset(head, 0, sizeof(head)), cnt = 0;
}
__inline__ __attribute__((always_inline)) void insert(ci u, ci v) {
push(u, v);
push(v, u);
}
__inline__ __attribute__((always_inline)) void push(ci u, ci v) {
edge[++cnt] = node(v, head[u]);
head[u] = cnt;
}
};
graph g;
int dp1[300000 + 1][20 + 1], dp2[300000 + 1][20 + 1], dp3[300000 + 1];
int son[300000 + 1], tmp[300000 + 1];
int n;
long long ans = 0;
void dfs(ci u, ci p = 0) {
int x = 0;
for (int i = g.head[u], v; i; i = g.edge[i].nxt)
if ((v = g.edge[i].v) != p) {
dfs(v, u);
maxify(x, dp3[v]);
}
ans += dp3[u] = x + 1;
int* cnt = son;
for (int i = g.head[u], v; i; i = g.edge[i].nxt)
if ((v = g.edge[i].v) != p) *++cnt = v;
dp1[u][1] = dp2[u][1] = n;
if (cnt == son) {
ans += n - 1;
memset(dp1[0] + 1, 0, ((20 - 1) << 2));
memset(dp2[0] + 1, 0, ((20 - 1) << 2));
return;
}
for (int k = 1, *i, *tp, kk = 1; k < 20;
k = kk, maxify(dp2[u][kk], dp1[u][kk] = tp - i)) {
tp = tmp, ++kk;
for (int *i = son, &t = dp2[u][kk]; ++i <= cnt;
*++tp = dp1[*i][k], maxify(t, dp2[*i][kk]))
;
sort(tmp + 1, tp + 1), i = tp;
for (int* x = tp + 1; i > tmp && x - i <= *i; --i)
;
}
for (int k = 1; k < 20; ++k) {
int x = dp2[u][k], y = dp2[u][k + 1];
if (!y) y = 1;
ans += k * (long long)(x - y);
if (y == 1) break;
}
}
int main() {
scanf("%d", &n), g.build();
for (int i = n, u, v; --i; g.insert(u, v)) scanf("%d%d", &u, &v);
dfs(1);
printf("%lld\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 99;
struct edge {
int to, nxt;
} e[maxn << 1];
int head[maxn], tot;
void add(int x, int y) {
e[++tot].to = y;
e[tot].nxt = head[x];
head[x] = tot;
}
int f[maxn][30], b[maxn], n, siz, top, stac[maxn], g[maxn], fa[maxn];
long long ans = 0;
vector<pair<int, int> > h[maxn];
void dp(int x) {
g[x] = 1;
f[x][1] = n;
for (int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if (y == fa[x]) continue;
fa[y] = x;
dp(y);
g[x] = max(g[x], g[y] + 1);
}
ans += g[x];
for (int j = 2; j < 30; j++) {
top = 0;
for (int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if (y == fa[x]) continue;
stac[++top] = f[y][j - 1];
}
sort(stac + 1, stac + 1 + top, greater<int>());
for (int i = top; i; i--)
if (stac[i] >= i) {
f[x][j] = i;
break;
}
h[f[x][j]].push_back(make_pair(x, j));
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
add(x, y);
add(y, x);
}
dp(1);
long long sum = n;
for (int i = 1; i <= n; i++) g[i] = 1;
for (int k = n; k > 1; k--) {
for (int i = 0; i < h[k].size(); i++) {
int x = h[k][i].first, now = h[k][i].second;
while (x) {
if (now < g[x]) break;
sum += now - g[x];
g[x] = now;
x = fa[x];
}
}
ans += sum;
}
cout << ans;
}
| ### Prompt
Please create a solution in Cpp to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 99;
struct edge {
int to, nxt;
} e[maxn << 1];
int head[maxn], tot;
void add(int x, int y) {
e[++tot].to = y;
e[tot].nxt = head[x];
head[x] = tot;
}
int f[maxn][30], b[maxn], n, siz, top, stac[maxn], g[maxn], fa[maxn];
long long ans = 0;
vector<pair<int, int> > h[maxn];
void dp(int x) {
g[x] = 1;
f[x][1] = n;
for (int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if (y == fa[x]) continue;
fa[y] = x;
dp(y);
g[x] = max(g[x], g[y] + 1);
}
ans += g[x];
for (int j = 2; j < 30; j++) {
top = 0;
for (int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if (y == fa[x]) continue;
stac[++top] = f[y][j - 1];
}
sort(stac + 1, stac + 1 + top, greater<int>());
for (int i = top; i; i--)
if (stac[i] >= i) {
f[x][j] = i;
break;
}
h[f[x][j]].push_back(make_pair(x, j));
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
add(x, y);
add(y, x);
}
dp(1);
long long sum = n;
for (int i = 1; i <= n; i++) g[i] = 1;
for (int k = n; k > 1; k--) {
for (int i = 0; i < h[k].size(); i++) {
int x = h[k][i].first, now = h[k][i].second;
while (x) {
if (now < g[x]) break;
sum += now - g[x];
g[x] = now;
x = fa[x];
}
}
ans += sum;
}
cout << ans;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long N = 3e5 + 5;
long long n;
vector<long long> G[N];
long long dp[N][20][2];
long long lev[N];
void dfs(long long u, long long p) {
vector<long long> ver;
for (long long i = 0; i < G[u].size(); ++i) {
long long v = G[u][i];
if (v == p) continue;
dfs(v, u);
lev[u] = max(lev[u], lev[v] + 1);
ver.push_back(v);
}
dp[u][1][0] = dp[u][1][1] = n;
for (long long i = 2; i <= 19; ++i) {
long long l = 0, r = n;
while (l < r) {
long long mid = l + r + 1 >> 1;
long long cnt = 0;
for (long long j = 0; j < ver.size(); ++j) {
long long v = ver[j];
if (dp[v][i - 1][0] >= mid) cnt++;
}
if (cnt >= mid)
l = mid;
else
r = mid - 1;
}
dp[u][i][0] = l;
for (long long j = 0; j < ver.size(); ++j) {
long long v = ver[j];
dp[u][i][1] = max(dp[u][i][1], dp[v][i][1]);
}
dp[u][i][1] = max(dp[u][i][1], dp[u][i][0]);
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin >> n;
for (long long i = 0; i < n - 1; ++i) {
long long u, v;
cin >> u >> v;
G[u].push_back(v), G[v].push_back(u);
}
dfs(1, 1);
long long res = 0;
for (long long i = 1; i <= n; ++i) res += lev[i];
for (long long u = 1; u <= n; ++u) {
for (long long i = 1; i <= 19; ++i) {
long long l = 2;
if (i <= 18) l = max(l, dp[u][i + 1][1] + 1);
long long r = dp[u][i][1];
if (l <= r) res += (r - l + 1) * (i - 1);
}
}
res += n * n;
cout << res << '\n';
}
| ### Prompt
Generate a Cpp solution to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long N = 3e5 + 5;
long long n;
vector<long long> G[N];
long long dp[N][20][2];
long long lev[N];
void dfs(long long u, long long p) {
vector<long long> ver;
for (long long i = 0; i < G[u].size(); ++i) {
long long v = G[u][i];
if (v == p) continue;
dfs(v, u);
lev[u] = max(lev[u], lev[v] + 1);
ver.push_back(v);
}
dp[u][1][0] = dp[u][1][1] = n;
for (long long i = 2; i <= 19; ++i) {
long long l = 0, r = n;
while (l < r) {
long long mid = l + r + 1 >> 1;
long long cnt = 0;
for (long long j = 0; j < ver.size(); ++j) {
long long v = ver[j];
if (dp[v][i - 1][0] >= mid) cnt++;
}
if (cnt >= mid)
l = mid;
else
r = mid - 1;
}
dp[u][i][0] = l;
for (long long j = 0; j < ver.size(); ++j) {
long long v = ver[j];
dp[u][i][1] = max(dp[u][i][1], dp[v][i][1]);
}
dp[u][i][1] = max(dp[u][i][1], dp[u][i][0]);
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin >> n;
for (long long i = 0; i < n - 1; ++i) {
long long u, v;
cin >> u >> v;
G[u].push_back(v), G[v].push_back(u);
}
dfs(1, 1);
long long res = 0;
for (long long i = 1; i <= n; ++i) res += lev[i];
for (long long u = 1; u <= n; ++u) {
for (long long i = 1; i <= 19; ++i) {
long long l = 2;
if (i <= 18) l = max(l, dp[u][i + 1][1] + 1);
long long r = dp[u][i][1];
if (l <= r) res += (r - l + 1) * (i - 1);
}
}
res += n * n;
cout << res << '\n';
}
``` |
#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
const long long inf = 1e9 + 7;
const long long max_n = 3e5 + 3;
long long n;
vector<long long> gr[max_n];
vector<long long> dp[max_n];
long long mx_k[max_n];
void dfs(long long v, long long pr) {
vector<long long> ch;
for (long long to : gr[v]) {
if (to == pr) continue;
dfs(to, v);
ch.emplace_back(to);
}
vector<vector<long long>> a((long long)(ch.size()));
for (long long to : ch) {
for (long long i = 0; i < min((long long)(ch.size()), mx_k[to]); i++) {
if ((long long)(dp[to].size()) > i)
a[i].emplace_back(dp[to][i]);
else
a[i].emplace_back(2);
}
}
for (long long k = 1; k <= (long long)(ch.size()); k++) {
sort(a[k - 1].begin(), a[k - 1].end());
reverse(a[k - 1].begin(), a[k - 1].end());
if ((long long)(a[k - 1].size()) > k - 1)
dp[v].emplace_back(a[k - 1][k - 1] + 1);
}
mx_k[v] = (long long)(ch.size());
}
void dfs1(long long v, long long pr) {
for (long long to : gr[v]) {
if (to == pr) continue;
dfs1(to, v);
for (long long k = 0; k < (long long)(dp[to].size()); k++) {
if ((long long)(dp[v].size()) == k)
dp[v].emplace_back(dp[to][k]);
else
dp[v][k] = max(dp[v][k], dp[to][k]);
}
mx_k[v] = max(mx_k[v], mx_k[to]);
}
}
void solve() {
cin >> n;
for (long long i = 0; i < n - 1; i++) {
long long v1, v2;
cin >> v1 >> v2;
gr[--v1].emplace_back(--v2);
gr[v2].emplace_back(v1);
}
dfs(0, -1);
dfs1(0, -1);
long long res = 0;
for (long long i = 0; i < n; i++) {
for (long long j : dp[i]) res += j;
res += (mx_k[i] - (long long)(dp[i].size())) * 2 + (n - mx_k[i]);
}
cout << res;
}
signed main() {
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
cout.precision(10);
solve();
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
const long long inf = 1e9 + 7;
const long long max_n = 3e5 + 3;
long long n;
vector<long long> gr[max_n];
vector<long long> dp[max_n];
long long mx_k[max_n];
void dfs(long long v, long long pr) {
vector<long long> ch;
for (long long to : gr[v]) {
if (to == pr) continue;
dfs(to, v);
ch.emplace_back(to);
}
vector<vector<long long>> a((long long)(ch.size()));
for (long long to : ch) {
for (long long i = 0; i < min((long long)(ch.size()), mx_k[to]); i++) {
if ((long long)(dp[to].size()) > i)
a[i].emplace_back(dp[to][i]);
else
a[i].emplace_back(2);
}
}
for (long long k = 1; k <= (long long)(ch.size()); k++) {
sort(a[k - 1].begin(), a[k - 1].end());
reverse(a[k - 1].begin(), a[k - 1].end());
if ((long long)(a[k - 1].size()) > k - 1)
dp[v].emplace_back(a[k - 1][k - 1] + 1);
}
mx_k[v] = (long long)(ch.size());
}
void dfs1(long long v, long long pr) {
for (long long to : gr[v]) {
if (to == pr) continue;
dfs1(to, v);
for (long long k = 0; k < (long long)(dp[to].size()); k++) {
if ((long long)(dp[v].size()) == k)
dp[v].emplace_back(dp[to][k]);
else
dp[v][k] = max(dp[v][k], dp[to][k]);
}
mx_k[v] = max(mx_k[v], mx_k[to]);
}
}
void solve() {
cin >> n;
for (long long i = 0; i < n - 1; i++) {
long long v1, v2;
cin >> v1 >> v2;
gr[--v1].emplace_back(--v2);
gr[v2].emplace_back(v1);
}
dfs(0, -1);
dfs1(0, -1);
long long res = 0;
for (long long i = 0; i < n; i++) {
for (long long j : dp[i]) res += j;
res += (mx_k[i] - (long long)(dp[i].size())) * 2 + (n - mx_k[i]);
}
cout << res;
}
signed main() {
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
cout.precision(10);
solve();
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n;
const int MaxN = 3e5;
vector<int> adj[MaxN];
vector<int> prec[MaxN];
void dfs(const int u, const int p) {
prec[u] = {1, n};
adj[u].erase(remove(adj[u].begin(), adj[u].end(), p), adj[u].end());
for (auto &&v : adj[u]) {
dfs(v, u);
prec[u][0] = max(prec[u][0], 1 + prec[v][0]);
}
for (int i = 2;; ++i) {
int lo = 1;
int hi = adj[u].size();
while (lo < hi) {
int mid = (lo + hi + 1) >> 1;
int count = 0;
for (auto &&v : adj[u])
count += i <= prec[v].size() && prec[v][i - 1] >= mid;
if (count >= mid)
lo = mid;
else
hi = mid - 1;
}
if (lo <= 1) break;
prec[u].push_back(lo);
}
}
void dfs2(const int u, long long &ans) {
for (auto &&v : adj[u]) {
dfs2(v, ans);
if (prec[u].size() < prec[v].size()) prec[u].resize(prec[v].size(), 0);
for (int i = 2; i < prec[v].size(); ++i)
prec[u][i] = max(prec[u][i], prec[v][i]);
}
ans -= prec[u].size() - 1;
for (auto &&x : prec[u]) ans += x;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
--u, --v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(0, -1);
long long ans = 0;
dfs2(0, ans);
cout << ans;
return 0;
}
| ### Prompt
Generate a CPP solution to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n;
const int MaxN = 3e5;
vector<int> adj[MaxN];
vector<int> prec[MaxN];
void dfs(const int u, const int p) {
prec[u] = {1, n};
adj[u].erase(remove(adj[u].begin(), adj[u].end(), p), adj[u].end());
for (auto &&v : adj[u]) {
dfs(v, u);
prec[u][0] = max(prec[u][0], 1 + prec[v][0]);
}
for (int i = 2;; ++i) {
int lo = 1;
int hi = adj[u].size();
while (lo < hi) {
int mid = (lo + hi + 1) >> 1;
int count = 0;
for (auto &&v : adj[u])
count += i <= prec[v].size() && prec[v][i - 1] >= mid;
if (count >= mid)
lo = mid;
else
hi = mid - 1;
}
if (lo <= 1) break;
prec[u].push_back(lo);
}
}
void dfs2(const int u, long long &ans) {
for (auto &&v : adj[u]) {
dfs2(v, ans);
if (prec[u].size() < prec[v].size()) prec[u].resize(prec[v].size(), 0);
for (int i = 2; i < prec[v].size(); ++i)
prec[u][i] = max(prec[u][i], prec[v][i]);
}
ans -= prec[u].size() - 1;
for (auto &&x : prec[u]) ans += x;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
--u, --v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(0, -1);
long long ans = 0;
dfs2(0, ans);
cout << ans;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5, bzmax = 20;
int n;
int h[N];
int f[N][bzmax];
int buf[N], par[N];
vector<pair<int, int> > upd[N];
vector<int> g[N];
long long res;
int dres;
inline void init() {
scanf("%d", &n);
for (int i = 1, u, v; i < n; i++) {
scanf("%d%d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
}
int dp(int p, int fa) {
int rt = 0;
par[p] = fa;
for (int i = 0; i < (signed)g[p].size(); i++) {
int e = g[p][i];
if (e ^ fa) rt = max(rt, dp(e, p));
}
f[p][1] = n;
for (int t = 2; t < bzmax; t++) {
int tp = 0;
for (int i = 0; i < (signed)g[p].size(); i++) {
int e = g[p][i];
if ((e ^ fa) && f[e][t - 1]) buf[++tp] = f[e][t - 1];
}
sort(buf + 1, buf + tp + 1, greater<int>());
for (int i = tp; i && !f[p][t]; i--)
if (buf[i] >= i) f[p][t] = i;
if (f[p][t] > 1) upd[f[p][t]].push_back(pair<int, int>(p, t));
}
res += rt + 1;
return rt + 1;
}
void update(int p, int v) {
while (p) {
if (v <= h[p]) break;
dres += v - h[p];
h[p] = v, p = par[p];
}
}
inline void solve() {
dp(1, 0);
dres = n;
for (int i = 1; i <= n; i++) h[i] = 1;
for (int k = n; k > 1; k--) {
for (int i = 0; i < (signed)upd[k].size(); i++)
update(upd[k][i].first, upd[k][i].second);
res += dres;
}
printf("%lld", res);
}
int main() {
init();
solve();
return 0;
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5, bzmax = 20;
int n;
int h[N];
int f[N][bzmax];
int buf[N], par[N];
vector<pair<int, int> > upd[N];
vector<int> g[N];
long long res;
int dres;
inline void init() {
scanf("%d", &n);
for (int i = 1, u, v; i < n; i++) {
scanf("%d%d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
}
int dp(int p, int fa) {
int rt = 0;
par[p] = fa;
for (int i = 0; i < (signed)g[p].size(); i++) {
int e = g[p][i];
if (e ^ fa) rt = max(rt, dp(e, p));
}
f[p][1] = n;
for (int t = 2; t < bzmax; t++) {
int tp = 0;
for (int i = 0; i < (signed)g[p].size(); i++) {
int e = g[p][i];
if ((e ^ fa) && f[e][t - 1]) buf[++tp] = f[e][t - 1];
}
sort(buf + 1, buf + tp + 1, greater<int>());
for (int i = tp; i && !f[p][t]; i--)
if (buf[i] >= i) f[p][t] = i;
if (f[p][t] > 1) upd[f[p][t]].push_back(pair<int, int>(p, t));
}
res += rt + 1;
return rt + 1;
}
void update(int p, int v) {
while (p) {
if (v <= h[p]) break;
dres += v - h[p];
h[p] = v, p = par[p];
}
}
inline void solve() {
dp(1, 0);
dres = n;
for (int i = 1; i <= n; i++) h[i] = 1;
for (int k = n; k > 1; k--) {
for (int i = 0; i < (signed)upd[k].size(); i++)
update(upd[k][i].first, upd[k][i].second);
res += dres;
}
printf("%lld", res);
}
int main() {
init();
solve();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<int> g[300005];
int dp[300005][25], dp2[300005][25];
int D[300005];
int n;
void dfs(int u, int p) {
D[u] = 1;
for (int i = 0; i < (int)(g[u]).size(); i++) {
int v = g[u][i];
if (v != p) {
dfs(v, u);
D[u] = max(D[u], D[v] + 1);
for (int d = 1; d < 20; d++) dp2[u][d] = max(dp2[u][d], dp2[v][d]);
}
}
for (int d = 2; d < 20; d++) {
vector<int> sum;
for (int i = 0; i < (int)(g[u]).size(); i++) {
int v = g[u][i];
if (v != p) sum.emplace_back(dp[v][d - 1]);
}
sort((sum).begin(), (sum).end());
reverse((sum).begin(), (sum).end());
for (int i = 0; i < (int)(sum).size(); i++) {
dp[u][d] = max(dp[u][d], min(i + 1, sum[i]));
dp2[u][d] = max(dp2[u][d], dp[u][d]);
}
}
dp[u][1] = n;
dp2[u][1] = n;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n - 1; i++) {
int a, b;
scanf("%d%d", &a, &b);
g[a].emplace_back(b);
g[b].emplace_back(a);
}
dfs(1, 0);
long long ans = 0;
for (int u = 1; u <= n; u++) {
if (D[u] > 19) ans += D[u];
for (int d = 19; d >= 1; d--) {
int diff = dp2[u][d] - dp2[u][d + 1];
if (D[u] > 19 && d == 19) diff--;
ans += diff * 1ll * d;
}
}
printf("%lld\n", ans);
}
| ### Prompt
Please create a solution in CPP to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> g[300005];
int dp[300005][25], dp2[300005][25];
int D[300005];
int n;
void dfs(int u, int p) {
D[u] = 1;
for (int i = 0; i < (int)(g[u]).size(); i++) {
int v = g[u][i];
if (v != p) {
dfs(v, u);
D[u] = max(D[u], D[v] + 1);
for (int d = 1; d < 20; d++) dp2[u][d] = max(dp2[u][d], dp2[v][d]);
}
}
for (int d = 2; d < 20; d++) {
vector<int> sum;
for (int i = 0; i < (int)(g[u]).size(); i++) {
int v = g[u][i];
if (v != p) sum.emplace_back(dp[v][d - 1]);
}
sort((sum).begin(), (sum).end());
reverse((sum).begin(), (sum).end());
for (int i = 0; i < (int)(sum).size(); i++) {
dp[u][d] = max(dp[u][d], min(i + 1, sum[i]));
dp2[u][d] = max(dp2[u][d], dp[u][d]);
}
}
dp[u][1] = n;
dp2[u][1] = n;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n - 1; i++) {
int a, b;
scanf("%d%d", &a, &b);
g[a].emplace_back(b);
g[b].emplace_back(a);
}
dfs(1, 0);
long long ans = 0;
for (int u = 1; u <= n; u++) {
if (D[u] > 19) ans += D[u];
for (int d = 19; d >= 1; d--) {
int diff = dp2[u][d] - dp2[u][d + 1];
if (D[u] > 19 && d == 19) diff--;
ans += diff * 1ll * d;
}
}
printf("%lld\n", ans);
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long ans = 0;
int n, f1[300010], dp[300010][20], fa[300010], buf[300010], dep[300010];
vector<int> v[300010];
vector<pair<int, int> > upd[300010];
void dfs(int np, int fath) {
f1[np] = 1;
fa[np] = fath;
for (int &x : v[np]) {
if (x == fath) continue;
dfs(x, np);
f1[np] = max(f1[np], f1[x] + 1);
}
dp[np][1] = n;
for (int i = 2; i < 20; i++) {
int tp = 0;
for (int &x : v[np]) {
if (x == fath) continue;
buf[++tp] = dp[x][i - 1];
}
sort(buf + 1, buf + tp + 1, greater<int>());
for (int j = tp; j >= 1; j--) {
if (buf[j] >= j) {
dp[np][i] = j;
break;
}
}
if (dp[np][i] > 1) upd[dp[np][i]].push_back(make_pair(np, i));
}
ans += f1[np];
}
long long cur = 0;
void update(int x, int y) {
while (x) {
if (dep[x] >= y) return;
cur += y - dep[x];
dep[x] = y;
x = fa[x];
}
}
int main() {
scanf("%d", &n);
fill(dep + 1, dep + n + 1, 1);
cur = n;
for (int i = 1, ti, tj; i < n; i++) {
scanf("%d%d", &ti, &tj);
v[ti].push_back(tj);
v[tj].push_back(ti);
}
dfs(1, 0);
for (int i = n; i >= 2; i--) {
for (auto &x : upd[i]) update(x.first, x.second);
ans += cur;
}
printf("%lld\n", ans);
return 0;
}
| ### Prompt
Generate a CPP solution to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long ans = 0;
int n, f1[300010], dp[300010][20], fa[300010], buf[300010], dep[300010];
vector<int> v[300010];
vector<pair<int, int> > upd[300010];
void dfs(int np, int fath) {
f1[np] = 1;
fa[np] = fath;
for (int &x : v[np]) {
if (x == fath) continue;
dfs(x, np);
f1[np] = max(f1[np], f1[x] + 1);
}
dp[np][1] = n;
for (int i = 2; i < 20; i++) {
int tp = 0;
for (int &x : v[np]) {
if (x == fath) continue;
buf[++tp] = dp[x][i - 1];
}
sort(buf + 1, buf + tp + 1, greater<int>());
for (int j = tp; j >= 1; j--) {
if (buf[j] >= j) {
dp[np][i] = j;
break;
}
}
if (dp[np][i] > 1) upd[dp[np][i]].push_back(make_pair(np, i));
}
ans += f1[np];
}
long long cur = 0;
void update(int x, int y) {
while (x) {
if (dep[x] >= y) return;
cur += y - dep[x];
dep[x] = y;
x = fa[x];
}
}
int main() {
scanf("%d", &n);
fill(dep + 1, dep + n + 1, 1);
cur = n;
for (int i = 1, ti, tj; i < n; i++) {
scanf("%d%d", &ti, &tj);
v[ti].push_back(tj);
v[tj].push_back(ti);
}
dfs(1, 0);
for (int i = n; i >= 2; i--) {
for (auto &x : upd[i]) update(x.first, x.second);
ans += cur;
}
printf("%lld\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int C = 600001, D = 70;
vector<vector<int> > tr(C);
int ij[C], a, b, n, dp[C], ih[C], bst[C], maxson[C];
long long wynn = 0;
int pom[C];
void Sebasort(int tab[], int l, int r) {
int lim = 2, limi = l + 1, limj = l + 2, j = limi, i = l, k = l;
while (lim / 2 <= r - l + 1) {
while (i <= r) {
if (limj > r) limj = r + 1;
if ((j < limj && tab[j] > tab[i]) || i >= limi)
pom[k] = tab[j], j++;
else
pom[k] = tab[i], i++;
k++;
if (i == limi && j == limj) limi += lim, limj += lim, i = j, j = limi;
}
for (i = l; i <= r; i++) tab[i] = pom[i];
lim *= 2, limi = l + lim / 2, limj = l + lim, j = limi, i = k = l;
}
}
int s[C], is = 0, ip = 1, pre[C], apre[C], par[C], ji[C];
void proc_tree(int a, int n, vector<vector<int> >& tab, int ij[]) {
s[0] = a, is = 1, pre[a] = 1, apre[1] = a, ip = 2;
int b;
maxson[a] = ij[a];
for (int z = 1; z <= n; z++) ji[z] = ij[z], par[z] = 0;
while (is > 0) {
a = s[is - 1];
if (ji[a] > 0 && tab[a][ji[a] - 1] == par[a]) ji[a]--;
if (ji[a] > 0)
b = s[is] = tab[a][ji[a] - 1], maxson[b] = ij[b] - 1, pre[b] = ip,
apre[ip] = b, par[b] = a, ip++, is++, ji[a]--;
else {
if (maxson[a] > maxson[par[a]]) maxson[par[a]] = maxson[a];
is--;
}
}
}
int main() {
int i, j, ji, y, x, sn;
scanf("%d", &n);
for (int z = 1; z < n; z++)
scanf("%d %d", &a, &b), tr[a].push_back(b), tr[b].push_back(a), ij[a]++,
ij[b]++;
proc_tree(1, n, tr, ij);
int** hp = new int*[C];
for (i = 0; i <= n; i++) hp[i] = new int[ij[i] + 1], ih[i] = 0;
for (i = 1; i <= min(D, n); i++) {
for (j = n; j > 0; j--) {
y = apre[j], x = par[y], sn = ij[y];
if (y == 1) sn++;
if (sn - 1 < i)
dp[y] = 1;
else if (ih[y] < i)
dp[y] = 2;
else {
Sebasort(hp[y], 0, ih[y] - 1);
dp[y] = hp[y][i - 1] + 1;
}
bst[y] = max(bst[y], dp[y]);
if (dp[y] > 1) hp[x][ih[x]] = dp[y], ih[x]++;
wynn += bst[y];
if (bst[y] > bst[x]) bst[x] = bst[y];
ih[y] = 0, dp[y] = bst[y] = 0;
}
ih[0] = 0;
}
if (n > D) {
for (i = n; i > 0; i--) {
y = apre[i], x = par[y], sn = ij[y];
if (y == 1) sn++;
if (ih[y] > 1) Sebasort(hp[y], 0, ih[y] - 1);
for (j = D; j < ih[y]; j++)
if (hp[y][j] <= j) break;
bst[y] = max(j, bst[y]);
wynn = wynn + (bst[y] - D);
bst[x] = max(bst[y], bst[x]);
hp[x][ih[x]] = sn - 1, ih[x]++;
if (maxson[i] < D)
wynn = wynn + (n - D);
else
wynn = wynn + (maxson[i] - D) * 2 + (n - maxson[i]);
}
}
printf("%I64d\n", wynn);
return 0;
}
| ### Prompt
In Cpp, your task is to solve the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int C = 600001, D = 70;
vector<vector<int> > tr(C);
int ij[C], a, b, n, dp[C], ih[C], bst[C], maxson[C];
long long wynn = 0;
int pom[C];
void Sebasort(int tab[], int l, int r) {
int lim = 2, limi = l + 1, limj = l + 2, j = limi, i = l, k = l;
while (lim / 2 <= r - l + 1) {
while (i <= r) {
if (limj > r) limj = r + 1;
if ((j < limj && tab[j] > tab[i]) || i >= limi)
pom[k] = tab[j], j++;
else
pom[k] = tab[i], i++;
k++;
if (i == limi && j == limj) limi += lim, limj += lim, i = j, j = limi;
}
for (i = l; i <= r; i++) tab[i] = pom[i];
lim *= 2, limi = l + lim / 2, limj = l + lim, j = limi, i = k = l;
}
}
int s[C], is = 0, ip = 1, pre[C], apre[C], par[C], ji[C];
void proc_tree(int a, int n, vector<vector<int> >& tab, int ij[]) {
s[0] = a, is = 1, pre[a] = 1, apre[1] = a, ip = 2;
int b;
maxson[a] = ij[a];
for (int z = 1; z <= n; z++) ji[z] = ij[z], par[z] = 0;
while (is > 0) {
a = s[is - 1];
if (ji[a] > 0 && tab[a][ji[a] - 1] == par[a]) ji[a]--;
if (ji[a] > 0)
b = s[is] = tab[a][ji[a] - 1], maxson[b] = ij[b] - 1, pre[b] = ip,
apre[ip] = b, par[b] = a, ip++, is++, ji[a]--;
else {
if (maxson[a] > maxson[par[a]]) maxson[par[a]] = maxson[a];
is--;
}
}
}
int main() {
int i, j, ji, y, x, sn;
scanf("%d", &n);
for (int z = 1; z < n; z++)
scanf("%d %d", &a, &b), tr[a].push_back(b), tr[b].push_back(a), ij[a]++,
ij[b]++;
proc_tree(1, n, tr, ij);
int** hp = new int*[C];
for (i = 0; i <= n; i++) hp[i] = new int[ij[i] + 1], ih[i] = 0;
for (i = 1; i <= min(D, n); i++) {
for (j = n; j > 0; j--) {
y = apre[j], x = par[y], sn = ij[y];
if (y == 1) sn++;
if (sn - 1 < i)
dp[y] = 1;
else if (ih[y] < i)
dp[y] = 2;
else {
Sebasort(hp[y], 0, ih[y] - 1);
dp[y] = hp[y][i - 1] + 1;
}
bst[y] = max(bst[y], dp[y]);
if (dp[y] > 1) hp[x][ih[x]] = dp[y], ih[x]++;
wynn += bst[y];
if (bst[y] > bst[x]) bst[x] = bst[y];
ih[y] = 0, dp[y] = bst[y] = 0;
}
ih[0] = 0;
}
if (n > D) {
for (i = n; i > 0; i--) {
y = apre[i], x = par[y], sn = ij[y];
if (y == 1) sn++;
if (ih[y] > 1) Sebasort(hp[y], 0, ih[y] - 1);
for (j = D; j < ih[y]; j++)
if (hp[y][j] <= j) break;
bst[y] = max(j, bst[y]);
wynn = wynn + (bst[y] - D);
bst[x] = max(bst[y], bst[x]);
hp[x][ih[x]] = sn - 1, ih[x]++;
if (maxson[i] < D)
wynn = wynn + (n - D);
else
wynn = wynn + (maxson[i] - D) * 2 + (n - maxson[i]);
}
}
printf("%I64d\n", wynn);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> v[300007];
int lvl[300007];
int cnt[300007];
int f[300007][21];
int dp[300007][21];
long long ans = 0;
void dfs(int vertex, int prv) {
int sz = v[vertex].size();
lvl[vertex] = 1;
for (int i = 0; i < sz; ++i) {
int h = v[vertex][i];
if (h == prv) {
continue;
}
dfs(h, vertex);
lvl[vertex] = max(lvl[vertex], lvl[h] + 1);
for (int j = 1; j < 21; ++j) {
dp[vertex][j] = max(dp[vertex][j], dp[h][j]);
}
}
ans += lvl[vertex];
f[vertex][1] = n;
for (int j = 2; j < 21; ++j) {
for (int i = 0; i < sz; ++i) {
int h = v[vertex][i];
if (h == prv) {
continue;
}
if (f[h][j - 1] <= sz) {
++cnt[f[h][j - 1]];
} else {
++cnt[sz];
}
}
int suff = 0;
for (int i = sz; i >= 0; --i) {
suff += cnt[i];
if (suff >= i) {
f[vertex][j] = i;
break;
}
}
for (int i = 0; i <= sz; ++i) {
cnt[i] = 0;
}
}
int lst = 1;
for (int j = 21 - 1; j >= 0; --j) {
dp[vertex][j] = max(dp[vertex][j], f[vertex][j]);
if (dp[vertex][j] > lst) {
ans += 1LL * (dp[vertex][j] - lst) * j;
lst = dp[vertex][j];
}
}
}
void input() {
scanf("%d", &n);
for (int i = 1; i < n; ++i) {
int x, y;
scanf("%d%d", &x, &y);
v[x].push_back(y);
v[y].push_back(x);
}
}
void solve() {
dfs(1, -1);
printf("%I64d\n", ans);
}
int main() {
ios_base ::sync_with_stdio(false);
cin.tie(NULL);
input();
solve();
return 0;
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> v[300007];
int lvl[300007];
int cnt[300007];
int f[300007][21];
int dp[300007][21];
long long ans = 0;
void dfs(int vertex, int prv) {
int sz = v[vertex].size();
lvl[vertex] = 1;
for (int i = 0; i < sz; ++i) {
int h = v[vertex][i];
if (h == prv) {
continue;
}
dfs(h, vertex);
lvl[vertex] = max(lvl[vertex], lvl[h] + 1);
for (int j = 1; j < 21; ++j) {
dp[vertex][j] = max(dp[vertex][j], dp[h][j]);
}
}
ans += lvl[vertex];
f[vertex][1] = n;
for (int j = 2; j < 21; ++j) {
for (int i = 0; i < sz; ++i) {
int h = v[vertex][i];
if (h == prv) {
continue;
}
if (f[h][j - 1] <= sz) {
++cnt[f[h][j - 1]];
} else {
++cnt[sz];
}
}
int suff = 0;
for (int i = sz; i >= 0; --i) {
suff += cnt[i];
if (suff >= i) {
f[vertex][j] = i;
break;
}
}
for (int i = 0; i <= sz; ++i) {
cnt[i] = 0;
}
}
int lst = 1;
for (int j = 21 - 1; j >= 0; --j) {
dp[vertex][j] = max(dp[vertex][j], f[vertex][j]);
if (dp[vertex][j] > lst) {
ans += 1LL * (dp[vertex][j] - lst) * j;
lst = dp[vertex][j];
}
}
}
void input() {
scanf("%d", &n);
for (int i = 1; i < n; ++i) {
int x, y;
scanf("%d%d", &x, &y);
v[x].push_back(y);
v[y].push_back(x);
}
}
void solve() {
dfs(1, -1);
printf("%I64d\n", ans);
}
int main() {
ios_base ::sync_with_stdio(false);
cin.tie(NULL);
input();
solve();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 300005;
long long ans;
int ne[N << 1], fi[N], zz[N << 1], tot, x, y, dp[N][25], T, n, Max[N],
dp2[N][25];
void jb(int x, int y) {
ne[++tot] = fi[x];
fi[x] = tot;
zz[tot] = y;
}
int cmp(int x, int y) { return dp[x][T] > dp[y][T]; }
void dfs(int x, int y) {
Max[x] = 1;
vector<int> v;
for (int i = fi[x]; i; i = ne[i])
if (zz[i] != y) {
dfs(zz[i], x);
Max[x] = max(Max[x], Max[zz[i]] + 1);
v.push_back(zz[i]);
}
dp[x][0] = 1e9;
ans += n - 1;
for (int i = 1; i <= 20; i++) {
T = i - 1;
sort(v.begin(), v.end(), cmp);
for (int j = 0; j < (int)v.size(); j++)
if (dp[v[j]][i - 1] >= j + 1) dp[x][i] = j + 1;
dp2[x][i] = dp[x][i];
for (int j : v) dp2[x][i] = max(dp2[x][i], dp2[j][i]);
if (dp2[x][i] > 1) ans += dp2[x][i] - 1;
}
ans += Max[x];
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
scanf("%d%d", &x, &y);
jb(x, y);
jb(y, x);
}
dfs(1, 0);
printf("%lld\n", ans);
return 0;
}
| ### Prompt
Your task is to create a cpp solution to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 300005;
long long ans;
int ne[N << 1], fi[N], zz[N << 1], tot, x, y, dp[N][25], T, n, Max[N],
dp2[N][25];
void jb(int x, int y) {
ne[++tot] = fi[x];
fi[x] = tot;
zz[tot] = y;
}
int cmp(int x, int y) { return dp[x][T] > dp[y][T]; }
void dfs(int x, int y) {
Max[x] = 1;
vector<int> v;
for (int i = fi[x]; i; i = ne[i])
if (zz[i] != y) {
dfs(zz[i], x);
Max[x] = max(Max[x], Max[zz[i]] + 1);
v.push_back(zz[i]);
}
dp[x][0] = 1e9;
ans += n - 1;
for (int i = 1; i <= 20; i++) {
T = i - 1;
sort(v.begin(), v.end(), cmp);
for (int j = 0; j < (int)v.size(); j++)
if (dp[v[j]][i - 1] >= j + 1) dp[x][i] = j + 1;
dp2[x][i] = dp[x][i];
for (int j : v) dp2[x][i] = max(dp2[x][i], dp2[j][i]);
if (dp2[x][i] > 1) ans += dp2[x][i] - 1;
}
ans += Max[x];
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
scanf("%d%d", &x, &y);
jb(x, y);
jb(y, x);
}
dfs(1, 0);
printf("%lld\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int sz = 3e5 + 10, x = 5;
vector<int> sv[sz];
int n, k = 1, dp[sz], dp2[sz][x + 1], dp3[sz][x + 1];
long long an = 0;
int dfs(int v, int pr) {
int re = 0;
dp[v] = 1;
vector<int> sp;
for (int a = 0; a < sv[v].size(); a++) {
int ne = sv[v][a];
if (ne != pr) {
re = max(re, dfs(ne, v));
sp.push_back(dp[ne]);
}
}
if (sp.size() >= k) {
sort(sp.begin(), sp.end(), greater<int>());
dp[v] = sp[k - 1] + 1;
}
re = max(re, dp[v]);
an += re;
return re;
}
void dfs2(int v, int pr) {
dp2[v][1] = n;
vector<int> sp[x + 1];
for (int a = 0; a < sv[v].size(); a++) {
int ne = sv[v][a];
if (ne != pr) {
dfs2(ne, v);
for (int b = 1; b <= x; b++) dp3[v][b] = max(dp3[v][b], dp3[ne][b]);
for (int b = 2; b <= x; b++) sp[b].push_back(dp2[ne][b - 1]);
}
}
for (int a = 2; a <= x; a++) {
sort(sp[a].begin(), sp[a].end(), greater<int>());
for (int b = 0; b < sp[a].size(); b++)
if (sp[a][b] >= b + 1) dp2[v][a] = max(dp2[v][a], b + 1);
}
for (int a = 1; a <= x; a++) dp3[v][a] = max(dp3[v][a], dp2[v][a]);
for (int a = 1; a <= x; a++) {
int va = max(dp3[v][a], k - 1), pr = k - 1;
if (a < x) pr = max(dp3[v][a + 1], k - 1);
an += (va - pr) * a;
}
}
int main() {
cin >> n;
for (int a = 0; a < n - 1; a++) {
int u, v;
scanf("%d%d", &u, &v);
u--, v--;
sv[u].push_back(v);
sv[v].push_back(u);
}
while (1) {
int q = 0, va = 1;
for (int a = 0; a <= x; a++) {
q += va, va *= k;
}
if (q > n) break;
dfs(0, -1);
k++;
}
dfs2(0, -1);
cout << an;
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int sz = 3e5 + 10, x = 5;
vector<int> sv[sz];
int n, k = 1, dp[sz], dp2[sz][x + 1], dp3[sz][x + 1];
long long an = 0;
int dfs(int v, int pr) {
int re = 0;
dp[v] = 1;
vector<int> sp;
for (int a = 0; a < sv[v].size(); a++) {
int ne = sv[v][a];
if (ne != pr) {
re = max(re, dfs(ne, v));
sp.push_back(dp[ne]);
}
}
if (sp.size() >= k) {
sort(sp.begin(), sp.end(), greater<int>());
dp[v] = sp[k - 1] + 1;
}
re = max(re, dp[v]);
an += re;
return re;
}
void dfs2(int v, int pr) {
dp2[v][1] = n;
vector<int> sp[x + 1];
for (int a = 0; a < sv[v].size(); a++) {
int ne = sv[v][a];
if (ne != pr) {
dfs2(ne, v);
for (int b = 1; b <= x; b++) dp3[v][b] = max(dp3[v][b], dp3[ne][b]);
for (int b = 2; b <= x; b++) sp[b].push_back(dp2[ne][b - 1]);
}
}
for (int a = 2; a <= x; a++) {
sort(sp[a].begin(), sp[a].end(), greater<int>());
for (int b = 0; b < sp[a].size(); b++)
if (sp[a][b] >= b + 1) dp2[v][a] = max(dp2[v][a], b + 1);
}
for (int a = 1; a <= x; a++) dp3[v][a] = max(dp3[v][a], dp2[v][a]);
for (int a = 1; a <= x; a++) {
int va = max(dp3[v][a], k - 1), pr = k - 1;
if (a < x) pr = max(dp3[v][a + 1], k - 1);
an += (va - pr) * a;
}
}
int main() {
cin >> n;
for (int a = 0; a < n - 1; a++) {
int u, v;
scanf("%d%d", &u, &v);
u--, v--;
sv[u].push_back(v);
sv[v].push_back(u);
}
while (1) {
int q = 0, va = 1;
for (int a = 0; a <= x; a++) {
q += va, va *= k;
}
if (q > n) break;
dfs(0, -1);
k++;
}
dfs2(0, -1);
cout << an;
}
``` |
#include <bits/stdc++.h>
inline bool C(int a, int b) { return a > b; }
inline void inc(int& a, const int& b) {
if (a < b) a = b;
}
struct d {
d* n;
int t;
d(d* a, int b) : n(a), t(b) {}
} * G[300005];
void I(int f, int t) { G[f] = new d(G[f], t); }
int A[300005], M[300005][20], F[300005][20], V[300005], n;
long long T;
void s(int x, int f) {
V[x] = 1;
M[x][1] = F[x][1] = n;
for (d* i = G[x]; i; i = i->n)
if (i->t != f) s(i->t, x), inc(V[x], V[i->t] + 1);
for (int i = 2, m; m = 0, i < 19; ++i) {
for (d* j = G[x]; j; j = j->n)
if (j->t != f) A[m++] = F[j->t][i - 1];
std::sort(A, A + m, C);
A[m] = 0;
while (F[x][i] < A[F[x][i]]) ++F[x][i];
inc(F[x][i], 1);
M[x][i] = F[x][i];
for (d* j = G[x]; j; j = j->n)
if (j->t != f) inc(M[x][i], M[j->t][i]);
}
M[x][19] = F[x][19] = 1;
T += V[x];
for (int i = 1; i < 19; ++i) T += i * (M[x][i] - M[x][i + 1]);
}
int main() {
scanf("%d", &n);
for (int i = 1, a, b; i < n; ++i) {
scanf("%d%d", &a, &b);
I(a, b);
I(b, a);
}
s(1, 0);
return !printf("%lld\n", T);
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
inline bool C(int a, int b) { return a > b; }
inline void inc(int& a, const int& b) {
if (a < b) a = b;
}
struct d {
d* n;
int t;
d(d* a, int b) : n(a), t(b) {}
} * G[300005];
void I(int f, int t) { G[f] = new d(G[f], t); }
int A[300005], M[300005][20], F[300005][20], V[300005], n;
long long T;
void s(int x, int f) {
V[x] = 1;
M[x][1] = F[x][1] = n;
for (d* i = G[x]; i; i = i->n)
if (i->t != f) s(i->t, x), inc(V[x], V[i->t] + 1);
for (int i = 2, m; m = 0, i < 19; ++i) {
for (d* j = G[x]; j; j = j->n)
if (j->t != f) A[m++] = F[j->t][i - 1];
std::sort(A, A + m, C);
A[m] = 0;
while (F[x][i] < A[F[x][i]]) ++F[x][i];
inc(F[x][i], 1);
M[x][i] = F[x][i];
for (d* j = G[x]; j; j = j->n)
if (j->t != f) inc(M[x][i], M[j->t][i]);
}
M[x][19] = F[x][19] = 1;
T += V[x];
for (int i = 1; i < 19; ++i) T += i * (M[x][i] - M[x][i + 1]);
}
int main() {
scanf("%d", &n);
for (int i = 1, a, b; i < n; ++i) {
scanf("%d%d", &a, &b);
I(a, b);
I(b, a);
}
s(1, 0);
return !printf("%lld\n", T);
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f, oo = inf;
inline long long read() {
register long long x = 0, f = 1;
register char c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') f = -1;
for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x * f;
}
void write(long long x) {
if (x < 0) x = -x, putchar('-');
if (x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
void writeln(long long x) {
write(x);
puts("");
}
const long long maxn = 3e5 + 233;
struct Edge {
long long to, nxt;
Edge() {}
Edge(long long to, long long nxt) : to(to), nxt(nxt) {}
} edge[maxn * 2];
long long first[maxn], nume;
long long deep[maxn], dp[maxn][20], n;
long long ans = 0;
void Addedge(long long a, long long b) {
edge[nume] = Edge(b, first[a]);
first[a] = nume++;
}
bool cmp(long long a, long long b) { return a > b; }
long long q[maxn];
void dfs(long long u, long long fa) {
for (long long e = first[u]; ~e; e = edge[e].nxt) {
long long v = edge[e].to;
if (v == fa) continue;
dfs(v, u);
deep[u] = max(deep[u], deep[v]);
}
deep[u]++;
ans += deep[u];
dp[u][1] = n;
for (long long i = 2; i < 20; i++) {
long long tot = 0;
for (long long e = first[u]; ~e; e = edge[e].nxt) {
long long v = edge[e].to;
if (v == fa) continue;
if (dp[v][i - 1] > 1) q[++tot] = dp[v][i - 1];
}
sort(q + 1, q + 1 + tot, cmp);
for (long long j = tot; j >= 2; j--)
if (q[j] >= j) {
dp[u][i] = j;
break;
}
}
}
void pushup(long long u, long long fa) {
for (long long e = first[u]; ~e; e = edge[e].nxt) {
long long v = edge[e].to;
if (v == fa) continue;
pushup(v, u);
for (long long i = 0; i < 20; i++) {
dp[u][i] = max(dp[u][i], dp[v][i]);
}
}
for (long long i = 1; i < 19; i++) {
ans = ans + i * (dp[u][i] - dp[u][i + 1]);
if (dp[u][i + 1] == 0) {
ans -= i;
break;
}
}
}
signed main() {
n = read();
memset(first, -1, sizeof(first));
nume = 0;
for (register long long i = (1); i < (n); ++i) {
long long a = read(), b = read();
Addedge(a, b);
Addedge(b, a);
}
dfs(1, 0);
pushup(1, 0);
writeln(ans);
return 0;
}
| ### Prompt
In cpp, your task is to solve the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f, oo = inf;
inline long long read() {
register long long x = 0, f = 1;
register char c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') f = -1;
for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x * f;
}
void write(long long x) {
if (x < 0) x = -x, putchar('-');
if (x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
void writeln(long long x) {
write(x);
puts("");
}
const long long maxn = 3e5 + 233;
struct Edge {
long long to, nxt;
Edge() {}
Edge(long long to, long long nxt) : to(to), nxt(nxt) {}
} edge[maxn * 2];
long long first[maxn], nume;
long long deep[maxn], dp[maxn][20], n;
long long ans = 0;
void Addedge(long long a, long long b) {
edge[nume] = Edge(b, first[a]);
first[a] = nume++;
}
bool cmp(long long a, long long b) { return a > b; }
long long q[maxn];
void dfs(long long u, long long fa) {
for (long long e = first[u]; ~e; e = edge[e].nxt) {
long long v = edge[e].to;
if (v == fa) continue;
dfs(v, u);
deep[u] = max(deep[u], deep[v]);
}
deep[u]++;
ans += deep[u];
dp[u][1] = n;
for (long long i = 2; i < 20; i++) {
long long tot = 0;
for (long long e = first[u]; ~e; e = edge[e].nxt) {
long long v = edge[e].to;
if (v == fa) continue;
if (dp[v][i - 1] > 1) q[++tot] = dp[v][i - 1];
}
sort(q + 1, q + 1 + tot, cmp);
for (long long j = tot; j >= 2; j--)
if (q[j] >= j) {
dp[u][i] = j;
break;
}
}
}
void pushup(long long u, long long fa) {
for (long long e = first[u]; ~e; e = edge[e].nxt) {
long long v = edge[e].to;
if (v == fa) continue;
pushup(v, u);
for (long long i = 0; i < 20; i++) {
dp[u][i] = max(dp[u][i], dp[v][i]);
}
}
for (long long i = 1; i < 19; i++) {
ans = ans + i * (dp[u][i] - dp[u][i + 1]);
if (dp[u][i + 1] == 0) {
ans -= i;
break;
}
}
}
signed main() {
n = read();
memset(first, -1, sizeof(first));
nume = 0;
for (register long long i = (1); i < (n); ++i) {
long long a = read(), b = read();
Addedge(a, b);
Addedge(b, a);
}
dfs(1, 0);
pushup(1, 0);
writeln(ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long ans;
const int C = 20;
vector<int> adj[300010];
int dp[C + 2][300010];
int mx[C + 2][300010];
int h[300010];
int n;
void dfs(int root, int dad = -1) {
h[root] = 1;
for (auto x : adj[root])
if (x != dad) {
dfs(x, root);
h[root] = max(h[root], h[x] + 1);
for (int(k) = (0); (k) < (C); (k)++)
mx[k][root] = max(mx[k][root], mx[k][x]);
}
dp[1][root] = n;
mx[1][root] = n;
for (int(d) = (2); (d) < (C); (d)++) {
int st = 0;
int en = n + 1;
while (st != en - 1) {
int mid = (st + en) / 2;
int cnt = 0;
for (auto x : adj[root])
if (x != dad && dp[d - 1][x] >= mid) cnt++;
if (cnt >= mid)
st = mid;
else
en = mid;
}
dp[d][root] = st;
mx[d][root] = max(mx[d][root], st);
}
int mm = 0;
for (int d = C - 1; d > 0; d--) {
ans += d * (mx[d][root] - mx[d + 1][root]);
if (mx[d][root]) mm = max(mm, d);
}
ans -= mm;
ans += h[root];
}
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (int(i) = (1); (i) < (n); (i)++) {
int v, u;
cin >> v >> u;
v--, u--;
adj[v].push_back(u);
adj[u].push_back(v);
}
dfs(0);
cout << ans << "\n";
}
| ### Prompt
Create a solution in Cpp for the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long ans;
const int C = 20;
vector<int> adj[300010];
int dp[C + 2][300010];
int mx[C + 2][300010];
int h[300010];
int n;
void dfs(int root, int dad = -1) {
h[root] = 1;
for (auto x : adj[root])
if (x != dad) {
dfs(x, root);
h[root] = max(h[root], h[x] + 1);
for (int(k) = (0); (k) < (C); (k)++)
mx[k][root] = max(mx[k][root], mx[k][x]);
}
dp[1][root] = n;
mx[1][root] = n;
for (int(d) = (2); (d) < (C); (d)++) {
int st = 0;
int en = n + 1;
while (st != en - 1) {
int mid = (st + en) / 2;
int cnt = 0;
for (auto x : adj[root])
if (x != dad && dp[d - 1][x] >= mid) cnt++;
if (cnt >= mid)
st = mid;
else
en = mid;
}
dp[d][root] = st;
mx[d][root] = max(mx[d][root], st);
}
int mm = 0;
for (int d = C - 1; d > 0; d--) {
ans += d * (mx[d][root] - mx[d + 1][root]);
if (mx[d][root]) mm = max(mm, d);
}
ans -= mm;
ans += h[root];
}
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (int(i) = (1); (i) < (n); (i)++) {
int v, u;
cin >> v >> u;
v--, u--;
adj[v].push_back(u);
adj[u].push_back(v);
}
dfs(0);
cout << ans << "\n";
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<int> g[300005];
int dp[300005][25];
int D[300005];
int n;
void dfs1(int u, int p) {
D[u] = 1;
for (int i = 0; i < (int)(g[u]).size(); i++) {
int v = g[u][i];
if (v != p) {
dfs1(v, u);
D[u] = max(D[u], D[v] + 1);
}
}
for (int d = 2; d < 20; d++) {
vector<int> sum;
for (int i = 0; i < (int)(g[u]).size(); i++) {
int v = g[u][i];
if (v != p) sum.emplace_back(dp[v][d - 1]);
}
sort((sum).begin(), (sum).end());
reverse((sum).begin(), (sum).end());
int mn = 1e9;
for (int i = 0; i < (int)(sum).size(); i++) {
mn = min(mn, sum[i]);
dp[u][d] = max(dp[u][d], min(i + 1, mn));
}
}
dp[u][1] = n;
}
void dfs2(int u, int p) {
for (int i = 0; i < (int)(g[u]).size(); i++) {
int v = g[u][i];
if (v != p) {
dfs2(v, u);
for (int d = 1; d < 20; d++) {
dp[u][d] = max(dp[u][d], dp[v][d]);
}
}
}
for (int d = 18; d >= 1; d--) dp[u][d] = max(dp[u][d], dp[u][d + 1]);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n - 1; i++) {
int a, b;
scanf("%d%d", &a, &b);
g[a].emplace_back(b);
g[b].emplace_back(a);
}
dfs1(1, 0);
dfs2(1, 0);
long long ans = 0;
for (int u = 1; u <= n; u++) {
if (D[u] > 19) {
ans += D[u];
}
for (int d = 19; d >= 1; d--) {
int diff = dp[u][d] - dp[u][d + 1];
if (D[u] > 19 && d == 19) diff--;
ans += diff * 1ll * d;
}
}
printf("%lld\n", ans);
}
| ### Prompt
Please create a solution in CPP to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> g[300005];
int dp[300005][25];
int D[300005];
int n;
void dfs1(int u, int p) {
D[u] = 1;
for (int i = 0; i < (int)(g[u]).size(); i++) {
int v = g[u][i];
if (v != p) {
dfs1(v, u);
D[u] = max(D[u], D[v] + 1);
}
}
for (int d = 2; d < 20; d++) {
vector<int> sum;
for (int i = 0; i < (int)(g[u]).size(); i++) {
int v = g[u][i];
if (v != p) sum.emplace_back(dp[v][d - 1]);
}
sort((sum).begin(), (sum).end());
reverse((sum).begin(), (sum).end());
int mn = 1e9;
for (int i = 0; i < (int)(sum).size(); i++) {
mn = min(mn, sum[i]);
dp[u][d] = max(dp[u][d], min(i + 1, mn));
}
}
dp[u][1] = n;
}
void dfs2(int u, int p) {
for (int i = 0; i < (int)(g[u]).size(); i++) {
int v = g[u][i];
if (v != p) {
dfs2(v, u);
for (int d = 1; d < 20; d++) {
dp[u][d] = max(dp[u][d], dp[v][d]);
}
}
}
for (int d = 18; d >= 1; d--) dp[u][d] = max(dp[u][d], dp[u][d + 1]);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n - 1; i++) {
int a, b;
scanf("%d%d", &a, &b);
g[a].emplace_back(b);
g[b].emplace_back(a);
}
dfs1(1, 0);
dfs2(1, 0);
long long ans = 0;
for (int u = 1; u <= n; u++) {
if (D[u] > 19) {
ans += D[u];
}
for (int d = 19; d >= 1; d--) {
int diff = dp[u][d] - dp[u][d + 1];
if (D[u] > 19 && d == 19) diff--;
ans += diff * 1ll * d;
}
}
printf("%lld\n", ans);
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const long long inf_ll = 1e18;
const int N = 3e5 + 5;
long long d[N], pt[N], ans, sum;
vector<long long> adj[N], dp[N], s;
multiset<long long> f[N];
void dfs1(int i, int p) {
pt[i] = d[i] = adj[i].size() - (i != 0);
dp[i].resize(d[i]);
for (auto& j : adj[i])
if (j != p) {
dfs1(j, i);
for (int k = 0; k < min(d[i], d[j]); k++) dp[i][k]++;
pt[i] += pt[j];
}
for (long long k = 0; k < d[i]; k++) {
if (dp[i][k] <= k)
dp[i][k] = 1;
else {
s.clear();
for (auto& j : adj[i])
if (j != p && d[j] > k) s.push_back(dp[j][k]);
nth_element(s.begin(), s.end() - 1 - k, s.end());
;
dp[i][k] = s[s.size() - 1 - k] + 1;
}
};
}
void ins(int i, int p) {
for (int j = 0; j < d[i]; j++) {
long long pre = f[j].empty() ? 0 : *--f[j].end();
f[j].insert(dp[i][j]);
sum += *--f[j].end() - pre;
}
for (auto& j : adj[i])
if (j != p) ins(j, i);
}
void rem(int i, int p) {
for (int j = 0; j < d[i]; j++) {
long long pre = *--f[j].end();
f[j].erase(f[j].find(dp[i][j]));
sum += (f[j].empty() ? 0 : *--f[j].end()) - pre;
}
for (auto& j : adj[i])
if (j != p) rem(j, i);
}
void dfs2(int i, int p) {
int k = -1;
for (auto& j : adj[i])
if (j != p && (k == -1 || pt[j] > pt[k])) k = j;
for (auto& j : adj[i])
if (j != p && j != k) dfs2(j, i), rem(j, i);
if (k != -1) dfs2(k, i);
for (auto& j : adj[i])
if (j != p && j != k) ins(j, i);
for (int j = 0; j < d[i]; j++) {
long long pre = f[j].empty() ? 0 : *--f[j].end();
f[j].insert(dp[i][j]);
sum += *--f[j].end() - pre;
};
ans += sum;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--, v--;
adj[u].push_back(v), adj[v].push_back(u);
}
dfs1(0, 0);
dfs2(0, 0);
cout << ans + n * n << "\n";
}
| ### Prompt
In Cpp, your task is to solve the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const long long inf_ll = 1e18;
const int N = 3e5 + 5;
long long d[N], pt[N], ans, sum;
vector<long long> adj[N], dp[N], s;
multiset<long long> f[N];
void dfs1(int i, int p) {
pt[i] = d[i] = adj[i].size() - (i != 0);
dp[i].resize(d[i]);
for (auto& j : adj[i])
if (j != p) {
dfs1(j, i);
for (int k = 0; k < min(d[i], d[j]); k++) dp[i][k]++;
pt[i] += pt[j];
}
for (long long k = 0; k < d[i]; k++) {
if (dp[i][k] <= k)
dp[i][k] = 1;
else {
s.clear();
for (auto& j : adj[i])
if (j != p && d[j] > k) s.push_back(dp[j][k]);
nth_element(s.begin(), s.end() - 1 - k, s.end());
;
dp[i][k] = s[s.size() - 1 - k] + 1;
}
};
}
void ins(int i, int p) {
for (int j = 0; j < d[i]; j++) {
long long pre = f[j].empty() ? 0 : *--f[j].end();
f[j].insert(dp[i][j]);
sum += *--f[j].end() - pre;
}
for (auto& j : adj[i])
if (j != p) ins(j, i);
}
void rem(int i, int p) {
for (int j = 0; j < d[i]; j++) {
long long pre = *--f[j].end();
f[j].erase(f[j].find(dp[i][j]));
sum += (f[j].empty() ? 0 : *--f[j].end()) - pre;
}
for (auto& j : adj[i])
if (j != p) rem(j, i);
}
void dfs2(int i, int p) {
int k = -1;
for (auto& j : adj[i])
if (j != p && (k == -1 || pt[j] > pt[k])) k = j;
for (auto& j : adj[i])
if (j != p && j != k) dfs2(j, i), rem(j, i);
if (k != -1) dfs2(k, i);
for (auto& j : adj[i])
if (j != p && j != k) ins(j, i);
for (int j = 0; j < d[i]; j++) {
long long pre = f[j].empty() ? 0 : *--f[j].end();
f[j].insert(dp[i][j]);
sum += *--f[j].end() - pre;
};
ans += sum;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--, v--;
adj[u].push_back(v), adj[v].push_back(u);
}
dfs1(0, 0);
dfs2(0, 0);
cout << ans + n * n << "\n";
}
``` |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int const nmax = 300000;
vector<int> g[1 + nmax];
vector<pair<int, int>> dp[1 + nmax];
int n;
int extract(int node, int l) {
int pos = 0;
for (int jump = 16; 0 < jump; jump /= 2)
if (pos + jump < dp[node].size() && dp[node][pos + jump].second <= l)
pos += jump;
return dp[node][pos].first;
}
int eval(int node, int l) {
if (g[node].size() < l)
return 1;
else {
vector<int> v;
for (int h = 0; h < g[node].size(); h++) {
int to = g[node][h];
v.push_back(extract(to, l));
}
sort(v.begin(), v.end());
reverse(v.begin(), v.end());
return v[l - 1] + 1;
}
}
void dfs(int node, int parent) {
for (int h = 0; h < g[node].size(); h++) {
int to = g[node][h];
if (to == parent) g[node].erase(g[node].begin() + h);
}
for (int h = 0; h < g[node].size(); h++) {
int to = g[node][h];
dfs(to, node);
}
if (g[node].size() == 0) {
dp[node].push_back({1, 1});
dp[node].push_back({0, n + 1});
} else {
int ptr = 1;
while (ptr <= n) {
int sol = eval(node, ptr);
dp[node].push_back({sol, ptr});
for (int jump = (1 << 17); 0 < jump; jump /= 2)
if (eval(node, ptr + jump) == sol) ptr += jump;
ptr++;
}
dp[node].push_back({0, n + 1});
}
}
int neweval(int node, int l) {
int result = extract(node, l);
for (int h = 0; h < g[node].size(); h++) {
int to = g[node][h];
result = max(result, extract(to, l));
}
return result;
}
void dfs2(int node, int parent) {
for (int h = 0; h < g[node].size(); h++) {
int to = g[node][h];
dfs2(to, node);
}
vector<pair<int, int>> newdp;
int ptr = 1;
while (ptr <= n) {
int sol = neweval(node, ptr);
newdp.push_back({sol, ptr});
for (int jump = (1 << 17); 0 < jump; jump /= 2)
if (neweval(node, ptr + jump) == sol) ptr += jump;
ptr++;
}
newdp.push_back({0, n + 1});
dp[node] = newdp;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
dfs(1, 0);
dfs2(1, 0);
ll result = 0;
for (int i = 1; i <= n; i++)
for (int h = 0; h < dp[i].size() - 1; h++)
result += 1LL * dp[i][h].first * (dp[i][h + 1].second - dp[i][h].second);
cout << result;
return 0;
}
| ### Prompt
Construct a Cpp code solution to the problem outlined:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int const nmax = 300000;
vector<int> g[1 + nmax];
vector<pair<int, int>> dp[1 + nmax];
int n;
int extract(int node, int l) {
int pos = 0;
for (int jump = 16; 0 < jump; jump /= 2)
if (pos + jump < dp[node].size() && dp[node][pos + jump].second <= l)
pos += jump;
return dp[node][pos].first;
}
int eval(int node, int l) {
if (g[node].size() < l)
return 1;
else {
vector<int> v;
for (int h = 0; h < g[node].size(); h++) {
int to = g[node][h];
v.push_back(extract(to, l));
}
sort(v.begin(), v.end());
reverse(v.begin(), v.end());
return v[l - 1] + 1;
}
}
void dfs(int node, int parent) {
for (int h = 0; h < g[node].size(); h++) {
int to = g[node][h];
if (to == parent) g[node].erase(g[node].begin() + h);
}
for (int h = 0; h < g[node].size(); h++) {
int to = g[node][h];
dfs(to, node);
}
if (g[node].size() == 0) {
dp[node].push_back({1, 1});
dp[node].push_back({0, n + 1});
} else {
int ptr = 1;
while (ptr <= n) {
int sol = eval(node, ptr);
dp[node].push_back({sol, ptr});
for (int jump = (1 << 17); 0 < jump; jump /= 2)
if (eval(node, ptr + jump) == sol) ptr += jump;
ptr++;
}
dp[node].push_back({0, n + 1});
}
}
int neweval(int node, int l) {
int result = extract(node, l);
for (int h = 0; h < g[node].size(); h++) {
int to = g[node][h];
result = max(result, extract(to, l));
}
return result;
}
void dfs2(int node, int parent) {
for (int h = 0; h < g[node].size(); h++) {
int to = g[node][h];
dfs2(to, node);
}
vector<pair<int, int>> newdp;
int ptr = 1;
while (ptr <= n) {
int sol = neweval(node, ptr);
newdp.push_back({sol, ptr});
for (int jump = (1 << 17); 0 < jump; jump /= 2)
if (neweval(node, ptr + jump) == sol) ptr += jump;
ptr++;
}
newdp.push_back({0, n + 1});
dp[node] = newdp;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
dfs(1, 0);
dfs2(1, 0);
ll result = 0;
for (int i = 1; i <= n; i++)
for (int h = 0; h < dp[i].size() - 1; h++)
result += 1LL * dp[i][h].first * (dp[i][h + 1].second - dp[i][h].second);
cout << result;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
bool home = 1;
signed realMain();
signed main() {
home = 0;
if (home) {
freopen("input", "r", stdin);
} else {
ios::sync_with_stdio(0);
cin.tie(0);
}
realMain();
}
const long long N = (long long)3e5 + 7;
const long long M = 20;
long long n, dep[N], ret, dp[N][M], down[N];
vector<long long> g[N];
void build(long long a) {
down[a] = 1;
vector<long long> kids;
for (auto &b : g[a]) {
if (dep[b]) continue;
kids.push_back(b);
dep[b] = 1 + dep[a];
build(b);
down[a] = max(down[a], 1 + down[b]);
}
g[a] = kids;
for (long long j = 0; j < M; j++) dp[a][j] = -1;
}
void dfs(long long a) {
dp[a][1] = n;
for (auto &b : g[a]) dfs(b);
for (long long h = 2; h < M; h++) {
vector<long long> lol;
for (auto &b : g[a]) {
lol.push_back(dp[b][h - 1]);
}
sort(lol.rbegin(), lol.rend());
for (long long k = 1; k <= (long long)g[a].size(); k++) {
if (k <= lol[k - 1]) {
dp[a][h] = k;
}
}
}
}
void dfs2(long long a) {
for (auto &b : g[a]) {
dfs2(b);
for (long long h = 1; h < M; h++) {
dp[a][h] = max(dp[a][h], dp[b][h]);
}
}
}
signed realMain() {
cin >> n;
for (long long i = 1; i < n; i++) {
long long x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
dep[1] = 1;
build(1);
for (long long i = 1; i <= n; i++) {
ret += down[i];
}
dfs(1);
dfs2(1);
for (long long i = 1; i <= n; i++) {
long long last = 1;
for (long long h = M - 1; h >= 1; h--) {
if (dp[i][h] != -1) {
ret += (dp[i][h] - last) * h;
last = dp[i][h];
}
}
}
cout << ret << "\n";
return 0;
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
bool home = 1;
signed realMain();
signed main() {
home = 0;
if (home) {
freopen("input", "r", stdin);
} else {
ios::sync_with_stdio(0);
cin.tie(0);
}
realMain();
}
const long long N = (long long)3e5 + 7;
const long long M = 20;
long long n, dep[N], ret, dp[N][M], down[N];
vector<long long> g[N];
void build(long long a) {
down[a] = 1;
vector<long long> kids;
for (auto &b : g[a]) {
if (dep[b]) continue;
kids.push_back(b);
dep[b] = 1 + dep[a];
build(b);
down[a] = max(down[a], 1 + down[b]);
}
g[a] = kids;
for (long long j = 0; j < M; j++) dp[a][j] = -1;
}
void dfs(long long a) {
dp[a][1] = n;
for (auto &b : g[a]) dfs(b);
for (long long h = 2; h < M; h++) {
vector<long long> lol;
for (auto &b : g[a]) {
lol.push_back(dp[b][h - 1]);
}
sort(lol.rbegin(), lol.rend());
for (long long k = 1; k <= (long long)g[a].size(); k++) {
if (k <= lol[k - 1]) {
dp[a][h] = k;
}
}
}
}
void dfs2(long long a) {
for (auto &b : g[a]) {
dfs2(b);
for (long long h = 1; h < M; h++) {
dp[a][h] = max(dp[a][h], dp[b][h]);
}
}
}
signed realMain() {
cin >> n;
for (long long i = 1; i < n; i++) {
long long x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
dep[1] = 1;
build(1);
for (long long i = 1; i <= n; i++) {
ret += down[i];
}
dfs(1);
dfs2(1);
for (long long i = 1; i <= n; i++) {
long long last = 1;
for (long long h = M - 1; h >= 1; h--) {
if (dp[i][h] != -1) {
ret += (dp[i][h] - last) * h;
last = dp[i][h];
}
}
}
cout << ret << "\n";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
int n;
vector<int> adj[N];
int h[N];
int f[N][20], g[N][20];
void dfs(int u, int p) {
for (int v : adj[u])
if (v != p) {
dfs(v, u);
h[u] = max(h[u], h[v] + 1);
}
f[u][1] = g[u][1] = n;
for (int i = 2; i <= 19; ++i) {
int l = 0, r = n;
while (l < r) {
int mid = (l + r + 1) >> 1;
int cnt = 0;
for (int v : adj[u])
if (v != p) {
if (g[v][i - 1] >= mid) cnt++;
}
if (cnt >= mid)
l = mid;
else
r = mid - 1;
}
g[u][i] = l;
for (int v : adj[u])
if (v != p) {
f[u][i] = max(f[u][i], f[v][i]);
}
f[u][i] = max(f[u][i], g[u][i]);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n - 1; ++i) {
int u, v;
cin >> u >> v;
adj[u].push_back(v), adj[v].push_back(u);
}
dfs(1, 1);
long long res = 0;
for (int u = 1; u <= n; ++u) res += h[u];
for (int u = 1; u <= n; ++u) {
for (int i = 1; i <= 19; ++i) {
int l = 2;
if (i < 19) l = max(l, f[u][i + 1] + 1);
int r = f[u][i];
if (l <= r) res += 1LL * (r - l + 1) * (i - 1);
}
}
res += 1LL * n * n;
cout << res << '\n';
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
int n;
vector<int> adj[N];
int h[N];
int f[N][20], g[N][20];
void dfs(int u, int p) {
for (int v : adj[u])
if (v != p) {
dfs(v, u);
h[u] = max(h[u], h[v] + 1);
}
f[u][1] = g[u][1] = n;
for (int i = 2; i <= 19; ++i) {
int l = 0, r = n;
while (l < r) {
int mid = (l + r + 1) >> 1;
int cnt = 0;
for (int v : adj[u])
if (v != p) {
if (g[v][i - 1] >= mid) cnt++;
}
if (cnt >= mid)
l = mid;
else
r = mid - 1;
}
g[u][i] = l;
for (int v : adj[u])
if (v != p) {
f[u][i] = max(f[u][i], f[v][i]);
}
f[u][i] = max(f[u][i], g[u][i]);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n - 1; ++i) {
int u, v;
cin >> u >> v;
adj[u].push_back(v), adj[v].push_back(u);
}
dfs(1, 1);
long long res = 0;
for (int u = 1; u <= n; ++u) res += h[u];
for (int u = 1; u <= n; ++u) {
for (int i = 1; i <= 19; ++i) {
int l = 2;
if (i < 19) l = max(l, f[u][i + 1] + 1);
int r = f[u][i];
if (l <= r) res += 1LL * (r - l + 1) * (i - 1);
}
}
res += 1LL * n * n;
cout << res << '\n';
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
bool chkmax(T &a, T b) {
return (a < b) ? a = b, 1 : 0;
}
template <typename T>
bool chkmin(T &a, T b) {
return (a > b) ? a = b, 1 : 0;
}
inline int read() {
int x = 0, fh = 1;
char ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-') fh = -1;
for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ '0');
return x * fh;
}
int n;
int he[400005], to[1200005], ne[1200005], e;
void add(int x, int y) {
to[++e] = y;
ne[e] = he[x];
he[x] = e;
}
long long dp[400005][20];
long long dep[400005];
long long num[400005], tmp;
long long ans;
void dfs(int x, int ff) {
dep[x] = 1;
for (int i = he[x]; i; i = ne[i]) {
int v = to[i];
if (v == ff) continue;
dfs(v, x);
chkmax(dep[x], dep[v] + 1);
}
dp[x][1] = n;
for (int i = (2), _end_ = (int)(19); i <= _end_; ++i) {
tmp = 0;
for (int j = he[x]; j; j = ne[j]) {
int v = to[j];
if (v == ff) continue;
if (dp[v][i - 1]) {
num[++tmp] = dp[v][i - 1];
}
}
sort(num + 1, num + tmp + 1);
for (int j = (tmp), _end_ = (int)(1); j >= _end_; --j) {
if (num[tmp - j + 1] >= j) {
dp[x][i] = j;
break;
}
}
}
}
void dfs2(int x, int ff) {
for (int i = he[x]; i; i = ne[i]) {
int v = to[i];
if (v == ff) continue;
dfs2(v, x);
for (int j = (2), _end_ = (int)(19); j <= _end_; ++j)
chkmax(dp[x][j], dp[v][j]);
}
}
int main() {
n = read();
for (int i = (1), _end_ = (int)(n - 1); i <= _end_; ++i) {
int x = read(), y = read();
add(x, y);
add(y, x);
}
dfs(1, 0);
dfs2(1, 0);
for (int i = (1), _end_ = (int)(n); i <= _end_; ++i) ans += dep[i];
for (int i = (1), _end_ = (int)(n); i <= _end_; ++i)
for (int j = (1), _end_ = (int)(19); j <= _end_; ++j) {
if (dp[i][j]) ans += dp[i][j] - 1;
}
printf("%lld\n", ans);
return 0;
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T>
bool chkmax(T &a, T b) {
return (a < b) ? a = b, 1 : 0;
}
template <typename T>
bool chkmin(T &a, T b) {
return (a > b) ? a = b, 1 : 0;
}
inline int read() {
int x = 0, fh = 1;
char ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-') fh = -1;
for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ '0');
return x * fh;
}
int n;
int he[400005], to[1200005], ne[1200005], e;
void add(int x, int y) {
to[++e] = y;
ne[e] = he[x];
he[x] = e;
}
long long dp[400005][20];
long long dep[400005];
long long num[400005], tmp;
long long ans;
void dfs(int x, int ff) {
dep[x] = 1;
for (int i = he[x]; i; i = ne[i]) {
int v = to[i];
if (v == ff) continue;
dfs(v, x);
chkmax(dep[x], dep[v] + 1);
}
dp[x][1] = n;
for (int i = (2), _end_ = (int)(19); i <= _end_; ++i) {
tmp = 0;
for (int j = he[x]; j; j = ne[j]) {
int v = to[j];
if (v == ff) continue;
if (dp[v][i - 1]) {
num[++tmp] = dp[v][i - 1];
}
}
sort(num + 1, num + tmp + 1);
for (int j = (tmp), _end_ = (int)(1); j >= _end_; --j) {
if (num[tmp - j + 1] >= j) {
dp[x][i] = j;
break;
}
}
}
}
void dfs2(int x, int ff) {
for (int i = he[x]; i; i = ne[i]) {
int v = to[i];
if (v == ff) continue;
dfs2(v, x);
for (int j = (2), _end_ = (int)(19); j <= _end_; ++j)
chkmax(dp[x][j], dp[v][j]);
}
}
int main() {
n = read();
for (int i = (1), _end_ = (int)(n - 1); i <= _end_; ++i) {
int x = read(), y = read();
add(x, y);
add(y, x);
}
dfs(1, 0);
dfs2(1, 0);
for (int i = (1), _end_ = (int)(n); i <= _end_; ++i) ans += dep[i];
for (int i = (1), _end_ = (int)(n); i <= _end_; ++i)
for (int j = (1), _end_ = (int)(19); j <= _end_; ++j) {
if (dp[i][j]) ans += dp[i][j] - 1;
}
printf("%lld\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 300005;
int fa[N], f[N], g[N], sn[N], id[N], n, u, v;
vector<int> s[N], e[N], tmp[N];
long long ans, sum;
int clk;
int dfs(int u) {
int mx = 1;
id[++clk] = u;
for (auto v : e[u])
if (v != fa[u]) {
fa[v] = u;
mx = max(mx, dfs(v) + 1);
++sn[u];
}
ans += mx;
return mx;
}
void up(int u, int now) {
if (now <= g[u]) return;
sum += now - g[u], g[u] = now;
if (fa[u]) up(fa[u], now);
}
int cnt[N];
long long solve(int k) {
for (auto u : s[k]) f[u] = n, cnt[u] = 0, tmp[u].clear();
for (auto u : s[k]) {
if (cnt[u] < k)
f[u] = 2;
else {
sort(tmp[u].begin(), tmp[u].end(), greater<int>());
f[u] = tmp[u][k - 1];
}
if (fa[u]) tmp[fa[u]].push_back(f[u] + 1), cnt[fa[u]]++;
}
long long res = n - s[k].size();
for (auto u : s[k]) up(u, f[u]);
return sum;
}
int main() {
cin >> n;
for (int i = (1); i <= (n - 1); i++) {
cin >> u >> v;
e[u].push_back(v);
e[v].push_back(u);
}
dfs(1);
for (int i = (n); i >= (1); i--) {
int u = id[i];
for (int j = (1); j <= (sn[u]); j++) s[j].push_back(u);
}
for (int i = (1); i <= (n); i++) g[i] = 1, sum++;
for (int i = (n); i >= (2); i--) ans += solve(i);
cout << ans << endl;
return 0;
}
| ### Prompt
Please create a solution in Cpp to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 300005;
int fa[N], f[N], g[N], sn[N], id[N], n, u, v;
vector<int> s[N], e[N], tmp[N];
long long ans, sum;
int clk;
int dfs(int u) {
int mx = 1;
id[++clk] = u;
for (auto v : e[u])
if (v != fa[u]) {
fa[v] = u;
mx = max(mx, dfs(v) + 1);
++sn[u];
}
ans += mx;
return mx;
}
void up(int u, int now) {
if (now <= g[u]) return;
sum += now - g[u], g[u] = now;
if (fa[u]) up(fa[u], now);
}
int cnt[N];
long long solve(int k) {
for (auto u : s[k]) f[u] = n, cnt[u] = 0, tmp[u].clear();
for (auto u : s[k]) {
if (cnt[u] < k)
f[u] = 2;
else {
sort(tmp[u].begin(), tmp[u].end(), greater<int>());
f[u] = tmp[u][k - 1];
}
if (fa[u]) tmp[fa[u]].push_back(f[u] + 1), cnt[fa[u]]++;
}
long long res = n - s[k].size();
for (auto u : s[k]) up(u, f[u]);
return sum;
}
int main() {
cin >> n;
for (int i = (1); i <= (n - 1); i++) {
cin >> u >> v;
e[u].push_back(v);
e[v].push_back(u);
}
dfs(1);
for (int i = (n); i >= (1); i--) {
int u = id[i];
for (int j = (1); j <= (sn[u]); j++) s[j].push_back(u);
}
for (int i = (1); i <= (n); i++) g[i] = 1, sum++;
for (int i = (n); i >= (2); i--) ans += solve(i);
cout << ans << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 3e5 + 10, LGMAX = 19;
class Reader {
public:
Reader() : m_pos(kBufferSize - 1), m_buffer(new char[kBufferSize]) { next(); }
Reader& operator>>(int& value) {
value = 0;
while (current() < '0' || current() > '9') next();
while (current() >= '0' && current() <= '9') {
value = value * 10 + current() - '0';
next();
}
return *this;
}
private:
const int kBufferSize = 32768;
char current() { return m_buffer[m_pos]; }
void next() {
if (!(++m_pos != kBufferSize)) {
cin.read(m_buffer.get(), kBufferSize);
m_pos = 0;
}
}
int m_pos;
unique_ptr<char[]> m_buffer;
};
int N;
int MaxLevel;
int level[NMAX], height[NMAX];
int64_t answer;
int DP[LGMAX][NMAX], father[NMAX], val[NMAX];
vector<int> T[NMAX], LevelNodes[NMAX];
vector<pair<int, int>> GoUp[NMAX];
int DFS(int node, int from, int level) {
father[node] = from;
MaxLevel = max(MaxLevel, level);
LevelNodes[level].push_back(node);
if (from != -1 && T[node].size() == 1) {
++answer;
return 1;
}
int height = 1;
for (int next : T[node]) {
if (next == from) continue;
++DP[2][node];
height = max(height, 1 + DFS(next, node, level + 1));
}
GoUp[DP[2][node]].push_back({2, node});
answer += height;
return height;
}
void iterativeDFS(int node) {
stack<pair<int, int>> S;
father[node] = -1;
LevelNodes[0].push_back(node);
S.push({node, 0});
while (!S.empty()) {
int node, i;
tie(node, i) = S.top();
if (i == 0) {
height[node] = 1;
if (father[node] != -1 && T[node].size() == 1) {
++answer;
S.pop();
continue;
}
}
if (i < (int)T[node].size()) {
S.top().second = i + 1;
if (T[node][i] == father[node]) continue;
++DP[2][node];
father[T[node][i]] = node;
level[T[node][i]] = level[node] + 1;
MaxLevel = max(MaxLevel, level[node] + 1);
LevelNodes[level[node] + 1].push_back(T[node][i]);
S.push({T[node][i], 0});
} else if (i == (int)T[node].size()) {
S.pop();
GoUp[DP[2][node]].push_back({2, node});
for (int next : T[node]) {
if (next != father[node]) {
height[node] = max(height[node], 1 + height[next]);
}
}
answer += height[node];
}
}
}
int main() {
Reader cin;
cin >> N;
for (int i = 0; i < N - 1; ++i) {
int x, y;
cin >> x >> y;
T[x].push_back(y);
T[y].push_back(x);
}
iterativeDFS(1);
for (int i = 3; i < LGMAX; ++i) {
for (int j = MaxLevel; j >= 0; --j) {
for (int node : LevelNodes[j]) {
vector<int> values;
for (int next : T[node]) {
if (next == father[node]) continue;
values.push_back(DP[i - 1][next]);
}
sort(values.begin(), values.end(), greater<int>());
int k = 0;
while (k < (int)values.size() && k + 1 <= values[k]) ++k;
if (k > 0 && k <= values[k - 1]) {
DP[i][node] = k;
GoUp[DP[i][node]].push_back({i, node});
}
}
}
}
int64_t value = N;
for (int i = 1; i <= N; ++i) val[i] = 1;
for (int i = N; i >= 2; --i) {
for (auto det : GoUp[i]) {
int depth = det.first;
int node = det.second;
while (node != -1 && depth > val[node]) {
value += depth - val[node];
val[node] = depth;
node = father[node];
}
}
answer += value;
}
cout << answer << '\n';
return 0;
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 3e5 + 10, LGMAX = 19;
class Reader {
public:
Reader() : m_pos(kBufferSize - 1), m_buffer(new char[kBufferSize]) { next(); }
Reader& operator>>(int& value) {
value = 0;
while (current() < '0' || current() > '9') next();
while (current() >= '0' && current() <= '9') {
value = value * 10 + current() - '0';
next();
}
return *this;
}
private:
const int kBufferSize = 32768;
char current() { return m_buffer[m_pos]; }
void next() {
if (!(++m_pos != kBufferSize)) {
cin.read(m_buffer.get(), kBufferSize);
m_pos = 0;
}
}
int m_pos;
unique_ptr<char[]> m_buffer;
};
int N;
int MaxLevel;
int level[NMAX], height[NMAX];
int64_t answer;
int DP[LGMAX][NMAX], father[NMAX], val[NMAX];
vector<int> T[NMAX], LevelNodes[NMAX];
vector<pair<int, int>> GoUp[NMAX];
int DFS(int node, int from, int level) {
father[node] = from;
MaxLevel = max(MaxLevel, level);
LevelNodes[level].push_back(node);
if (from != -1 && T[node].size() == 1) {
++answer;
return 1;
}
int height = 1;
for (int next : T[node]) {
if (next == from) continue;
++DP[2][node];
height = max(height, 1 + DFS(next, node, level + 1));
}
GoUp[DP[2][node]].push_back({2, node});
answer += height;
return height;
}
void iterativeDFS(int node) {
stack<pair<int, int>> S;
father[node] = -1;
LevelNodes[0].push_back(node);
S.push({node, 0});
while (!S.empty()) {
int node, i;
tie(node, i) = S.top();
if (i == 0) {
height[node] = 1;
if (father[node] != -1 && T[node].size() == 1) {
++answer;
S.pop();
continue;
}
}
if (i < (int)T[node].size()) {
S.top().second = i + 1;
if (T[node][i] == father[node]) continue;
++DP[2][node];
father[T[node][i]] = node;
level[T[node][i]] = level[node] + 1;
MaxLevel = max(MaxLevel, level[node] + 1);
LevelNodes[level[node] + 1].push_back(T[node][i]);
S.push({T[node][i], 0});
} else if (i == (int)T[node].size()) {
S.pop();
GoUp[DP[2][node]].push_back({2, node});
for (int next : T[node]) {
if (next != father[node]) {
height[node] = max(height[node], 1 + height[next]);
}
}
answer += height[node];
}
}
}
int main() {
Reader cin;
cin >> N;
for (int i = 0; i < N - 1; ++i) {
int x, y;
cin >> x >> y;
T[x].push_back(y);
T[y].push_back(x);
}
iterativeDFS(1);
for (int i = 3; i < LGMAX; ++i) {
for (int j = MaxLevel; j >= 0; --j) {
for (int node : LevelNodes[j]) {
vector<int> values;
for (int next : T[node]) {
if (next == father[node]) continue;
values.push_back(DP[i - 1][next]);
}
sort(values.begin(), values.end(), greater<int>());
int k = 0;
while (k < (int)values.size() && k + 1 <= values[k]) ++k;
if (k > 0 && k <= values[k - 1]) {
DP[i][node] = k;
GoUp[DP[i][node]].push_back({i, node});
}
}
}
}
int64_t value = N;
for (int i = 1; i <= N; ++i) val[i] = 1;
for (int i = N; i >= 2; --i) {
for (auto det : GoUp[i]) {
int depth = det.first;
int node = det.second;
while (node != -1 && depth > val[node]) {
value += depth - val[node];
val[node] = depth;
node = father[node];
}
}
answer += value;
}
cout << answer << '\n';
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
const int mod = 1e9 + 7;
long long ans;
int n, d[N], son[N], size[N];
vector<int> G[N], dp[N];
bool cmp(int i, int j) { return d[i] > d[j]; }
void dfs(int x, int f) {
size[x] = 1;
if (find(G[x].begin(), G[x].end(), f) != G[x].end())
G[x].erase(find(G[x].begin(), G[x].end(), f));
d[x] = G[x].size();
dp[x].resize(d[x] + 1);
for (int u : G[x]) {
dfs(u, x);
size[x] += size[u];
if (size[u] >= size[son[x]]) son[x] = u;
}
sort(G[x].begin(), G[x].end(), cmp);
for (int i = 1; i <= d[x]; ++i) {
multiset<int> S;
dp[x][i] = 2;
for (int u : G[x]) {
if (d[u] < i) break;
S.insert(dp[u][i]);
}
while (S.size() > i) S.erase(S.begin());
if (S.size() == i) dp[x][i] = (*S.begin()) + 1;
}
}
int f[N];
long long sum;
void clear(int x) {
for (int i = 1; i <= d[x]; ++i) f[i] = 1;
for (int u : G[x]) clear(u);
}
void insert(int x) {
for (int i = 1; i <= d[x]; ++i) {
if (dp[x][i] > f[i]) {
sum += dp[x][i] - f[i];
f[i] = dp[x][i];
}
}
for (int u : G[x]) insert(u);
}
void dfs(int x) {
for (int u : G[x]) {
if (u == son[x]) continue;
dfs(u);
sum = n;
clear(u);
}
if (son[x]) dfs(son[x]);
for (int u : G[x])
if (u != son[x]) insert(u);
for (int i = 1; i <= d[x]; ++i) {
if (dp[x][i] > f[i]) {
sum += dp[x][i] - f[i];
f[i] = dp[x][i];
}
}
ans += sum;
}
int main() {
cin >> n;
for (int i = 1; i < n; ++i) {
int x, y;
scanf("%d %d", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
}
dfs(1, 0);
for (int i = 1; i <= n; ++i) f[i] = 1;
sum = n;
dfs(1);
cout << ans;
}
| ### Prompt
In cpp, your task is to solve the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
const int mod = 1e9 + 7;
long long ans;
int n, d[N], son[N], size[N];
vector<int> G[N], dp[N];
bool cmp(int i, int j) { return d[i] > d[j]; }
void dfs(int x, int f) {
size[x] = 1;
if (find(G[x].begin(), G[x].end(), f) != G[x].end())
G[x].erase(find(G[x].begin(), G[x].end(), f));
d[x] = G[x].size();
dp[x].resize(d[x] + 1);
for (int u : G[x]) {
dfs(u, x);
size[x] += size[u];
if (size[u] >= size[son[x]]) son[x] = u;
}
sort(G[x].begin(), G[x].end(), cmp);
for (int i = 1; i <= d[x]; ++i) {
multiset<int> S;
dp[x][i] = 2;
for (int u : G[x]) {
if (d[u] < i) break;
S.insert(dp[u][i]);
}
while (S.size() > i) S.erase(S.begin());
if (S.size() == i) dp[x][i] = (*S.begin()) + 1;
}
}
int f[N];
long long sum;
void clear(int x) {
for (int i = 1; i <= d[x]; ++i) f[i] = 1;
for (int u : G[x]) clear(u);
}
void insert(int x) {
for (int i = 1; i <= d[x]; ++i) {
if (dp[x][i] > f[i]) {
sum += dp[x][i] - f[i];
f[i] = dp[x][i];
}
}
for (int u : G[x]) insert(u);
}
void dfs(int x) {
for (int u : G[x]) {
if (u == son[x]) continue;
dfs(u);
sum = n;
clear(u);
}
if (son[x]) dfs(son[x]);
for (int u : G[x])
if (u != son[x]) insert(u);
for (int i = 1; i <= d[x]; ++i) {
if (dp[x][i] > f[i]) {
sum += dp[x][i] - f[i];
f[i] = dp[x][i];
}
}
ans += sum;
}
int main() {
cin >> n;
for (int i = 1; i < n; ++i) {
int x, y;
scanf("%d %d", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
}
dfs(1, 0);
for (int i = 1; i <= n; ++i) f[i] = 1;
sum = n;
dfs(1);
cout << ans;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int m, k;
long long ans;
const int N = 330000;
int h[N], dp[N], mdp[N], cnt[N], par[N], chd[N];
vector<int> adj[N];
vector<int> V;
void dfs(int u, int p) {
V.push_back(u);
par[u] = p;
for (int v : adj[u]) {
if (v == p) continue;
dfs(v, u);
}
chd[u] = adj[u].size() - (u != 1);
for (int v : adj[u]) {
if (v == p) continue;
h[u] = max(h[u], h[v] + 1);
chd[u] = max(chd[u], chd[v]);
}
ans += h[u];
}
int from[N], cc[N];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
m = 0;
ans = 1LL * n * n;
m = 1;
dfs(1, 0);
reverse(V.begin(), V.end());
int sum = 0;
int _m = 1;
for (m = 2; 1LL * m * (m + 1) + 1 <= n; m++) {
long long s = 1, t = 1;
k = 0;
while (s <= n) t *= m, s += t, k++;
for (int i = 1; i <= n; i++) mdp[i] = 0;
sum += k;
if (k >= 4) {
for (int u : V) {
for (int v : adj[u]) {
if (v == par[u]) continue;
mdp[u] = max(mdp[u], mdp[v]);
}
int c = adj[u].size() - (u != 1);
dp[u] = 0;
if (mdp[u] < k - 1) {
if (c >= m) {
for (int j = 0; j < k; j++) cnt[j] = 0;
for (int v : adj[u]) {
if (v == par[u]) continue;
cnt[dp[v]]++;
}
for (int j = k - 2; j >= 0; j--) {
cnt[j] += cnt[j + 1];
if (cnt[j] >= m) {
dp[u] = j + 1;
break;
}
}
mdp[u] = max(mdp[u], dp[u]);
}
}
ans += mdp[u];
}
_m = m;
} else {
for (int u : V) {
cc[u] = adj[u].size() - (u != 1);
from[u] = 0;
int cn = 0;
for (int v : adj[u]) {
if (v == par[u]) continue;
cnt[cn++] = cc[v];
from[u] = max(from[u], from[v]);
}
sort(cnt, cnt + cn);
int t = 0;
for (int j = cn - 1; j >= 0; j--) {
if (cnt[j] < cn - j) break;
t = max(t, cn - j);
}
from[u] = max(from[u], t);
if (from[u] >= m) ans += from[u] - m + 1;
}
break;
}
}
for (int i = 0; i <= n; i++) cnt[i] = 0;
for (int i = 1; i <= n; i++) cnt[chd[i]]++;
for (int i = n - 1; i >= 0; i--) cnt[i] += cnt[i + 1];
for (m = _m + 1; m <= n; m++) ans += cnt[m];
cout << ans << endl;
return 0;
}
| ### Prompt
Please formulate a CPP solution to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int m, k;
long long ans;
const int N = 330000;
int h[N], dp[N], mdp[N], cnt[N], par[N], chd[N];
vector<int> adj[N];
vector<int> V;
void dfs(int u, int p) {
V.push_back(u);
par[u] = p;
for (int v : adj[u]) {
if (v == p) continue;
dfs(v, u);
}
chd[u] = adj[u].size() - (u != 1);
for (int v : adj[u]) {
if (v == p) continue;
h[u] = max(h[u], h[v] + 1);
chd[u] = max(chd[u], chd[v]);
}
ans += h[u];
}
int from[N], cc[N];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
m = 0;
ans = 1LL * n * n;
m = 1;
dfs(1, 0);
reverse(V.begin(), V.end());
int sum = 0;
int _m = 1;
for (m = 2; 1LL * m * (m + 1) + 1 <= n; m++) {
long long s = 1, t = 1;
k = 0;
while (s <= n) t *= m, s += t, k++;
for (int i = 1; i <= n; i++) mdp[i] = 0;
sum += k;
if (k >= 4) {
for (int u : V) {
for (int v : adj[u]) {
if (v == par[u]) continue;
mdp[u] = max(mdp[u], mdp[v]);
}
int c = adj[u].size() - (u != 1);
dp[u] = 0;
if (mdp[u] < k - 1) {
if (c >= m) {
for (int j = 0; j < k; j++) cnt[j] = 0;
for (int v : adj[u]) {
if (v == par[u]) continue;
cnt[dp[v]]++;
}
for (int j = k - 2; j >= 0; j--) {
cnt[j] += cnt[j + 1];
if (cnt[j] >= m) {
dp[u] = j + 1;
break;
}
}
mdp[u] = max(mdp[u], dp[u]);
}
}
ans += mdp[u];
}
_m = m;
} else {
for (int u : V) {
cc[u] = adj[u].size() - (u != 1);
from[u] = 0;
int cn = 0;
for (int v : adj[u]) {
if (v == par[u]) continue;
cnt[cn++] = cc[v];
from[u] = max(from[u], from[v]);
}
sort(cnt, cnt + cn);
int t = 0;
for (int j = cn - 1; j >= 0; j--) {
if (cnt[j] < cn - j) break;
t = max(t, cn - j);
}
from[u] = max(from[u], t);
if (from[u] >= m) ans += from[u] - m + 1;
}
break;
}
}
for (int i = 0; i <= n; i++) cnt[i] = 0;
for (int i = 1; i <= n; i++) cnt[chd[i]]++;
for (int i = n - 1; i >= 0; i--) cnt[i] += cnt[i + 1];
for (m = _m + 1; m <= n; m++) ans += cnt[m];
cout << ans << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
int n;
vector<int> adj[N];
vector<int> karr[N];
multiset<int> ss[N];
vector<int> *val[N];
long long *sum[N];
long long ans = 0;
void dfs(int v, int p) {
int c = adj[v].size() - (p > 0);
for (auto it : adj[v]) {
if (it == p) continue;
dfs(it, v);
}
val[v] = new vector<int>(c, 0);
sum[v] = new long long(0);
for (auto it : adj[v]) {
if (it == p) continue;
for (int i = 0; i < (int)karr[it].size() && i < c; i++) {
ss[i].insert(karr[it][i]);
if ((int)ss[i].size() > i + 1) {
ss[i].erase(ss[i].begin());
}
}
if (val[v]->size() < val[it]->size()) {
swap(val[v], val[it]);
swap(sum[v], sum[it]);
}
for (int i = 0; i < (int)val[it]->size(); i++) {
if ((*val[it])[i] > (*val[v])[i]) {
(*sum[v]) += (*val[it])[i] - (*val[v])[i];
(*val[v])[i] = (*val[it])[i];
}
}
}
for (int i = 1; i <= c; i++) {
int cnt = 0;
if ((int)ss[i - 1].size() == i) {
cnt = 1 + (*ss[i - 1].begin());
} else
cnt = 2;
karr[v].push_back(cnt);
ss[i - 1].clear();
if (cnt > (*val[v])[i - 1]) {
(*sum[v]) += cnt - (*val[v])[i - 1];
(*val[v])[i - 1] = cnt;
}
}
ans += (*sum[v]);
ans += n - val[v]->size();
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1, 0);
cout << ans << "\n";
return 0;
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
int n;
vector<int> adj[N];
vector<int> karr[N];
multiset<int> ss[N];
vector<int> *val[N];
long long *sum[N];
long long ans = 0;
void dfs(int v, int p) {
int c = adj[v].size() - (p > 0);
for (auto it : adj[v]) {
if (it == p) continue;
dfs(it, v);
}
val[v] = new vector<int>(c, 0);
sum[v] = new long long(0);
for (auto it : adj[v]) {
if (it == p) continue;
for (int i = 0; i < (int)karr[it].size() && i < c; i++) {
ss[i].insert(karr[it][i]);
if ((int)ss[i].size() > i + 1) {
ss[i].erase(ss[i].begin());
}
}
if (val[v]->size() < val[it]->size()) {
swap(val[v], val[it]);
swap(sum[v], sum[it]);
}
for (int i = 0; i < (int)val[it]->size(); i++) {
if ((*val[it])[i] > (*val[v])[i]) {
(*sum[v]) += (*val[it])[i] - (*val[v])[i];
(*val[v])[i] = (*val[it])[i];
}
}
}
for (int i = 1; i <= c; i++) {
int cnt = 0;
if ((int)ss[i - 1].size() == i) {
cnt = 1 + (*ss[i - 1].begin());
} else
cnt = 2;
karr[v].push_back(cnt);
ss[i - 1].clear();
if (cnt > (*val[v])[i - 1]) {
(*sum[v]) += cnt - (*val[v])[i - 1];
(*val[v])[i - 1] = cnt;
}
}
ans += (*sum[v]);
ans += n - val[v]->size();
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1, 0);
cout << ans << "\n";
return 0;
}
``` |
#include <bits/stdc++.h>
inline bool C(int a, int b) { return a > b; }
inline void inc(int &a, const int &b) {
if (a < b) a = b;
}
struct d {
d *n;
int t;
d(d *a, int b) : n(a), t(b) {}
} * G[300005];
void I(int f, int t) { G[f] = new d(G[f], t); }
int A[300005], M[300005][20], F[300005][20], V[300005], n;
long long T;
void s(int x, int f) {
V[x] = 1;
M[x][1] = F[x][1] = n;
for (d *i = G[x]; i; i = i->n)
if (i->t != f) s(i->t, x), inc(V[x], V[i->t] + 1);
for (int i = 2, m; m = 0, i < 19; ++i) {
for (d *j = G[x]; j; j = j->n)
if (j->t != f) A[m++] = F[j->t][i - 1];
std::sort(A, A + m, C);
A[m] = 0;
while (F[x][i] < A[F[x][i]]) ++F[x][i];
inc(F[x][i], 1);
M[x][i] = F[x][i];
for (d *j = G[x]; j; j = j->n)
if (j->t != f) inc(M[x][i], M[j->t][i]);
}
M[x][19] = F[x][19] = 1;
T += V[x];
for (int i = 1; i < 19; ++i) T += i * (M[x][i] - M[x][i + 1]);
}
int main() {
scanf("%d", &n);
for (int i = 1, a, b; i < n; ++i) {
scanf("%d%d", &a, &b);
I(a, b);
I(b, a);
}
s(1, 0);
return !printf("%lld\n", T);
}
| ### Prompt
Generate a Cpp solution to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
inline bool C(int a, int b) { return a > b; }
inline void inc(int &a, const int &b) {
if (a < b) a = b;
}
struct d {
d *n;
int t;
d(d *a, int b) : n(a), t(b) {}
} * G[300005];
void I(int f, int t) { G[f] = new d(G[f], t); }
int A[300005], M[300005][20], F[300005][20], V[300005], n;
long long T;
void s(int x, int f) {
V[x] = 1;
M[x][1] = F[x][1] = n;
for (d *i = G[x]; i; i = i->n)
if (i->t != f) s(i->t, x), inc(V[x], V[i->t] + 1);
for (int i = 2, m; m = 0, i < 19; ++i) {
for (d *j = G[x]; j; j = j->n)
if (j->t != f) A[m++] = F[j->t][i - 1];
std::sort(A, A + m, C);
A[m] = 0;
while (F[x][i] < A[F[x][i]]) ++F[x][i];
inc(F[x][i], 1);
M[x][i] = F[x][i];
for (d *j = G[x]; j; j = j->n)
if (j->t != f) inc(M[x][i], M[j->t][i]);
}
M[x][19] = F[x][19] = 1;
T += V[x];
for (int i = 1; i < 19; ++i) T += i * (M[x][i] - M[x][i + 1]);
}
int main() {
scanf("%d", &n);
for (int i = 1, a, b; i < n; ++i) {
scanf("%d%d", &a, &b);
I(a, b);
I(b, a);
}
s(1, 0);
return !printf("%lld\n", T);
}
``` |
#include <bits/stdc++.h>
using namespace std;
struct EDGE {
int to, next;
} e[600010];
int n, head[300010], top;
void add(int u, int v) {
e[top].to = v;
e[top].next = head[u];
head[u] = top++;
}
int dp[300010][20], fa[300010], dep[300010];
void dfs(int x) {
dp[x][1] = n;
int i, j;
for (i = head[x]; ~i; i = e[i].next) {
if (e[i].to == fa[x]) continue;
fa[e[i].to] = x;
dfs(e[i].to);
dep[x] = max(dep[x], dep[e[i].to]);
}
dep[x]++;
vector<int> v;
for (j = 2; j < 20; j++) {
v.push_back(-1);
for (i = head[x]; ~i; i = e[i].next) {
if (e[i].to == fa[x]) continue;
v.push_back(dp[e[i].to][j - 1]);
}
sort(v.begin() + 1, v.end(), greater<int>());
for (i = 1; i < v.size(); i++) {
if (v[i] >= i)
dp[x][j] = i;
else
break;
}
v.clear();
}
}
void dfs1(int x) {
int i, j;
for (i = head[x]; ~i; i = e[i].next) {
if (e[i].to == fa[x]) continue;
dfs1(e[i].to);
for (j = 1; j < 20; j++) dp[x][j] = max(dp[x][j], dp[e[i].to][j]);
}
}
int main() {
memset(head, 255, sizeof(head));
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n;
int i, u, v;
for (i = 1; i < n; i++) cin >> u >> v, add(u, v), add(v, u);
dfs(1);
dfs1(1);
long long ans = 0;
int j;
for (i = 1; i <= n; i++) {
for (j = 1; j < 20; j++) {
ans += max(dp[i][j] - 1, 0);
}
ans += dep[i];
}
cout << ans << '\n';
return 0;
}
| ### Prompt
In cpp, your task is to solve the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
struct EDGE {
int to, next;
} e[600010];
int n, head[300010], top;
void add(int u, int v) {
e[top].to = v;
e[top].next = head[u];
head[u] = top++;
}
int dp[300010][20], fa[300010], dep[300010];
void dfs(int x) {
dp[x][1] = n;
int i, j;
for (i = head[x]; ~i; i = e[i].next) {
if (e[i].to == fa[x]) continue;
fa[e[i].to] = x;
dfs(e[i].to);
dep[x] = max(dep[x], dep[e[i].to]);
}
dep[x]++;
vector<int> v;
for (j = 2; j < 20; j++) {
v.push_back(-1);
for (i = head[x]; ~i; i = e[i].next) {
if (e[i].to == fa[x]) continue;
v.push_back(dp[e[i].to][j - 1]);
}
sort(v.begin() + 1, v.end(), greater<int>());
for (i = 1; i < v.size(); i++) {
if (v[i] >= i)
dp[x][j] = i;
else
break;
}
v.clear();
}
}
void dfs1(int x) {
int i, j;
for (i = head[x]; ~i; i = e[i].next) {
if (e[i].to == fa[x]) continue;
dfs1(e[i].to);
for (j = 1; j < 20; j++) dp[x][j] = max(dp[x][j], dp[e[i].to][j]);
}
}
int main() {
memset(head, 255, sizeof(head));
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n;
int i, u, v;
for (i = 1; i < n; i++) cin >> u >> v, add(u, v), add(v, u);
dfs(1);
dfs1(1);
long long ans = 0;
int j;
for (i = 1; i <= n; i++) {
for (j = 1; j < 20; j++) {
ans += max(dp[i][j] - 1, 0);
}
ans += dep[i];
}
cout << ans << '\n';
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int mx[300005], f[300005][20], n;
long long ans;
vector<int> e[300005];
int main() {
scanf("%d", &n);
for (int i = 1, u, v; i < n; i++)
scanf("%d%d", &u, &v), e[u].emplace_back(v), e[v].emplace_back(u);
function<void(int, int)> dfs1 = [&](int u, int fa) {
mx[u] = 1;
for (int v : e[u])
if (v ^ fa) dfs1(v, u), mx[u] = max(mx[u], mx[v] + 1);
ans += mx[u];
static int tmp[300005];
f[u][0] = n;
for (int i = 1; i < 20; i++) {
int top = 0;
for (int v : e[u])
if (v ^ fa) tmp[++top] = f[v][i - 1];
sort(tmp + 1, tmp + 1 + top, [](int _, int $) { return _ > $; });
for (f[u][i] = 1; f[u][i] < top && f[u][i] < tmp[f[u][i] + 1]; f[u][i]++)
;
}
};
dfs1(1, 0);
function<void(int, int)> dfs2 = [&](int u, int fa) {
for (int v : e[u])
if (v ^ fa) dfs2(v, u);
for (int i = 1; i < 20; i++)
for (int v : e[u])
if (v ^ fa) f[u][i] = max(f[u][i], f[v][i]);
for (int i = 1; i < 20; i++) ans += 1ll * i * (f[u][i - 1] - f[u][i]);
};
dfs2(1, 0);
printf("%lld\n", ans);
}
| ### Prompt
Generate a cpp solution to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int mx[300005], f[300005][20], n;
long long ans;
vector<int> e[300005];
int main() {
scanf("%d", &n);
for (int i = 1, u, v; i < n; i++)
scanf("%d%d", &u, &v), e[u].emplace_back(v), e[v].emplace_back(u);
function<void(int, int)> dfs1 = [&](int u, int fa) {
mx[u] = 1;
for (int v : e[u])
if (v ^ fa) dfs1(v, u), mx[u] = max(mx[u], mx[v] + 1);
ans += mx[u];
static int tmp[300005];
f[u][0] = n;
for (int i = 1; i < 20; i++) {
int top = 0;
for (int v : e[u])
if (v ^ fa) tmp[++top] = f[v][i - 1];
sort(tmp + 1, tmp + 1 + top, [](int _, int $) { return _ > $; });
for (f[u][i] = 1; f[u][i] < top && f[u][i] < tmp[f[u][i] + 1]; f[u][i]++)
;
}
};
dfs1(1, 0);
function<void(int, int)> dfs2 = [&](int u, int fa) {
for (int v : e[u])
if (v ^ fa) dfs2(v, u);
for (int i = 1; i < 20; i++)
for (int v : e[u])
if (v ^ fa) f[u][i] = max(f[u][i], f[v][i]);
for (int i = 1; i < 20; i++) ans += 1ll * i * (f[u][i - 1] - f[u][i]);
};
dfs2(1, 0);
printf("%lld\n", ans);
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 300000;
const int MAXDEP = 18;
int n;
vector<int> adj[MAXN];
int par[MAXN];
int top[MAXN], ntop;
void dfsinit(int at) {
top[ntop++] = at;
for (int i = (0); i < (((int)(adj[at]).size())); ++i) {
int to = adj[at][i];
if (to == par[at]) continue;
par[to] = at;
dfsinit(to);
}
}
int jmx[MAXN];
int kmx[MAXN][MAXDEP + 1];
void dfsjmx(int at) {
jmx[at] = 1;
for (int i = (0); i < (((int)(adj[at]).size())); ++i) {
int to = adj[at][i];
if (to == par[at]) continue;
dfsjmx(to);
jmx[at] = max(jmx[at], jmx[to] + 1);
}
}
int cur[MAXN], ncur;
void dfskmx(int at) {
for (int i = (0); i < (((int)(adj[at]).size())); ++i) {
int to = adj[at][i];
if (to == par[at]) continue;
dfskmx(to);
}
kmx[at][1] = n;
for (int j = (2); j <= (MAXDEP); ++j) {
ncur = 0;
for (int i = (0); i < (((int)(adj[at]).size())); ++i) {
int to = adj[at][i];
if (to != par[at]) cur[ncur++] = kmx[to][j - 1];
}
sort(cur, cur + ncur);
reverse(cur, cur + ncur);
kmx[at][j] = 0;
for (int pos = (0); pos < (ncur); ++pos)
if (cur[pos] >= pos + 1) kmx[at][j] = pos + 1;
}
}
int dp[MAXN];
long long dpsum;
vector<pair<int, int> > e[MAXN + 1];
void upddp(int at, int val) {
while (at != -1 && val > dp[at]) {
dpsum += val - dp[at];
dp[at] = val;
at = par[at];
}
}
void run() {
scanf("%d", &n);
for (int i = (0); i < (n - 1); ++i) {
int a, b;
scanf("%d%d", &a, &b);
--a, --b;
adj[a].push_back(b);
adj[b].push_back(a);
}
par[0] = -1;
ntop = 0;
dfsinit(0);
dfsjmx(0);
dfskmx(0);
long long ans = 0;
for (int i = (0); i < (n); ++i) dp[i] = 0;
dpsum = 0;
for (int i = (0); i < (n); ++i)
for (int j = (1); j <= (MAXDEP); ++j)
if (kmx[i][j] >= 2) e[kmx[i][j]].push_back(make_pair(i, j));
for (int k = n; k >= 2; --k) {
for (int idx = (0); idx < (((int)(e[k]).size())); ++idx) {
int i = e[k][idx].first, j = e[k][idx].second;
upddp(i, j);
}
ans += dpsum;
}
for (int i = (0); i < (n); ++i) dp[i] = jmx[i];
for (int i = n - 1; i >= 0; --i) {
int at = top[i];
ans += dp[at];
if (par[at] != -1) dp[par[at]] = max(dp[par[at]], dp[at]);
}
printf("%lld\n", ans);
}
int main() {
run();
return 0;
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 300000;
const int MAXDEP = 18;
int n;
vector<int> adj[MAXN];
int par[MAXN];
int top[MAXN], ntop;
void dfsinit(int at) {
top[ntop++] = at;
for (int i = (0); i < (((int)(adj[at]).size())); ++i) {
int to = adj[at][i];
if (to == par[at]) continue;
par[to] = at;
dfsinit(to);
}
}
int jmx[MAXN];
int kmx[MAXN][MAXDEP + 1];
void dfsjmx(int at) {
jmx[at] = 1;
for (int i = (0); i < (((int)(adj[at]).size())); ++i) {
int to = adj[at][i];
if (to == par[at]) continue;
dfsjmx(to);
jmx[at] = max(jmx[at], jmx[to] + 1);
}
}
int cur[MAXN], ncur;
void dfskmx(int at) {
for (int i = (0); i < (((int)(adj[at]).size())); ++i) {
int to = adj[at][i];
if (to == par[at]) continue;
dfskmx(to);
}
kmx[at][1] = n;
for (int j = (2); j <= (MAXDEP); ++j) {
ncur = 0;
for (int i = (0); i < (((int)(adj[at]).size())); ++i) {
int to = adj[at][i];
if (to != par[at]) cur[ncur++] = kmx[to][j - 1];
}
sort(cur, cur + ncur);
reverse(cur, cur + ncur);
kmx[at][j] = 0;
for (int pos = (0); pos < (ncur); ++pos)
if (cur[pos] >= pos + 1) kmx[at][j] = pos + 1;
}
}
int dp[MAXN];
long long dpsum;
vector<pair<int, int> > e[MAXN + 1];
void upddp(int at, int val) {
while (at != -1 && val > dp[at]) {
dpsum += val - dp[at];
dp[at] = val;
at = par[at];
}
}
void run() {
scanf("%d", &n);
for (int i = (0); i < (n - 1); ++i) {
int a, b;
scanf("%d%d", &a, &b);
--a, --b;
adj[a].push_back(b);
adj[b].push_back(a);
}
par[0] = -1;
ntop = 0;
dfsinit(0);
dfsjmx(0);
dfskmx(0);
long long ans = 0;
for (int i = (0); i < (n); ++i) dp[i] = 0;
dpsum = 0;
for (int i = (0); i < (n); ++i)
for (int j = (1); j <= (MAXDEP); ++j)
if (kmx[i][j] >= 2) e[kmx[i][j]].push_back(make_pair(i, j));
for (int k = n; k >= 2; --k) {
for (int idx = (0); idx < (((int)(e[k]).size())); ++idx) {
int i = e[k][idx].first, j = e[k][idx].second;
upddp(i, j);
}
ans += dpsum;
}
for (int i = (0); i < (n); ++i) dp[i] = jmx[i];
for (int i = n - 1; i >= 0; --i) {
int at = top[i];
ans += dp[at];
if (par[at] != -1) dp[par[at]] = max(dp[par[at]], dp[at]);
}
printf("%lld\n", ans);
}
int main() {
run();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
struct edge {
int to, next;
} e[300005 * 2];
int head[300005], tot;
void add(int x, int y) {
e[++tot] = (edge){y, head[x]};
head[x] = tot;
}
int mx[300005], dp[300005][25];
long long ans;
int n, q[300005];
bool cmp(int x, int y) { return x > y; }
void dfs(int x, int fa) {
for (int i = head[x]; i; i = e[i].next)
if (e[i].to != fa) {
dfs(e[i].to, x);
mx[x] = max(mx[x], mx[e[i].to]);
}
++mx[x];
ans += mx[x];
dp[x][1] = n;
for (int p = 2; p <= 20; p++) {
int tp = 0;
for (int i = head[x]; i; i = e[i].next)
if (e[i].to != fa) q[++tp] = dp[e[i].to][p - 1];
sort(q + 1, q + tp + 1, cmp);
for (int j = tp; j >= 2; j--)
if (q[j] >= j) {
dp[x][p] = j;
break;
}
}
}
void solve(int x, int fa) {
for (int i = head[x]; i; i = e[i].next)
if (e[i].to != fa) {
solve(e[i].to, x);
for (int j = 1; j <= 20; j++) dp[x][j] = max(dp[x][j], dp[e[i].to][j]);
}
for (int i = 1; i <= 20; i++) {
ans += 1ll * (dp[x][i] - dp[x][i + 1]) * i;
if (dp[x][i + 1] == 0) {
ans -= i;
break;
}
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
add(x, y);
add(y, x);
}
dfs(1, 0);
solve(1, 0);
printf("%lld", ans);
}
| ### Prompt
Generate a Cpp solution to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
struct edge {
int to, next;
} e[300005 * 2];
int head[300005], tot;
void add(int x, int y) {
e[++tot] = (edge){y, head[x]};
head[x] = tot;
}
int mx[300005], dp[300005][25];
long long ans;
int n, q[300005];
bool cmp(int x, int y) { return x > y; }
void dfs(int x, int fa) {
for (int i = head[x]; i; i = e[i].next)
if (e[i].to != fa) {
dfs(e[i].to, x);
mx[x] = max(mx[x], mx[e[i].to]);
}
++mx[x];
ans += mx[x];
dp[x][1] = n;
for (int p = 2; p <= 20; p++) {
int tp = 0;
for (int i = head[x]; i; i = e[i].next)
if (e[i].to != fa) q[++tp] = dp[e[i].to][p - 1];
sort(q + 1, q + tp + 1, cmp);
for (int j = tp; j >= 2; j--)
if (q[j] >= j) {
dp[x][p] = j;
break;
}
}
}
void solve(int x, int fa) {
for (int i = head[x]; i; i = e[i].next)
if (e[i].to != fa) {
solve(e[i].to, x);
for (int j = 1; j <= 20; j++) dp[x][j] = max(dp[x][j], dp[e[i].to][j]);
}
for (int i = 1; i <= 20; i++) {
ans += 1ll * (dp[x][i] - dp[x][i + 1]) * i;
if (dp[x][i + 1] == 0) {
ans -= i;
break;
}
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
add(x, y);
add(y, x);
}
dfs(1, 0);
solve(1, 0);
printf("%lld", ans);
}
``` |
#include <bits/stdc++.h>
std::vector<int> node[524288];
int mk[524288][32], km[524288][32];
int dpmk[524288][32], dpkm[524288][32];
long long int ans;
int a[524288];
int na;
int n, k, m;
void dfs(int p, int u) {
0;
for (auto v : node[u])
if (v != p) dfs(u, v);
for (int i = 1; i < k; ++i) {
na = 0;
for (auto v : node[u])
if (v != p) a[na++] = mk[v][i];
if (na >= i) {
std::nth_element(a, a + na - i, a + na);
dpmk[u][i] = mk[u][i] = a[na - i] + 1;
} else
dpmk[u][i] = mk[u][i] = 1;
for (auto v : node[u])
if (v != p) dpmk[u][i] = std::max(dpmk[u][i], dpmk[v][i]);
ans += dpmk[u][i];
0;
}
dpkm[u][1] = km[u][1] = n;
ans += n - (k - 1);
for (int i = 2; i < m; ++i) {
na = 0;
for (auto v : node[u])
if (v != p) a[na++] = km[v][i - 1];
std::sort(a, a + na);
km[u][i] = 0;
for (int j = 1; j <= na; ++j)
if (a[na - j] >= j) km[u][i] = j;
dpkm[u][i] = km[u][i];
for (auto v : node[u])
if (v != p) dpkm[u][i] = std::max(dpkm[u][i], dpkm[v][i]);
ans += std::max(0, dpkm[u][i] - (k - 1));
0;
}
}
int main() {
scanf("%d", &n), k = std::min(n + 1, 32), m = std::min(n + 1, 32);
for (int i = 1; i < n; ++i) {
int u, v;
scanf("%d%d", &u, &v);
node[u].push_back(v);
node[v].push_back(u);
}
dfs(1, 1);
printf("%lld\n", ans);
return 0;
}
| ### Prompt
Create a solution in Cpp for the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
std::vector<int> node[524288];
int mk[524288][32], km[524288][32];
int dpmk[524288][32], dpkm[524288][32];
long long int ans;
int a[524288];
int na;
int n, k, m;
void dfs(int p, int u) {
0;
for (auto v : node[u])
if (v != p) dfs(u, v);
for (int i = 1; i < k; ++i) {
na = 0;
for (auto v : node[u])
if (v != p) a[na++] = mk[v][i];
if (na >= i) {
std::nth_element(a, a + na - i, a + na);
dpmk[u][i] = mk[u][i] = a[na - i] + 1;
} else
dpmk[u][i] = mk[u][i] = 1;
for (auto v : node[u])
if (v != p) dpmk[u][i] = std::max(dpmk[u][i], dpmk[v][i]);
ans += dpmk[u][i];
0;
}
dpkm[u][1] = km[u][1] = n;
ans += n - (k - 1);
for (int i = 2; i < m; ++i) {
na = 0;
for (auto v : node[u])
if (v != p) a[na++] = km[v][i - 1];
std::sort(a, a + na);
km[u][i] = 0;
for (int j = 1; j <= na; ++j)
if (a[na - j] >= j) km[u][i] = j;
dpkm[u][i] = km[u][i];
for (auto v : node[u])
if (v != p) dpkm[u][i] = std::max(dpkm[u][i], dpkm[v][i]);
ans += std::max(0, dpkm[u][i] - (k - 1));
0;
}
}
int main() {
scanf("%d", &n), k = std::min(n + 1, 32), m = std::min(n + 1, 32);
for (int i = 1; i < n; ++i) {
int u, v;
scanf("%d%d", &u, &v);
node[u].push_back(v);
node[v].push_back(u);
}
dfs(1, 1);
printf("%lld\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<int> g[300010], ch[300010];
vector<pair<int, int> > up[300010];
int p[300010], dp[300010][30], val[300010];
int u, v, n;
long long ans;
void dfs1(int u, int f) {
p[u] = f;
for (int v : g[u])
if (v != f) {
ch[u].emplace_back(v);
dfs1(v, u);
}
}
void dfs2(int u) {
for (int v : ch[u]) dfs2(v);
auto &_dp = dp[u];
_dp[1] = n;
for (int k = 2; k < 30; k++) {
vector<int> hp;
for (int v : ch[u])
if (dp[v][k - 1]) hp.emplace_back(dp[v][k - 1]);
sort(hp.begin(), hp.end(), greater<int>());
for (int s = hp.size() - 1; s > 0; s--)
if (hp[s] >= s + 1) {
_dp[k] = s + 1;
break;
}
if (_dp[k]) up[_dp[k]].emplace_back(u, k);
}
}
int dfs3(int u) {
if (!ch[u].size()) {
ans++;
return 1;
}
int mx = 0;
for (int v : ch[u]) mx = max(mx, dfs3(v));
ans += mx + 1;
return mx + 1;
}
int main() {
cin >> n;
for (int i = 1; i < n; i++) {
cin >> u >> v;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
dfs1(1, 0);
fill_n(&dp[0][0], 300010 * 30, 0);
dfs2(1);
dfs3(1);
long long add = n;
fill_n(val, 300010, 1);
for (int k = n; k > 1; k--) {
for (auto e : up[k]) {
int u = e.first, v = e.second;
while (u && val[u] < v) {
add += v - val[u];
val[u] = v;
u = p[u];
}
}
ans += add;
}
cout << ans << endl;
return 0;
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> g[300010], ch[300010];
vector<pair<int, int> > up[300010];
int p[300010], dp[300010][30], val[300010];
int u, v, n;
long long ans;
void dfs1(int u, int f) {
p[u] = f;
for (int v : g[u])
if (v != f) {
ch[u].emplace_back(v);
dfs1(v, u);
}
}
void dfs2(int u) {
for (int v : ch[u]) dfs2(v);
auto &_dp = dp[u];
_dp[1] = n;
for (int k = 2; k < 30; k++) {
vector<int> hp;
for (int v : ch[u])
if (dp[v][k - 1]) hp.emplace_back(dp[v][k - 1]);
sort(hp.begin(), hp.end(), greater<int>());
for (int s = hp.size() - 1; s > 0; s--)
if (hp[s] >= s + 1) {
_dp[k] = s + 1;
break;
}
if (_dp[k]) up[_dp[k]].emplace_back(u, k);
}
}
int dfs3(int u) {
if (!ch[u].size()) {
ans++;
return 1;
}
int mx = 0;
for (int v : ch[u]) mx = max(mx, dfs3(v));
ans += mx + 1;
return mx + 1;
}
int main() {
cin >> n;
for (int i = 1; i < n; i++) {
cin >> u >> v;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
dfs1(1, 0);
fill_n(&dp[0][0], 300010 * 30, 0);
dfs2(1);
dfs3(1);
long long add = n;
fill_n(val, 300010, 1);
for (int k = n; k > 1; k--) {
for (auto e : up[k]) {
int u = e.first, v = e.second;
while (u && val[u] < v) {
add += v - val[u];
val[u] = v;
u = p[u];
}
}
ans += add;
}
cout << ans << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 5;
const int lg = 21;
int n, dp[maxn][lg], k, mx[maxn], d[lg];
long long s;
vector<int> ad[maxn];
void fix(int u, int p) {
for (auto it = ad[u].begin(); it != ad[u].end(); ++it) {
if (*it == p) {
ad[u].erase(it);
break;
}
}
for (auto v : ad[u]) {
fix(v, u);
mx[u] = max(mx[u], mx[v]);
}
mx[u]++;
s += mx[u];
}
void dfs(int u) {
for (auto v : ad[u]) dfs(v);
dp[u][1] = n;
for (int h = 2; h < lg + 1; ++h) {
vector<int> tmp;
for (auto v : ad[u]) {
if (dp[v][h - 1]) {
tmp.push_back(dp[v][h - 1]);
}
}
sort(tmp.begin(), tmp.end());
for (int i = 0; i < tmp.size(); ++i) {
if (tmp[i] >= tmp.size() - i) {
dp[u][h] = tmp.size() - i;
break;
}
}
}
}
void dsf(int u) {
for (auto v : ad[u]) {
dsf(v);
}
for (int h = 2; h < lg + 1; ++h) {
for (auto v : ad[u]) {
if (dp[v][h]) dp[u][h] = max(dp[u][h], dp[v][h]);
}
d[h] = dp[u][h];
}
d[1] = n;
int t = 0;
for (int h = lg; h >= 1; h--)
if (d[h]) {
d[h] -= t;
s += h * d[h];
if (!t) s -= h;
t += d[h];
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 1; i < n; ++i) {
int x, y;
cin >> x >> y;
ad[x].push_back(y);
ad[y].push_back(x);
}
fix(1, -1);
dfs(1);
dsf(1);
cout << s;
return 0;
}
| ### Prompt
Generate a cpp solution to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 5;
const int lg = 21;
int n, dp[maxn][lg], k, mx[maxn], d[lg];
long long s;
vector<int> ad[maxn];
void fix(int u, int p) {
for (auto it = ad[u].begin(); it != ad[u].end(); ++it) {
if (*it == p) {
ad[u].erase(it);
break;
}
}
for (auto v : ad[u]) {
fix(v, u);
mx[u] = max(mx[u], mx[v]);
}
mx[u]++;
s += mx[u];
}
void dfs(int u) {
for (auto v : ad[u]) dfs(v);
dp[u][1] = n;
for (int h = 2; h < lg + 1; ++h) {
vector<int> tmp;
for (auto v : ad[u]) {
if (dp[v][h - 1]) {
tmp.push_back(dp[v][h - 1]);
}
}
sort(tmp.begin(), tmp.end());
for (int i = 0; i < tmp.size(); ++i) {
if (tmp[i] >= tmp.size() - i) {
dp[u][h] = tmp.size() - i;
break;
}
}
}
}
void dsf(int u) {
for (auto v : ad[u]) {
dsf(v);
}
for (int h = 2; h < lg + 1; ++h) {
for (auto v : ad[u]) {
if (dp[v][h]) dp[u][h] = max(dp[u][h], dp[v][h]);
}
d[h] = dp[u][h];
}
d[1] = n;
int t = 0;
for (int h = lg; h >= 1; h--)
if (d[h]) {
d[h] -= t;
s += h * d[h];
if (!t) s -= h;
t += d[h];
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 1; i < n; ++i) {
int x, y;
cin >> x >> y;
ad[x].push_back(y);
ad[y].push_back(x);
}
fix(1, -1);
dfs(1);
dsf(1);
cout << s;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 100;
vector<int> G[N];
int dp[N][75];
int sub[N][75];
int dp1[N], dp2[N];
int M, n;
long long ans;
void dfs(int u, int p) {
int child = 0;
vector<int> ch_sz;
for (int v : G[u]) {
if (v == p) continue;
dfs(v, u);
for (int k = 1; k < M; k++) {
sub[u][k] = max(sub[u][k], sub[v][k]);
}
dp1[u] = max(dp1[u], dp1[v]);
dp2[u] = max(dp2[u], dp2[v]);
child++;
ch_sz.push_back(G[v].size() - 1);
}
sort(ch_sz.rbegin(), ch_sz.rend());
for (int k = 1; k <= min(child, M - 1); k++) {
vector<int> opts;
for (int v : G[u]) {
if (v == p) continue;
opts.push_back(-dp[v][k]);
}
nth_element(opts.begin(), opts.begin() + k - 1, opts.end());
dp[u][k] = max(dp[u][k], -opts[k - 1] + 1);
}
for (int k = 1; k < M; k++) {
sub[u][k] = max(sub[u][k], dp[u][k]);
ans += sub[u][k] + 1;
}
if (ch_sz.size() > 0 && ch_sz[0] > 0) {
int lo = 1, hi = ch_sz.size();
while (hi > lo) {
int mid = (hi + lo + 1) / 2;
if (ch_sz[mid - 1] >= mid)
lo = mid;
else
hi = mid - 1;
}
dp1[u] = max(dp1[u], lo);
}
dp2[u] = max(dp2[u], (int)ch_sz.size());
ans += (n - M + 1);
if (dp1[u] >= M) ans += (dp1[u] - M + 1);
if (dp2[u] >= M) ans += (dp2[u] - M + 1);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
M = min(70, n + 1);
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
dfs(1, -1);
cout << ans << endl;
return 0;
}
| ### Prompt
Create a solution in Cpp for the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 100;
vector<int> G[N];
int dp[N][75];
int sub[N][75];
int dp1[N], dp2[N];
int M, n;
long long ans;
void dfs(int u, int p) {
int child = 0;
vector<int> ch_sz;
for (int v : G[u]) {
if (v == p) continue;
dfs(v, u);
for (int k = 1; k < M; k++) {
sub[u][k] = max(sub[u][k], sub[v][k]);
}
dp1[u] = max(dp1[u], dp1[v]);
dp2[u] = max(dp2[u], dp2[v]);
child++;
ch_sz.push_back(G[v].size() - 1);
}
sort(ch_sz.rbegin(), ch_sz.rend());
for (int k = 1; k <= min(child, M - 1); k++) {
vector<int> opts;
for (int v : G[u]) {
if (v == p) continue;
opts.push_back(-dp[v][k]);
}
nth_element(opts.begin(), opts.begin() + k - 1, opts.end());
dp[u][k] = max(dp[u][k], -opts[k - 1] + 1);
}
for (int k = 1; k < M; k++) {
sub[u][k] = max(sub[u][k], dp[u][k]);
ans += sub[u][k] + 1;
}
if (ch_sz.size() > 0 && ch_sz[0] > 0) {
int lo = 1, hi = ch_sz.size();
while (hi > lo) {
int mid = (hi + lo + 1) / 2;
if (ch_sz[mid - 1] >= mid)
lo = mid;
else
hi = mid - 1;
}
dp1[u] = max(dp1[u], lo);
}
dp2[u] = max(dp2[u], (int)ch_sz.size());
ans += (n - M + 1);
if (dp1[u] >= M) ans += (dp1[u] - M + 1);
if (dp2[u] >= M) ans += (dp2[u] - M + 1);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
M = min(70, n + 1);
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
dfs(1, -1);
cout << ans << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
inline char gc() {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
? EOF
: *p1++;
}
inline int read() {
int x = 0;
char ch = getchar();
bool positive = 1;
for (; !isdigit(ch); ch = getchar())
if (ch == '-') positive = 0;
for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
return positive ? x : -x;
}
inline void write(int a) {
if (a >= 10) write(a / 10);
putchar('0' + a % 10);
}
inline void writeln(int a) {
if (a < 0) {
a = -a;
putchar('-');
}
write(a);
puts("");
}
const int N = 300005, K = 19;
long long ans;
int n, tp[N], q[N], dp[N][K];
vector<int> v[N];
inline bool cmp(int a, int b) { return a > b; }
void dfs(int p, int fa) {
for (unsigned i = 0; i < v[p].size(); i++)
if (v[p][i] != fa) {
dfs(v[p][i], p);
tp[p] = max(tp[v[p][i]], tp[p]);
}
ans += ++tp[p];
dp[p][1] = n;
for (int i = 2; i < K; i++) {
int tot = 0;
for (unsigned j = 0; j < v[p].size(); j++) q[++tot] = dp[v[p][j]][i - 1];
sort(&q[1], &q[tot + 1], cmp);
for (int j = tot; j > 1; j--) {
if (q[j] >= j) {
dp[p][i] = j;
break;
}
}
}
}
void solve(int p, int fa) {
for (unsigned i = 0; i < v[p].size(); i++)
if (v[p][i] != fa) {
solve(v[p][i], p);
for (int j = 0; j < K; j++) dp[p][j] = max(dp[p][j], dp[v[p][i]][j]);
}
for (int i = 1; i < K; i++) {
ans += ((long long)dp[p][i] - (i + 1 == K ? 0 : dp[p][i + 1])) * i;
if (i + 1 == K || dp[p][i + 1] == 0) {
ans -= i;
break;
}
}
}
int main() {
n = read();
for (int i = 1; i < n; i++) {
int s = read(), t = read();
v[s].push_back(t);
v[t].push_back(s);
}
dfs(1, 0);
solve(1, 0);
cout << ans << endl;
}
| ### Prompt
Please create a solution in CPP to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline char gc() {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
? EOF
: *p1++;
}
inline int read() {
int x = 0;
char ch = getchar();
bool positive = 1;
for (; !isdigit(ch); ch = getchar())
if (ch == '-') positive = 0;
for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
return positive ? x : -x;
}
inline void write(int a) {
if (a >= 10) write(a / 10);
putchar('0' + a % 10);
}
inline void writeln(int a) {
if (a < 0) {
a = -a;
putchar('-');
}
write(a);
puts("");
}
const int N = 300005, K = 19;
long long ans;
int n, tp[N], q[N], dp[N][K];
vector<int> v[N];
inline bool cmp(int a, int b) { return a > b; }
void dfs(int p, int fa) {
for (unsigned i = 0; i < v[p].size(); i++)
if (v[p][i] != fa) {
dfs(v[p][i], p);
tp[p] = max(tp[v[p][i]], tp[p]);
}
ans += ++tp[p];
dp[p][1] = n;
for (int i = 2; i < K; i++) {
int tot = 0;
for (unsigned j = 0; j < v[p].size(); j++) q[++tot] = dp[v[p][j]][i - 1];
sort(&q[1], &q[tot + 1], cmp);
for (int j = tot; j > 1; j--) {
if (q[j] >= j) {
dp[p][i] = j;
break;
}
}
}
}
void solve(int p, int fa) {
for (unsigned i = 0; i < v[p].size(); i++)
if (v[p][i] != fa) {
solve(v[p][i], p);
for (int j = 0; j < K; j++) dp[p][j] = max(dp[p][j], dp[v[p][i]][j]);
}
for (int i = 1; i < K; i++) {
ans += ((long long)dp[p][i] - (i + 1 == K ? 0 : dp[p][i + 1])) * i;
if (i + 1 == K || dp[p][i + 1] == 0) {
ans -= i;
break;
}
}
}
int main() {
n = read();
for (int i = 1; i < n; i++) {
int s = read(), t = read();
v[s].push_back(t);
v[t].push_back(s);
}
dfs(1, 0);
solve(1, 0);
cout << ans << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
struct edge {
int to, next;
} e[300005 * 2];
int head[300005], tot;
void add(int x, int y) {
e[++tot] = (edge){y, head[x]};
head[x] = tot;
}
int mx[300005], dp[300005][25], pp[300005][25];
long long ans;
int n, q[300005];
bool cmp(int x, int y) { return x > y; }
void dfs(int x, int fa) {
for (int i = head[x]; i; i = e[i].next)
if (e[i].to != fa) {
dfs(e[i].to, x);
mx[x] = max(mx[x], mx[e[i].to]);
}
++mx[x];
ans += mx[x];
dp[x][1] = pp[x][1] = n;
for (int p = 2; p <= 20; p++) {
int tp = 0;
for (int i = head[x]; i; i = e[i].next)
if (e[i].to != fa) q[++tp] = dp[e[i].to][p - 1];
sort(q + 1, q + tp + 1, cmp);
for (int j = tp; j >= 2; j--)
if (q[j] >= j) {
pp[x][p] = dp[x][p] = j;
break;
}
for (int i = head[x]; i; i = e[i].next)
if (e[i].to != fa) pp[x][p] = max(pp[e[i].to][p], pp[x][p]);
}
for (int i = 1; i <= 20; i++) {
ans += 1ll * (pp[x][i] - pp[x][i + 1]) * i;
if (pp[x][i + 1] == 0) {
ans -= i;
break;
}
}
}
void file_put() {
freopen("filename.in", "r", stdin);
freopen("filename.out", "w", stdout);
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
add(x, y);
add(y, x);
}
dfs(1, 0);
printf("%lld\n", ans);
return 0;
}
| ### Prompt
Construct a Cpp code solution to the problem outlined:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
struct edge {
int to, next;
} e[300005 * 2];
int head[300005], tot;
void add(int x, int y) {
e[++tot] = (edge){y, head[x]};
head[x] = tot;
}
int mx[300005], dp[300005][25], pp[300005][25];
long long ans;
int n, q[300005];
bool cmp(int x, int y) { return x > y; }
void dfs(int x, int fa) {
for (int i = head[x]; i; i = e[i].next)
if (e[i].to != fa) {
dfs(e[i].to, x);
mx[x] = max(mx[x], mx[e[i].to]);
}
++mx[x];
ans += mx[x];
dp[x][1] = pp[x][1] = n;
for (int p = 2; p <= 20; p++) {
int tp = 0;
for (int i = head[x]; i; i = e[i].next)
if (e[i].to != fa) q[++tp] = dp[e[i].to][p - 1];
sort(q + 1, q + tp + 1, cmp);
for (int j = tp; j >= 2; j--)
if (q[j] >= j) {
pp[x][p] = dp[x][p] = j;
break;
}
for (int i = head[x]; i; i = e[i].next)
if (e[i].to != fa) pp[x][p] = max(pp[e[i].to][p], pp[x][p]);
}
for (int i = 1; i <= 20; i++) {
ans += 1ll * (pp[x][i] - pp[x][i + 1]) * i;
if (pp[x][i + 1] == 0) {
ans -= i;
break;
}
}
}
void file_put() {
freopen("filename.in", "r", stdin);
freopen("filename.out", "w", stdout);
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
add(x, y);
add(y, x);
}
dfs(1, 0);
printf("%lld\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 3e5 + 10, LGMAX = 19;
int N;
int MaxLevel;
int level[NMAX], height[NMAX];
int64_t answer;
int DP[LGMAX][NMAX], father[NMAX], val[NMAX];
vector<int> T[NMAX], LevelNodes[NMAX];
vector<pair<int, int>> GoUp[NMAX];
int DFS(int node, int from, int level) {
father[node] = from;
MaxLevel = max(MaxLevel, level);
LevelNodes[level].push_back(node);
if (from != -1 && T[node].size() == 1) {
++answer;
return 1;
}
int height = 1;
for (int next : T[node]) {
if (next == from) continue;
++DP[2][node];
height = max(height, 1 + DFS(next, node, level + 1));
}
GoUp[DP[2][node]].push_back({2, node});
answer += height;
return height;
}
void iterativeDFS(int node) {
stack<pair<int, int>> S;
father[node] = -1;
LevelNodes[0].push_back(node);
S.push({node, 0});
while (!S.empty()) {
int node, i;
tie(node, i) = S.top();
if (i == 0) {
height[node] = 1;
if (father[node] != -1 && T[node].size() == 1) {
++answer;
S.pop();
continue;
}
}
if (i < (int)T[node].size()) {
S.top().second = i + 1;
if (T[node][i] == father[node]) continue;
++DP[2][node];
father[T[node][i]] = node;
level[T[node][i]] = level[node] + 1;
MaxLevel = max(MaxLevel, level[node] + 1);
LevelNodes[level[node] + 1].push_back(T[node][i]);
S.push({T[node][i], 0});
} else if (i == (int)T[node].size()) {
S.pop();
GoUp[DP[2][node]].push_back({2, node});
for (int next : T[node]) {
if (next != father[node]) {
height[node] = max(height[node], 1 + height[next]);
}
}
answer += height[node];
}
}
}
int main() {
scanf("%d", &N);
for (int i = 0; i < N - 1; ++i) {
int x, y;
scanf("%d %d", &x, &y);
T[x].push_back(y);
T[y].push_back(x);
}
iterativeDFS(1);
for (int i = 3; i < LGMAX; ++i) {
for (int j = MaxLevel; j >= 0; --j) {
for (int node : LevelNodes[j]) {
vector<int> values;
for (int next : T[node]) {
if (next == father[node]) continue;
values.push_back(DP[i - 1][next]);
}
sort(values.begin(), values.end(), greater<int>());
int k = 0;
while (k < (int)values.size() && k + 1 <= values[k]) ++k;
if (k > 0 && k <= values[k - 1]) {
DP[i][node] = k;
GoUp[DP[i][node]].push_back({i, node});
}
}
}
}
int64_t value = N;
for (int i = 1; i <= N; ++i) val[i] = 1;
for (int i = N; i >= 2; --i) {
for (auto det : GoUp[i]) {
int depth = det.first;
int node = det.second;
while (node != -1 && depth > val[node]) {
value += depth - val[node];
val[node] = depth;
node = father[node];
}
}
answer += value;
}
cout << answer << '\n';
return 0;
}
| ### Prompt
Your challenge is to write a cpp solution to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 3e5 + 10, LGMAX = 19;
int N;
int MaxLevel;
int level[NMAX], height[NMAX];
int64_t answer;
int DP[LGMAX][NMAX], father[NMAX], val[NMAX];
vector<int> T[NMAX], LevelNodes[NMAX];
vector<pair<int, int>> GoUp[NMAX];
int DFS(int node, int from, int level) {
father[node] = from;
MaxLevel = max(MaxLevel, level);
LevelNodes[level].push_back(node);
if (from != -1 && T[node].size() == 1) {
++answer;
return 1;
}
int height = 1;
for (int next : T[node]) {
if (next == from) continue;
++DP[2][node];
height = max(height, 1 + DFS(next, node, level + 1));
}
GoUp[DP[2][node]].push_back({2, node});
answer += height;
return height;
}
void iterativeDFS(int node) {
stack<pair<int, int>> S;
father[node] = -1;
LevelNodes[0].push_back(node);
S.push({node, 0});
while (!S.empty()) {
int node, i;
tie(node, i) = S.top();
if (i == 0) {
height[node] = 1;
if (father[node] != -1 && T[node].size() == 1) {
++answer;
S.pop();
continue;
}
}
if (i < (int)T[node].size()) {
S.top().second = i + 1;
if (T[node][i] == father[node]) continue;
++DP[2][node];
father[T[node][i]] = node;
level[T[node][i]] = level[node] + 1;
MaxLevel = max(MaxLevel, level[node] + 1);
LevelNodes[level[node] + 1].push_back(T[node][i]);
S.push({T[node][i], 0});
} else if (i == (int)T[node].size()) {
S.pop();
GoUp[DP[2][node]].push_back({2, node});
for (int next : T[node]) {
if (next != father[node]) {
height[node] = max(height[node], 1 + height[next]);
}
}
answer += height[node];
}
}
}
int main() {
scanf("%d", &N);
for (int i = 0; i < N - 1; ++i) {
int x, y;
scanf("%d %d", &x, &y);
T[x].push_back(y);
T[y].push_back(x);
}
iterativeDFS(1);
for (int i = 3; i < LGMAX; ++i) {
for (int j = MaxLevel; j >= 0; --j) {
for (int node : LevelNodes[j]) {
vector<int> values;
for (int next : T[node]) {
if (next == father[node]) continue;
values.push_back(DP[i - 1][next]);
}
sort(values.begin(), values.end(), greater<int>());
int k = 0;
while (k < (int)values.size() && k + 1 <= values[k]) ++k;
if (k > 0 && k <= values[k - 1]) {
DP[i][node] = k;
GoUp[DP[i][node]].push_back({i, node});
}
}
}
}
int64_t value = N;
for (int i = 1; i <= N; ++i) val[i] = 1;
for (int i = N; i >= 2; --i) {
for (auto det : GoUp[i]) {
int depth = det.first;
int node = det.second;
while (node != -1 && depth > val[node]) {
value += depth - val[node];
val[node] = depth;
node = father[node];
}
}
answer += value;
}
cout << answer << '\n';
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 300005;
int n, cnt, last[N], dep[N], f[N][25], a[N], dp[N], fa[N];
struct edge {
int to, next;
} e[N * 2];
vector<pair<int, int> > vec[N];
long long ans;
void addedge(int u, int v) {
e[++cnt].to = v;
e[cnt].next = last[u];
last[u] = cnt;
e[++cnt].to = u;
e[cnt].next = last[v];
last[v] = cnt;
}
bool cmp(int x, int y) { return x > y; }
void dfs(int x) {
dep[x] = 1;
f[x][1] = n;
for (int i = last[x]; i; i = e[i].next)
if (e[i].to != fa[x])
fa[e[i].to] = x, dfs(e[i].to), dep[x] = max(dep[x], dep[e[i].to] + 1);
ans += dep[x];
for (int i = 2; i < 20; i++) {
int tot = 0, k = 0;
for (int j = last[x]; j; j = e[j].next)
if (e[j].to != fa[x]) a[++tot] = f[e[j].to][i - 1];
if (!tot) break;
sort(a + 1, a + tot + 1, cmp);
while (k < tot && a[k + 1] > k) k++;
f[x][i] = k;
vec[k].push_back(make_pair(x, i));
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
addedge(x, y);
}
dfs(1);
int sum = n;
for (int i = 1; i <= n; i++) dp[i] = 1;
for (int k = n; k > 1; k--) {
for (int j = 0; j < vec[k].size(); j++) {
int x = vec[k][j].first, y = vec[k][j].second;
while (x && dp[x] < y) sum += y - dp[x], dp[x] = y, x = fa[x];
}
ans += sum;
}
printf("%I64d", ans);
return 0;
}
| ### Prompt
Develop a solution in Cpp to the problem described below:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 300005;
int n, cnt, last[N], dep[N], f[N][25], a[N], dp[N], fa[N];
struct edge {
int to, next;
} e[N * 2];
vector<pair<int, int> > vec[N];
long long ans;
void addedge(int u, int v) {
e[++cnt].to = v;
e[cnt].next = last[u];
last[u] = cnt;
e[++cnt].to = u;
e[cnt].next = last[v];
last[v] = cnt;
}
bool cmp(int x, int y) { return x > y; }
void dfs(int x) {
dep[x] = 1;
f[x][1] = n;
for (int i = last[x]; i; i = e[i].next)
if (e[i].to != fa[x])
fa[e[i].to] = x, dfs(e[i].to), dep[x] = max(dep[x], dep[e[i].to] + 1);
ans += dep[x];
for (int i = 2; i < 20; i++) {
int tot = 0, k = 0;
for (int j = last[x]; j; j = e[j].next)
if (e[j].to != fa[x]) a[++tot] = f[e[j].to][i - 1];
if (!tot) break;
sort(a + 1, a + tot + 1, cmp);
while (k < tot && a[k + 1] > k) k++;
f[x][i] = k;
vec[k].push_back(make_pair(x, i));
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
addedge(x, y);
}
dfs(1);
int sum = n;
for (int i = 1; i <= n; i++) dp[i] = 1;
for (int k = n; k > 1; k--) {
for (int j = 0; j < vec[k].size(); j++) {
int x = vec[k][j].first, y = vec[k][j].second;
while (x && dp[x] < y) sum += y - dp[x], dp[x] = y, x = fa[x];
}
ans += sum;
}
printf("%I64d", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 5, MAXLOG = 20;
template <typename _T>
void read(_T &x) {
x = 0;
char s = getchar();
int f = 1;
while (s > '9' || s < '0') {
if (s == '-') f = -1;
s = getchar();
}
while (s >= '0' && s <= '9') {
x = (x << 3) + (x << 1) + (s - '0'), s = getchar();
}
x *= f;
}
template <typename _T>
void write(_T x) {
if (x < 0) {
putchar('-');
x = (~x) + 1;
}
if (9 < x) {
write(x / 10);
}
putchar(x % 10 + '0');
}
template <typename _T>
_T MAX(const _T a, const _T b) {
return a > b ? a : b;
}
struct Edge {
int to, nxt;
} Graph[MAXN << 1];
vector<pair<int, int> > upt[MAXN];
int mx[MAXN];
int f[MAXN][MAXLOG];
int f1[MAXN], seq[MAXN];
int head[MAXN], fath[MAXN];
int N, cnt, lg2;
long long ans = 0, res = 0;
bool Cmp(const int &x, const int &y) { return x > y; }
void AddEdge(const int from, const int to) {
Graph[++cnt].to = to, Graph[cnt].nxt = head[from];
head[from] = cnt;
}
void DFS(const int u, const int fa) {
int siz = 0;
f[u][1] = N;
fath[u] = fa;
for (int i = head[u], v; i; i = Graph[i].nxt)
if ((v = Graph[i].to) ^ fa) DFS(v, u), f1[u] = MAX(f1[u], f1[v]);
for (int j = 2; j <= lg2; j++) {
siz = 0;
for (int i = head[u], v; i; i = Graph[i].nxt)
if ((v = Graph[i].to) ^ fa && f[v][j - 1]) seq[++siz] = f[v][j - 1];
std ::sort(seq + 1, seq + 1 + siz, Cmp);
for (int k = siz; k; k--)
if (seq[k] >= k) {
f[u][j] = k;
break;
}
if (f[u][j]) upt[f[u][j]].push_back(pair<int, int>(u, j));
}
ans += ++f1[u];
}
void Update(int x, int val) {
for (; x && mx[x] < val; x = fath[x]) res += val - mx[x], mx[x] = val;
}
int main() {
read(N);
for (int i = 1, a, b; i < N; i++)
read(a), read(b), AddEdge(a, b), AddEdge(b, a);
lg2 = log2(N);
DFS(1, 0);
for (int i = 1; i <= N; i++) mx[i] = 1;
res = N;
for (int k = N; k > 1; k--) {
for (int i = 0; i < (int)upt[k].size(); i++)
Update(upt[k][i].first, upt[k][i].second);
ans += res;
}
write(ans), putchar('\n');
return 0;
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 5, MAXLOG = 20;
template <typename _T>
void read(_T &x) {
x = 0;
char s = getchar();
int f = 1;
while (s > '9' || s < '0') {
if (s == '-') f = -1;
s = getchar();
}
while (s >= '0' && s <= '9') {
x = (x << 3) + (x << 1) + (s - '0'), s = getchar();
}
x *= f;
}
template <typename _T>
void write(_T x) {
if (x < 0) {
putchar('-');
x = (~x) + 1;
}
if (9 < x) {
write(x / 10);
}
putchar(x % 10 + '0');
}
template <typename _T>
_T MAX(const _T a, const _T b) {
return a > b ? a : b;
}
struct Edge {
int to, nxt;
} Graph[MAXN << 1];
vector<pair<int, int> > upt[MAXN];
int mx[MAXN];
int f[MAXN][MAXLOG];
int f1[MAXN], seq[MAXN];
int head[MAXN], fath[MAXN];
int N, cnt, lg2;
long long ans = 0, res = 0;
bool Cmp(const int &x, const int &y) { return x > y; }
void AddEdge(const int from, const int to) {
Graph[++cnt].to = to, Graph[cnt].nxt = head[from];
head[from] = cnt;
}
void DFS(const int u, const int fa) {
int siz = 0;
f[u][1] = N;
fath[u] = fa;
for (int i = head[u], v; i; i = Graph[i].nxt)
if ((v = Graph[i].to) ^ fa) DFS(v, u), f1[u] = MAX(f1[u], f1[v]);
for (int j = 2; j <= lg2; j++) {
siz = 0;
for (int i = head[u], v; i; i = Graph[i].nxt)
if ((v = Graph[i].to) ^ fa && f[v][j - 1]) seq[++siz] = f[v][j - 1];
std ::sort(seq + 1, seq + 1 + siz, Cmp);
for (int k = siz; k; k--)
if (seq[k] >= k) {
f[u][j] = k;
break;
}
if (f[u][j]) upt[f[u][j]].push_back(pair<int, int>(u, j));
}
ans += ++f1[u];
}
void Update(int x, int val) {
for (; x && mx[x] < val; x = fath[x]) res += val - mx[x], mx[x] = val;
}
int main() {
read(N);
for (int i = 1, a, b; i < N; i++)
read(a), read(b), AddEdge(a, b), AddEdge(b, a);
lg2 = log2(N);
DFS(1, 0);
for (int i = 1; i <= N; i++) mx[i] = 1;
res = N;
for (int k = N; k > 1; k--) {
for (int i = 0; i < (int)upt[k].size(); i++)
Update(upt[k][i].first, upt[k][i].second);
ans += res;
}
write(ans), putchar('\n');
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
bool mini(T1 &a, T2 b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
bool maxi(T1 &a, T2 b) {
if (a < b) {
a = b;
return true;
}
return false;
}
const int N = 3e5 + 5;
const int T = 500;
const int oo = 1e9;
vector<int> adj[N];
int mx[N][22];
int dp[N][22];
int deg[N];
int h[N];
int n;
void dfs(int u, int p = -1) {
h[u] = 1;
for (int v : adj[u])
if (v != p) {
dfs(v, u);
maxi(h[u], h[v] + 1);
}
dp[u][1] = n;
for (int i = 2; i <= 20; i++) {
vector<int> best;
for (int v : adj[u])
if (v != p) best.push_back(dp[v][i - 1]);
sort(best.begin(), best.end(), greater<int>());
for (int j = 0; j < deg[u]; j++)
if (best[j] >= j + 1) dp[u][i] = j + 1;
}
for (int i = 1; i <= 20; i++) {
mx[u][i] = dp[u][i];
for (int v : adj[u])
if (v != p) maxi(mx[u][i], mx[v][i]);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
deg[u]++;
deg[v]++;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 2; i <= n; i++) deg[i]--;
h[1] = 1;
dfs(1);
long long ans = accumulate(h + 1, h + n + 1, 0LL);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= 21; j++) maxi(mx[i][j], 1);
for (int j = 1; j <= 20; j++) {
if (mx[i][j] == 1) break;
ans += (mx[i][j] - mx[i][j + 1]) * j;
}
}
cout << ans;
return 0;
}
| ### Prompt
Generate a cpp solution to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
bool mini(T1 &a, T2 b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
bool maxi(T1 &a, T2 b) {
if (a < b) {
a = b;
return true;
}
return false;
}
const int N = 3e5 + 5;
const int T = 500;
const int oo = 1e9;
vector<int> adj[N];
int mx[N][22];
int dp[N][22];
int deg[N];
int h[N];
int n;
void dfs(int u, int p = -1) {
h[u] = 1;
for (int v : adj[u])
if (v != p) {
dfs(v, u);
maxi(h[u], h[v] + 1);
}
dp[u][1] = n;
for (int i = 2; i <= 20; i++) {
vector<int> best;
for (int v : adj[u])
if (v != p) best.push_back(dp[v][i - 1]);
sort(best.begin(), best.end(), greater<int>());
for (int j = 0; j < deg[u]; j++)
if (best[j] >= j + 1) dp[u][i] = j + 1;
}
for (int i = 1; i <= 20; i++) {
mx[u][i] = dp[u][i];
for (int v : adj[u])
if (v != p) maxi(mx[u][i], mx[v][i]);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
deg[u]++;
deg[v]++;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 2; i <= n; i++) deg[i]--;
h[1] = 1;
dfs(1);
long long ans = accumulate(h + 1, h + n + 1, 0LL);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= 21; j++) maxi(mx[i][j], 1);
for (int j = 1; j <= 20; j++) {
if (mx[i][j] == 1) break;
ans += (mx[i][j] - mx[i][j + 1]) * j;
}
}
cout << ans;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 300005, S = 550;
int n;
vector<int> g[N];
int p0[N];
int e[N];
vector<int> v0;
void dfs0(int x, int p) {
e[x] = 1;
p0[x] = p;
for (int i = 0; i < g[x].size(); ++i) {
int h = g[x][i];
if (h == p) continue;
dfs0(h, x);
e[x] += e[h];
}
for (int i = 0; i < g[x].size(); ++i) {
int h = g[x][i];
if (h == p) {
swap(g[x][i], g[x].back());
g[x].pop_back();
break;
}
}
v0.push_back(x);
}
vector<int> v[N];
int q2;
bool c[N];
int u;
int q[22];
int t[88];
void ubd(int tl, int tr, int x, int y, int pos) {
while (1) {
t[pos] += y;
if (tl == tr) return;
int m = (tl + tr) / 2;
if (x <= m) {
tr = m;
pos *= 2;
} else {
tl = m + 1;
pos *= 2;
++pos;
}
}
}
int qry(int tl, int tr, int k, int pos) {
while (1) {
if (tl == tr) return tl;
int m = (tl + tr) / 2;
if (t[pos * 2 + 1] >= k) {
tl = m + 1;
pos *= 2;
++pos;
} else {
tr = m;
k -= t[pos * 2 + 1];
pos *= 2;
}
}
}
int dp[N];
int maxu[N];
void dfs(int k) {
if (k == 1) {
for (int i = 0; i < n; ++i) {
int x = v0[i];
dp[x] = 1;
for (int i = 0; i < g[x].size(); ++i) {
int h = g[x][i];
dp[x] = max(dp[x], dp[h] + 1);
}
maxu[x] = dp[x];
}
return;
}
for (int i = 0; i < n; ++i) {
int x = v0[i];
dp[x] = 1;
maxu[x] = 1;
if (e[x] <= k) continue;
for (int i = 0; i < g[x].size(); ++i) {
int h = g[x][i];
++q[dp[h]];
maxu[x] = max(maxu[x], maxu[h]);
}
int s = 0;
for (int i = u; i >= 1; --i) {
s += q[i];
if (s >= k) {
dp[x] = i + 1;
break;
}
}
maxu[x] = max(maxu[x], dp[x]);
for (int i = 0; i < g[x].size(); ++i) {
int h = g[x][i];
--q[dp[h]];
}
}
}
void solv() {
cin >> n;
for (int i = 0; i < n - 1; ++i) {
int x = i + 1, y = i + 2;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
dfs0(1, 1);
for (int x = 1; x <= n; ++x) {
v[((int)(g[x]).size())].push_back(x);
}
long long ans = 0;
for (int i = n; i > S; --i) {
for (int j = 0; j < v[i].size(); ++j) {
int x = v[i][j];
while (!c[x]) {
c[x] = true;
++q2;
x = p0[x];
}
}
ans += (n + q2);
}
for (int i = 1; i <= min(n, S); ++i) {
u = 1;
if (i > 1) {
int t = 1;
while (t < n) {
t *= i;
++u;
}
}
dfs(i);
for (int x = 1; x <= n; ++x) ans += maxu[x];
}
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
solv();
return 0;
}
| ### Prompt
Develop a solution in Cpp to the problem described below:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 300005, S = 550;
int n;
vector<int> g[N];
int p0[N];
int e[N];
vector<int> v0;
void dfs0(int x, int p) {
e[x] = 1;
p0[x] = p;
for (int i = 0; i < g[x].size(); ++i) {
int h = g[x][i];
if (h == p) continue;
dfs0(h, x);
e[x] += e[h];
}
for (int i = 0; i < g[x].size(); ++i) {
int h = g[x][i];
if (h == p) {
swap(g[x][i], g[x].back());
g[x].pop_back();
break;
}
}
v0.push_back(x);
}
vector<int> v[N];
int q2;
bool c[N];
int u;
int q[22];
int t[88];
void ubd(int tl, int tr, int x, int y, int pos) {
while (1) {
t[pos] += y;
if (tl == tr) return;
int m = (tl + tr) / 2;
if (x <= m) {
tr = m;
pos *= 2;
} else {
tl = m + 1;
pos *= 2;
++pos;
}
}
}
int qry(int tl, int tr, int k, int pos) {
while (1) {
if (tl == tr) return tl;
int m = (tl + tr) / 2;
if (t[pos * 2 + 1] >= k) {
tl = m + 1;
pos *= 2;
++pos;
} else {
tr = m;
k -= t[pos * 2 + 1];
pos *= 2;
}
}
}
int dp[N];
int maxu[N];
void dfs(int k) {
if (k == 1) {
for (int i = 0; i < n; ++i) {
int x = v0[i];
dp[x] = 1;
for (int i = 0; i < g[x].size(); ++i) {
int h = g[x][i];
dp[x] = max(dp[x], dp[h] + 1);
}
maxu[x] = dp[x];
}
return;
}
for (int i = 0; i < n; ++i) {
int x = v0[i];
dp[x] = 1;
maxu[x] = 1;
if (e[x] <= k) continue;
for (int i = 0; i < g[x].size(); ++i) {
int h = g[x][i];
++q[dp[h]];
maxu[x] = max(maxu[x], maxu[h]);
}
int s = 0;
for (int i = u; i >= 1; --i) {
s += q[i];
if (s >= k) {
dp[x] = i + 1;
break;
}
}
maxu[x] = max(maxu[x], dp[x]);
for (int i = 0; i < g[x].size(); ++i) {
int h = g[x][i];
--q[dp[h]];
}
}
}
void solv() {
cin >> n;
for (int i = 0; i < n - 1; ++i) {
int x = i + 1, y = i + 2;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
dfs0(1, 1);
for (int x = 1; x <= n; ++x) {
v[((int)(g[x]).size())].push_back(x);
}
long long ans = 0;
for (int i = n; i > S; --i) {
for (int j = 0; j < v[i].size(); ++j) {
int x = v[i][j];
while (!c[x]) {
c[x] = true;
++q2;
x = p0[x];
}
}
ans += (n + q2);
}
for (int i = 1; i <= min(n, S); ++i) {
u = 1;
if (i > 1) {
int t = 1;
while (t < n) {
t *= i;
++u;
}
}
dfs(i);
for (int x = 1; x <= n; ++x) ans += maxu[x];
}
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
solv();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int logn = 20;
int n;
vector<vector<int> > g, dp;
vector<int> f, maxd;
void dfs1(int v = 0, int p = -1) {
for (int i : g[v]) {
if (i != p) {
dfs1(i, v);
maxd[v] = max(maxd[i] + 1, maxd[v]);
}
}
dp[v][0] = n;
for (int l = 1; l < logn; l++) {
for (int i : g[v]) f[min((int)g[v].size(), dp[i][l - 1])]++;
int j = g[v].size(), cnt = 0;
for (int i = 1; i <= g[v].size(); i++) {
while (cnt < i) cnt += f[j--];
if (j + 1 >= i) dp[v][l] = i;
}
for (int i : g[v]) f[min((int)g[v].size(), dp[i][l - 1])]--;
}
for (int l = logn - 1; l > 1; l--) dp[v][l - 1] = max(dp[v][l - 1], dp[v][l]);
}
void dfs2(int v = 0, int p = -1) {
for (int i : g[v]) {
if (i != p) {
dfs2(i, v);
for (int l = 1; l < logn; l++) dp[v][l] = max(dp[v][l], dp[i][l]);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
g.resize(n);
maxd.resize(n, 1);
f.resize(n + 1, 0);
dp.resize(n, vector<int>(logn, 0));
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
g[--u].push_back(--v);
g[v].push_back(u);
}
dfs1();
dfs2();
long long ans = 0;
for (int i = 0; i < n; i++) {
ans += maxd[i];
for (long long l = 0; l < logn; l++) ans += max(0ll, dp[i][l] - 1ll);
}
cout << ans << "\n";
return 0;
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int logn = 20;
int n;
vector<vector<int> > g, dp;
vector<int> f, maxd;
void dfs1(int v = 0, int p = -1) {
for (int i : g[v]) {
if (i != p) {
dfs1(i, v);
maxd[v] = max(maxd[i] + 1, maxd[v]);
}
}
dp[v][0] = n;
for (int l = 1; l < logn; l++) {
for (int i : g[v]) f[min((int)g[v].size(), dp[i][l - 1])]++;
int j = g[v].size(), cnt = 0;
for (int i = 1; i <= g[v].size(); i++) {
while (cnt < i) cnt += f[j--];
if (j + 1 >= i) dp[v][l] = i;
}
for (int i : g[v]) f[min((int)g[v].size(), dp[i][l - 1])]--;
}
for (int l = logn - 1; l > 1; l--) dp[v][l - 1] = max(dp[v][l - 1], dp[v][l]);
}
void dfs2(int v = 0, int p = -1) {
for (int i : g[v]) {
if (i != p) {
dfs2(i, v);
for (int l = 1; l < logn; l++) dp[v][l] = max(dp[v][l], dp[i][l]);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
g.resize(n);
maxd.resize(n, 1);
f.resize(n + 1, 0);
dp.resize(n, vector<int>(logn, 0));
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
g[--u].push_back(--v);
g[v].push_back(u);
}
dfs1();
dfs2();
long long ans = 0;
for (int i = 0; i < n; i++) {
ans += maxd[i];
for (long long l = 0; l < logn; l++) ans += max(0ll, dp[i][l] - 1ll);
}
cout << ans << "\n";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int N;
vector<int> graph[300005];
int mdp[20][300005];
int cdp[20][300005];
int mdep[300005];
int fre[300005];
long long ans = 0;
void dfs(int n) {
cdp[1][n] = N;
mdep[n] = 1;
for (int e : graph[n]) {
graph[e].erase(find(graph[e].begin(), graph[e].end(), n));
dfs(e);
mdep[n] = max(mdep[n], mdep[e] + 1);
for (int k = 1; k < 20; k++) {
mdp[k][n] = max(mdp[k][n], mdp[k][e]);
}
}
int dgr = graph[n].size();
for (int k = 2; k < 20; k++) {
fill(fre, fre + dgr + 2, 0);
for (int e : graph[n]) {
fre[min(dgr, cdp[k - 1][e])]++;
}
for (int d = dgr; d; d--) {
fre[d] += fre[d + 1];
if (fre[d] >= d) {
cdp[k][n] = d;
break;
}
}
}
ans += mdep[n];
for (int k = 1; k < 20; k++) {
mdp[k][n] = max(mdp[k][n], cdp[k][n]);
if (mdp[k][n] >= 2) {
ans += mdp[k][n] - 1;
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N;
for (int i = 1; i < N; i++) {
int a, b;
cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
dfs(1);
cout << ans;
}
| ### Prompt
Your challenge is to write a cpp solution to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int N;
vector<int> graph[300005];
int mdp[20][300005];
int cdp[20][300005];
int mdep[300005];
int fre[300005];
long long ans = 0;
void dfs(int n) {
cdp[1][n] = N;
mdep[n] = 1;
for (int e : graph[n]) {
graph[e].erase(find(graph[e].begin(), graph[e].end(), n));
dfs(e);
mdep[n] = max(mdep[n], mdep[e] + 1);
for (int k = 1; k < 20; k++) {
mdp[k][n] = max(mdp[k][n], mdp[k][e]);
}
}
int dgr = graph[n].size();
for (int k = 2; k < 20; k++) {
fill(fre, fre + dgr + 2, 0);
for (int e : graph[n]) {
fre[min(dgr, cdp[k - 1][e])]++;
}
for (int d = dgr; d; d--) {
fre[d] += fre[d + 1];
if (fre[d] >= d) {
cdp[k][n] = d;
break;
}
}
}
ans += mdep[n];
for (int k = 1; k < 20; k++) {
mdp[k][n] = max(mdp[k][n], cdp[k][n]);
if (mdp[k][n] >= 2) {
ans += mdp[k][n] - 1;
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N;
for (int i = 1; i < N; i++) {
int a, b;
cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
dfs(1);
cout << ans;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
template <typename T>
bool cmax(T &a, T b) {
return (a < b) ? a = b, 1 : 0;
}
template <typename T>
bool cmin(T &a, T b) {
return (a > b) ? a = b, 1 : 0;
}
template <typename T>
T read() {
T ans = 0, f = 1;
char ch = getchar();
while (!isdigit(ch) && ch != '-') ch = getchar();
if (ch == '-') f = -1, ch = getchar();
while (isdigit(ch))
ans = (ans << 3) + (ans << 1) + (ch - '0'), ch = getchar();
return ans * f;
}
template <typename T>
void write(T x, char y) {
if (x == 0) {
putchar('0'), putchar(y);
return;
}
if (x < 0) {
putchar('-');
x = -x;
}
static char wr[20];
int top = 0;
for (; x; x /= 10) wr[++top] = x % 10 + '0';
while (top) putchar(wr[top--]);
putchar(y);
}
void file() {}
int n;
vector<int> E[N];
void input() {
int x, y;
n = read<int>();
for (register int i = (int)(2); i <= (int)(n); ++i) {
x = read<int>(), y = read<int>();
E[x].push_back(y), E[y].push_back(x);
}
}
const int inf = 0x3f3f3f3f;
const int M = 20;
int dp[N][21];
long long ans;
int q[N], top;
void DP(int u, int pre) {
dp[u][1] = n;
for (int v : E[u])
if (v ^ pre) DP(v, u);
dp[u][1] = n;
for (register int i = (int)(2); i <= (int)(M - 1); ++i) {
top = 0;
for (int v : E[u])
if (v ^ pre) q[++top] = dp[v][i - 1];
sort(q + 1, q + top + 1);
dp[u][i] = 0;
for (register int k = (int)(top); k >= (int)(1); --k)
if (q[top - k + 1] >= k) {
dp[u][i] = k;
break;
}
}
}
int num[N];
void dfs(int u, int pre) {
num[u] = 1;
for (int v : E[u])
if (v ^ pre) {
dfs(v, u);
cmax(num[u], num[v] + 1);
for (register int i = (int)(1); i <= (int)(M - 1); ++i)
cmax(dp[u][i], dp[v][i]);
}
}
void work() {
DP(1, 0);
dfs(1, 0);
for (register int i = (int)(1); i <= (int)(n); ++i) ans += num[i];
for (register int i = (int)(1); i <= (int)(n); ++i)
for (register int j = (int)(1); j <= (int)(M - 1); ++j) {
if (dp[i][j]) ans += dp[i][j] - 1;
}
write(ans, '\n');
}
int main() {
file();
input();
work();
return 0;
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
template <typename T>
bool cmax(T &a, T b) {
return (a < b) ? a = b, 1 : 0;
}
template <typename T>
bool cmin(T &a, T b) {
return (a > b) ? a = b, 1 : 0;
}
template <typename T>
T read() {
T ans = 0, f = 1;
char ch = getchar();
while (!isdigit(ch) && ch != '-') ch = getchar();
if (ch == '-') f = -1, ch = getchar();
while (isdigit(ch))
ans = (ans << 3) + (ans << 1) + (ch - '0'), ch = getchar();
return ans * f;
}
template <typename T>
void write(T x, char y) {
if (x == 0) {
putchar('0'), putchar(y);
return;
}
if (x < 0) {
putchar('-');
x = -x;
}
static char wr[20];
int top = 0;
for (; x; x /= 10) wr[++top] = x % 10 + '0';
while (top) putchar(wr[top--]);
putchar(y);
}
void file() {}
int n;
vector<int> E[N];
void input() {
int x, y;
n = read<int>();
for (register int i = (int)(2); i <= (int)(n); ++i) {
x = read<int>(), y = read<int>();
E[x].push_back(y), E[y].push_back(x);
}
}
const int inf = 0x3f3f3f3f;
const int M = 20;
int dp[N][21];
long long ans;
int q[N], top;
void DP(int u, int pre) {
dp[u][1] = n;
for (int v : E[u])
if (v ^ pre) DP(v, u);
dp[u][1] = n;
for (register int i = (int)(2); i <= (int)(M - 1); ++i) {
top = 0;
for (int v : E[u])
if (v ^ pre) q[++top] = dp[v][i - 1];
sort(q + 1, q + top + 1);
dp[u][i] = 0;
for (register int k = (int)(top); k >= (int)(1); --k)
if (q[top - k + 1] >= k) {
dp[u][i] = k;
break;
}
}
}
int num[N];
void dfs(int u, int pre) {
num[u] = 1;
for (int v : E[u])
if (v ^ pre) {
dfs(v, u);
cmax(num[u], num[v] + 1);
for (register int i = (int)(1); i <= (int)(M - 1); ++i)
cmax(dp[u][i], dp[v][i]);
}
}
void work() {
DP(1, 0);
dfs(1, 0);
for (register int i = (int)(1); i <= (int)(n); ++i) ans += num[i];
for (register int i = (int)(1); i <= (int)(n); ++i)
for (register int j = (int)(1); j <= (int)(M - 1); ++j) {
if (dp[i][j]) ans += dp[i][j] - 1;
}
write(ans, '\n');
}
int main() {
file();
input();
work();
return 0;
}
``` |
#include <bits/stdc++.h>
namespace fuck {
const int N = 300100, M = 20;
int begin[N], next[N * 2], to[N * 2];
int n, e, lim;
void add(int x, int y, bool k = 1) {
to[++e] = y;
next[e] = begin[x];
begin[x] = e;
if (k) add(y, x, 0);
}
void initialize() {
scanf("%d", &n);
for (int i = 1, u, v; i < n; i++) scanf("%d%d", &u, &v), add(u, v);
lim = (int)std::sqrt(n);
while (lim * lim + lim + 1 <= n) lim++;
}
std::vector<int> seq;
int f[N][M];
void dfs(int p = 1, int h = 0) {
for (int i = begin[p], q; i; i = next[i])
if ((q = to[i]) != h) dfs(q, p);
f[p][1] = n;
for (int dep = 2; dep < M; dep++) {
seq.resize(0);
for (int i = begin[p], q; i; i = next[i])
if ((q = to[i]) != h) seq.push_back(f[q][dep - 1]);
f[p][dep] = 0;
std::sort(seq.begin(), seq.end());
for (int k = seq.size(); k; k--)
if (seq.end()[-k] >= k) {
f[p][dep] = k;
break;
}
}
}
void redfs(int p = 1, int h = 0) {
for (int i = begin[p], q; i; i = next[i])
if ((q = to[i]) != h) {
redfs(q, p);
for (int dep = 1; dep < M; dep++)
f[p][dep] = std::max(f[p][dep], f[q][dep]);
}
}
long long part1() {
dfs(), redfs();
long long ret = 0;
for (int i = 1; i <= n; i++)
for (int dep = 1; dep < M; dep++)
if (f[i][dep] > 0) ret += f[i][dep] - 1;
return ret;
}
int dep[N];
void depdfs(int p = 1, int h = 0) {
dep[p] = 1;
for (int i = begin[p], q; i; i = next[i])
if ((q = to[i]) != h) depdfs(q, p), dep[p] = std::max(dep[p], dep[q] + 1);
}
long long part2() {
depdfs();
long long ret = 0;
for (int i = 1; i <= n; i++) ret += dep[i];
return ret;
}
void solve() {
initialize();
long long ans = part1() + part2();
printf("%I64d\n", ans);
}
} // namespace fuck
int main() {
fuck::solve();
return 0;
}
| ### Prompt
Your task is to create a cpp solution to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
namespace fuck {
const int N = 300100, M = 20;
int begin[N], next[N * 2], to[N * 2];
int n, e, lim;
void add(int x, int y, bool k = 1) {
to[++e] = y;
next[e] = begin[x];
begin[x] = e;
if (k) add(y, x, 0);
}
void initialize() {
scanf("%d", &n);
for (int i = 1, u, v; i < n; i++) scanf("%d%d", &u, &v), add(u, v);
lim = (int)std::sqrt(n);
while (lim * lim + lim + 1 <= n) lim++;
}
std::vector<int> seq;
int f[N][M];
void dfs(int p = 1, int h = 0) {
for (int i = begin[p], q; i; i = next[i])
if ((q = to[i]) != h) dfs(q, p);
f[p][1] = n;
for (int dep = 2; dep < M; dep++) {
seq.resize(0);
for (int i = begin[p], q; i; i = next[i])
if ((q = to[i]) != h) seq.push_back(f[q][dep - 1]);
f[p][dep] = 0;
std::sort(seq.begin(), seq.end());
for (int k = seq.size(); k; k--)
if (seq.end()[-k] >= k) {
f[p][dep] = k;
break;
}
}
}
void redfs(int p = 1, int h = 0) {
for (int i = begin[p], q; i; i = next[i])
if ((q = to[i]) != h) {
redfs(q, p);
for (int dep = 1; dep < M; dep++)
f[p][dep] = std::max(f[p][dep], f[q][dep]);
}
}
long long part1() {
dfs(), redfs();
long long ret = 0;
for (int i = 1; i <= n; i++)
for (int dep = 1; dep < M; dep++)
if (f[i][dep] > 0) ret += f[i][dep] - 1;
return ret;
}
int dep[N];
void depdfs(int p = 1, int h = 0) {
dep[p] = 1;
for (int i = begin[p], q; i; i = next[i])
if ((q = to[i]) != h) depdfs(q, p), dep[p] = std::max(dep[p], dep[q] + 1);
}
long long part2() {
depdfs();
long long ret = 0;
for (int i = 1; i <= n; i++) ret += dep[i];
return ret;
}
void solve() {
initialize();
long long ans = part1() + part2();
printf("%I64d\n", ans);
}
} // namespace fuck
int main() {
fuck::solve();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int LG = 20;
int dp[300100][LG], mx[300100][LG], dep[300100];
int buf[300100];
vector<int> g[300100];
long long solve(int u, int p) {
long long ans = 0;
dep[u] = 1;
for (auto v : g[u]) {
if (v == p) continue;
ans += solve(v, u);
dep[u] = max(dep[u], dep[v] + 1);
for (int j = 0; j < LG; j++) {
mx[u][j] = max(mx[u][j], mx[v][j]);
}
}
for (int j = 0; j < LG; j++) {
if (j) {
int m = 0;
for (auto v : g[u]) {
if (v != p) {
buf[m++] = dp[v][j - 1];
}
}
sort(buf, buf + m, greater<int>());
while (m && buf[m - 1] < m) m--;
dp[u][j] = m;
}
mx[u][j] = max(mx[u][j], dp[u][j]);
ans += mx[u][j];
}
if (dep[u] > LG) ans += dep[u] - LG;
return ans;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 1; i <= n; i++) dp[i][0] = n;
printf("%lld\n", solve(1, 0));
return 0;
}
| ### Prompt
Generate a CPP solution to the following problem:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int LG = 20;
int dp[300100][LG], mx[300100][LG], dep[300100];
int buf[300100];
vector<int> g[300100];
long long solve(int u, int p) {
long long ans = 0;
dep[u] = 1;
for (auto v : g[u]) {
if (v == p) continue;
ans += solve(v, u);
dep[u] = max(dep[u], dep[v] + 1);
for (int j = 0; j < LG; j++) {
mx[u][j] = max(mx[u][j], mx[v][j]);
}
}
for (int j = 0; j < LG; j++) {
if (j) {
int m = 0;
for (auto v : g[u]) {
if (v != p) {
buf[m++] = dp[v][j - 1];
}
}
sort(buf, buf + m, greater<int>());
while (m && buf[m - 1] < m) m--;
dp[u][j] = m;
}
mx[u][j] = max(mx[u][j], dp[u][j]);
ans += mx[u][j];
}
if (dep[u] > LG) ans += dep[u] - LG;
return ans;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 1; i <= n; i++) dp[i][0] = n;
printf("%lld\n", solve(1, 0));
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <class _Tp>
_Tp gcd(_Tp a, _Tp b) {
return (b == 0) ? (a) : (gcd(b, a % b));
}
const long long Inf = 1000000000000000000ll;
const int inf = 1000000000;
char buf[1 << 25], *p1 = buf, *p2 = buf;
inline int getc() {
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 25, stdin), p1 == p2)
? EOF
: *p1++;
}
inline int read() {
int x = 0, f = 0;
char c = getc();
while (!isdigit(c)) (c == '-') && (f = 1), c = getc();
while (isdigit(c)) x = x * 10 + (c & 15), c = getc();
return f ? -x : x;
}
const int Lg = 19;
const int N = 300005;
vector<int> e[N];
vector<pair<int, int> > upt[N];
int fa[N], a[N], dp[N][Lg], n;
long long ans, delta;
int getone(int u, int f) {
int res = 0;
fa[u] = f;
for (int i = 0; i < e[u].size(); ++i) {
if (e[u][i] == f) continue;
res = max(res, getone(e[u][i], u));
}
ans += res + 1;
return res + 1;
}
void dfs(int u) {
for (int i = 0; i < e[u].size(); ++i) {
if (e[u][i] == fa[u]) continue;
dfs(e[u][i]);
}
dp[u][1] = n;
for (int t = 2; t < Lg; ++t) {
int tot = 0;
for (int i = 0; i < e[u].size(); ++i) {
int to = e[u][i];
if (to == fa[u]) continue;
if (dp[to][t - 1]) a[++tot] = dp[to][t - 1];
}
sort(a + 1, a + tot + 1, greater<int>());
for (int i = tot; i >= 1 && !dp[u][t]; --i)
if (a[i] >= i) dp[u][t] = i;
if (dp[u][t] > 1) upt[dp[u][t]].push_back(make_pair(u, t));
}
}
void update(int u, int w) {
while (u) {
if (w <= a[u]) break;
delta += w - a[u];
a[u] = w;
u = fa[u];
}
}
int main() {
n = read();
for (int i = 1; i < n; ++i) {
int x = read(), y = read();
e[x].push_back(y);
e[y].push_back(x);
}
getone(1, 0);
dfs(1);
delta = n;
for (int i = 1; i <= n; ++i) a[i] = 1;
for (int k = n; k > 1; --k) {
for (int i = 0; i < upt[k].size(); ++i)
update(upt[k][i].first, upt[k][i].second);
ans += delta;
}
printf("%lld\n", ans);
return 0;
}
| ### Prompt
Develop a solution in Cpp to the problem described below:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <class _Tp>
_Tp gcd(_Tp a, _Tp b) {
return (b == 0) ? (a) : (gcd(b, a % b));
}
const long long Inf = 1000000000000000000ll;
const int inf = 1000000000;
char buf[1 << 25], *p1 = buf, *p2 = buf;
inline int getc() {
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 25, stdin), p1 == p2)
? EOF
: *p1++;
}
inline int read() {
int x = 0, f = 0;
char c = getc();
while (!isdigit(c)) (c == '-') && (f = 1), c = getc();
while (isdigit(c)) x = x * 10 + (c & 15), c = getc();
return f ? -x : x;
}
const int Lg = 19;
const int N = 300005;
vector<int> e[N];
vector<pair<int, int> > upt[N];
int fa[N], a[N], dp[N][Lg], n;
long long ans, delta;
int getone(int u, int f) {
int res = 0;
fa[u] = f;
for (int i = 0; i < e[u].size(); ++i) {
if (e[u][i] == f) continue;
res = max(res, getone(e[u][i], u));
}
ans += res + 1;
return res + 1;
}
void dfs(int u) {
for (int i = 0; i < e[u].size(); ++i) {
if (e[u][i] == fa[u]) continue;
dfs(e[u][i]);
}
dp[u][1] = n;
for (int t = 2; t < Lg; ++t) {
int tot = 0;
for (int i = 0; i < e[u].size(); ++i) {
int to = e[u][i];
if (to == fa[u]) continue;
if (dp[to][t - 1]) a[++tot] = dp[to][t - 1];
}
sort(a + 1, a + tot + 1, greater<int>());
for (int i = tot; i >= 1 && !dp[u][t]; --i)
if (a[i] >= i) dp[u][t] = i;
if (dp[u][t] > 1) upt[dp[u][t]].push_back(make_pair(u, t));
}
}
void update(int u, int w) {
while (u) {
if (w <= a[u]) break;
delta += w - a[u];
a[u] = w;
u = fa[u];
}
}
int main() {
n = read();
for (int i = 1; i < n; ++i) {
int x = read(), y = read();
e[x].push_back(y);
e[y].push_back(x);
}
getone(1, 0);
dfs(1);
delta = n;
for (int i = 1; i <= n; ++i) a[i] = 1;
for (int k = n; k > 1; --k) {
for (int i = 0; i < upt[k].size(); ++i)
update(upt[k][i].first, upt[k][i].second);
ans += delta;
}
printf("%lld\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long read() {
char ch = getchar();
long long x = 0;
int op = 1;
for (; !isdigit(ch); ch = getchar())
if (ch == '-') op = -1;
for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
return x * op;
}
int n, cnt, head[300005], mx[300005], dp[300005][25], q[300005];
long long ans;
struct edge {
int to, nxt;
} e[300005 << 1];
void adde(int x, int y) {
e[++cnt].to = y;
e[cnt].nxt = head[x];
head[x] = cnt;
}
void ins(int x, int y) {
adde(x, y);
adde(y, x);
}
const bool cmp(const int x, const int y) { return x > y; }
void dfs(int u, int pr) {
for (int i = head[u], v; i; i = e[i].nxt)
if ((v = e[i].to) != pr) {
dfs(v, u);
mx[u] = max(mx[u], mx[v]);
}
ans += ++mx[u];
dp[u][1] = n;
for (int i = (2); i <= (20); i++) {
int tp = 0;
for (int j = head[u], v; j; j = e[j].nxt)
if ((v = e[j].to) != pr) q[++tp] = dp[v][i - 1];
sort(q + 1, q + 1 + tp, cmp);
for (int j = (tp); j >= (2); j--)
if (q[j] >= j) {
dp[u][i] = j;
break;
}
}
}
void solve(int u, int pr) {
for (int i = head[u], v; i; i = e[i].nxt)
if ((v = e[i].to) != pr) {
solve(v, u);
for (int j = (1); j <= (20); j++) dp[u][j] = max(dp[u][j], dp[v][j]);
}
for (int i = (1); i <= (20); i++) {
ans += (long long)(dp[u][i] - dp[u][i + 1]) * i;
if (dp[u][i + 1] == 0) {
ans -= i;
break;
}
}
}
int main() {
n = read();
for (int i = (1); i <= (n - 1); i++) {
int x = read(), y = read();
ins(x, y);
}
dfs(1, 0);
solve(1, 0);
cout << ans << endl;
return 0;
}
| ### Prompt
Develop a solution in CPP to the problem described below:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long read() {
char ch = getchar();
long long x = 0;
int op = 1;
for (; !isdigit(ch); ch = getchar())
if (ch == '-') op = -1;
for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
return x * op;
}
int n, cnt, head[300005], mx[300005], dp[300005][25], q[300005];
long long ans;
struct edge {
int to, nxt;
} e[300005 << 1];
void adde(int x, int y) {
e[++cnt].to = y;
e[cnt].nxt = head[x];
head[x] = cnt;
}
void ins(int x, int y) {
adde(x, y);
adde(y, x);
}
const bool cmp(const int x, const int y) { return x > y; }
void dfs(int u, int pr) {
for (int i = head[u], v; i; i = e[i].nxt)
if ((v = e[i].to) != pr) {
dfs(v, u);
mx[u] = max(mx[u], mx[v]);
}
ans += ++mx[u];
dp[u][1] = n;
for (int i = (2); i <= (20); i++) {
int tp = 0;
for (int j = head[u], v; j; j = e[j].nxt)
if ((v = e[j].to) != pr) q[++tp] = dp[v][i - 1];
sort(q + 1, q + 1 + tp, cmp);
for (int j = (tp); j >= (2); j--)
if (q[j] >= j) {
dp[u][i] = j;
break;
}
}
}
void solve(int u, int pr) {
for (int i = head[u], v; i; i = e[i].nxt)
if ((v = e[i].to) != pr) {
solve(v, u);
for (int j = (1); j <= (20); j++) dp[u][j] = max(dp[u][j], dp[v][j]);
}
for (int i = (1); i <= (20); i++) {
ans += (long long)(dp[u][i] - dp[u][i + 1]) * i;
if (dp[u][i + 1] == 0) {
ans -= i;
break;
}
}
}
int main() {
n = read();
for (int i = (1); i <= (n - 1); i++) {
int x = read(), y = read();
ins(x, y);
}
dfs(1, 0);
solve(1, 0);
cout << ans << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
int n;
vector<int> adj[N];
vector<int> karr[N];
multiset<int> ss[N];
vector<int> *val[N];
long long sum[N];
long long ans = 0;
void dfs(int v, int p) {
int c = adj[v].size() - (p > 0);
for (auto it : adj[v]) {
if (it == p) continue;
dfs(it, v);
}
val[v] = new vector<int>(c, 0);
sum[v] = 0;
for (auto it : adj[v]) {
if (it == p) continue;
for (int i = 0; i < (int)karr[it].size() && i < c; i++) {
ss[i].insert(karr[it][i]);
if ((int)ss[i].size() > i + 1) {
ss[i].erase(ss[i].begin());
}
}
if (val[v]->size() < val[it]->size()) {
swap(val[v], val[it]);
swap(sum[v], sum[it]);
}
for (int i = 0; i < (int)val[it]->size(); i++) {
if ((*val[it])[i] > (*val[v])[i]) {
sum[v] += (*val[it])[i] - (*val[v])[i];
(*val[v])[i] = (*val[it])[i];
}
}
}
for (int i = 1; i <= c; i++) {
int cnt = 0;
if ((int)ss[i - 1].size() == i) {
cnt = 1 + (*ss[i - 1].begin());
} else
cnt = 2;
karr[v].push_back(cnt);
ss[i - 1].clear();
if (cnt > (*val[v])[i - 1]) {
sum[v] += cnt - (*val[v])[i - 1];
(*val[v])[i - 1] = cnt;
}
}
ans += sum[v];
ans += n - val[v]->size();
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1, 0);
cout << ans << "\n";
return 0;
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
You're given a tree with n vertices rooted at 1.
We say that there's a k-ary heap of depth m located at u if the following holds:
* For m = 1 u itself is a k-ary heap of depth 1.
* For m > 1 vertex u is a k-ary heap of depth m if at least k of its children are k-ary heaps of depth at least m - 1.
Denote dpk(u) as maximum depth of k-ary heap in the subtree of u (including u). Your goal is to compute <image>.
Input
The first line contains an integer n denoting the size of the tree (2 ≤ n ≤ 3·105).
The next n - 1 lines contain two integers u, v each, describing vertices connected by i-th edge.
It's guaranteed that the given configuration forms a tree.
Output
Output the answer to the task.
Examples
Input
4
1 3
2 3
4 3
Output
21
Input
4
1 2
2 3
3 4
Output
22
Note
Consider sample case one.
For k ≥ 3 all dpk will be equal to 1.
For k = 2 dpk is 2 if <image> and 1 otherwise.
For k = 1 dpk values are (3, 1, 2, 1) respectively.
To sum up, 4·1 + 4·1 + 2·2 + 2·1 + 3 + 1 + 2 + 1 = 21.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
int n;
vector<int> adj[N];
vector<int> karr[N];
multiset<int> ss[N];
vector<int> *val[N];
long long sum[N];
long long ans = 0;
void dfs(int v, int p) {
int c = adj[v].size() - (p > 0);
for (auto it : adj[v]) {
if (it == p) continue;
dfs(it, v);
}
val[v] = new vector<int>(c, 0);
sum[v] = 0;
for (auto it : adj[v]) {
if (it == p) continue;
for (int i = 0; i < (int)karr[it].size() && i < c; i++) {
ss[i].insert(karr[it][i]);
if ((int)ss[i].size() > i + 1) {
ss[i].erase(ss[i].begin());
}
}
if (val[v]->size() < val[it]->size()) {
swap(val[v], val[it]);
swap(sum[v], sum[it]);
}
for (int i = 0; i < (int)val[it]->size(); i++) {
if ((*val[it])[i] > (*val[v])[i]) {
sum[v] += (*val[it])[i] - (*val[v])[i];
(*val[v])[i] = (*val[it])[i];
}
}
}
for (int i = 1; i <= c; i++) {
int cnt = 0;
if ((int)ss[i - 1].size() == i) {
cnt = 1 + (*ss[i - 1].begin());
} else
cnt = 2;
karr[v].push_back(cnt);
ss[i - 1].clear();
if (cnt > (*val[v])[i - 1]) {
sum[v] += cnt - (*val[v])[i - 1];
(*val[v])[i - 1] = cnt;
}
}
ans += sum[v];
ans += n - val[v]->size();
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1, 0);
cout << ans << "\n";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
using cat = long long;
unsigned long long MOD = 998244353LL;
unsigned long long pw(unsigned long long a, unsigned long long e) {
if (e <= 0) return 1;
unsigned long long x = pw(a, e / 2);
x = (x * x) % MOD;
if (e % 2 != 0) x = (x * a) % MOD;
return x;
}
unsigned long long proot = 3;
vector<vector<unsigned long long> > OM;
vector<unsigned long long> DEP;
vector<unsigned long long> DFT(vector<unsigned long long> &A, int N, int d,
int x, int dir) {
vector<unsigned long long> ret(N, 0);
if (N == 1) {
ret[0] = A[x];
return ret;
}
if (N == 2) {
ret[0] = (A[x] + A[x + d]) % MOD;
ret[1] = (A[x] + MOD - A[x + d]) % MOD;
return ret;
}
int dep = DEP[N];
vector<unsigned long long> DFT0[2];
DFT0[0] = DFT(A, N / 2, d * 2, x, dir);
DFT0[1] = DFT(A, N / 2, d * 2, x + d, dir);
for (int i = 0; i < N / 2; i++) {
unsigned long long com = OM[dep][(N + dir * i) % N];
ret[i] = (DFT0[0][i] + com * DFT0[1][i]) % MOD;
ret[i + N / 2] = (DFT0[0][i] + (MOD - com) * DFT0[1][i]) % MOD;
}
return ret;
}
int SN = 1 << 20;
vector<unsigned long long> convolution(vector<unsigned long long> A,
vector<unsigned long long> B) {
if ((int)A.size() < 50) {
vector<unsigned long long> ret(A.size() * 2 - 1);
unsigned long long mod2 = MOD * MOD * 4;
for (int i = 0; i < (int)A.size(); i++)
for (int j = 0; j < (int)B.size(); j++) {
ret[i + j] += A[i] * B[j];
if (ret[i + j] >= mod2) ret[i + j] -= mod2;
}
for (int i = 0; i < (int)ret.size(); i++) ret[i] %= MOD;
return ret;
}
int Smin = 1;
while (Smin < (int)A.size() * 2) Smin *= 2;
if (OM.empty()) {
OM.resize(21);
for (int i = 0; i < 21; i++) {
unsigned long long root = pw(proot, (MOD - 1) / (1 << i));
OM[i].resize(1 << i, 1);
for (int j = 1; j < (1 << i); j++) OM[i][j] = (OM[i][j - 1] * root) % MOD;
reverse(begin(OM[i]) + 1, end(OM[i]));
}
DEP.resize(SN + 1);
for (int i = 1; i <= SN; i++) {
int dep = 0, n = i;
while (n > 1) n /= 2, dep++;
DEP[i] = dep;
}
}
A.resize(Smin, 0);
B.resize(Smin, 0);
vector<unsigned long long> FA = DFT(A, A.size(), 1, 0, 1);
vector<unsigned long long> FB = DFT(B, B.size(), 1, 0, 1);
vector<unsigned long long> Fret(FA.size(), 0);
for (unsigned int i = 0; i < FA.size(); i++) Fret[i] = (FA[i] * FB[i]) % MOD;
vector<unsigned long long> ret = DFT(Fret, Fret.size(), 1, 0, -1);
unsigned long long inv = pw(ret.size(), MOD - 2);
for (unsigned int i = 0; i < ret.size(); i++) ret[i] = (ret[i] * inv) % MOD;
ret.resize(A.size() * 2 - 1);
return ret;
}
vector<cat> poly_prod(vector<cat> V, int K, cat mod) {
int N = V.size();
if (N <= 10) {
vector<cat> ret(min(K + 1, N + 1), 0);
ret[0] = 1;
for (int i = 0; i < N; i++)
for (int j = min(K, i + 1); j > 0; j--)
ret[j] = (ret[j] + ret[j - 1] * V[i]) % mod;
return ret;
}
vector<cat> Vl, Vr;
for (int i = 0; i < N / 2; i++) Vl.push_back(V[i]);
for (int i = N / 2; i < N; i++) Vr.push_back(V[i]);
vector<cat> Pl = poly_prod(Vl, K, mod), Pr = poly_prod(Vr, K, mod);
vector<unsigned long long> Rl(min(K + 1, (int)max(Pl.size(), Pr.size())), 0);
vector<unsigned long long> Rr(min(K + 1, (int)max(Pl.size(), Pr.size())), 0);
for (int i = 0; i < min(K + 1, (int)Pl.size()); i++) Rl[i] = Pl[i];
for (int i = 0; i < min(K + 1, (int)Pr.size()); i++) Rr[i] = Pr[i];
vector<unsigned long long> R = convolution(Rl, Rr);
vector<cat> ret(min(K + 1, N + 1), 0);
for (int i = 0; i < min(K + 1, N + 1); i++) ret[i] = R[i];
return ret;
}
vector<cat> poly_inv(vector<cat> &P, int deg, cat mod) {
int deg_p2 = deg;
while (deg_p2 & (deg_p2 - 1)) deg_p2++;
vector<unsigned long long> A(1, 1);
for (int l = 1; l < deg_p2; l *= 2) {
vector<unsigned long long> L(l), U(l);
for (int i = 0; i < min((int)P.size(), l); i++) L[i] = P[i];
vector<unsigned long long> C = convolution(L, A);
C.push_back(0);
for (int i = 0; i < l; i++) C[i] = C[i + l];
C.resize(l);
for (int i = l; i < min((int)P.size(), 2 * l); i++) U[i - l] = P[i];
vector<unsigned long long> R = convolution(A, U);
R.resize(l, 0);
for (int i = 0; i < l; i++) R[i] = (R[i] + C[i]) % mod;
vector<unsigned long long> B = convolution(R, A);
A.resize(2 * l, 0);
for (int i = 0; i < l; i++) A[i + l] = (mod - B[i]) % mod;
}
vector<cat> ret(deg);
for (int i = 0; i < deg; i++) ret[i] = A[i];
return ret;
}
vector<cat> poly_rem(vector<cat> &P, vector<cat> Q, cat mod) {
if (P.size() < Q.size()) return P;
int deg = P.size() - Q.size() + 1;
vector<cat> Pi = P;
reverse(begin(Pi), end(Pi));
Pi.resize(min((int)Pi.size(), deg));
vector<cat> Qi = Q;
reverse(begin(Qi), end(Qi));
vector<cat> Qii = poly_inv(Qi, deg, mod);
vector<unsigned long long> pi(max(Pi.size(), Qii.size()), 0);
for (int i = 0; i < (int)Pi.size(); i++) pi[i] = Pi[i];
vector<unsigned long long> qii(max(Pi.size(), Qii.size()), 0);
for (int i = 0; i < (int)Qii.size(); i++) qii[i] = Qii[i];
vector<unsigned long long> D = convolution(pi, qii);
D.resize(deg, 0);
reverse(begin(D), end(D));
D.resize(max(Q.size(), D.size()), 0);
vector<unsigned long long> q(max(Q.size(), D.size()), 0);
for (int i = 0; i < (int)Q.size(); i++) q[i] = Q[i];
vector<unsigned long long> sub = convolution(q, D);
while (sub.size() > P.size() && sub.back() == 0) sub.pop_back();
vector<cat> ret = P;
for (int i = 0; i < (int)sub.size(); i++) {
ret[i] = (ret[i] - (cat)sub[i]) % mod;
if (ret[i] < 0) ret[i] += mod;
}
while ((int)ret.size() > 1 && ret.back() == 0) ret.pop_back();
assert(ret.size() < Q.size());
return ret;
}
class poly_eval_tree {
vector<cat> X;
vector<vector<cat> > Q;
vector<vector<int> > son;
cat mod;
void build_tree(int l, int r, int cur) {
if (r - l <= 2) {
Q[cur].resize(r - l + 1, 0);
Q[cur][0] = 1;
for (int i = l; i < r; i++)
for (int j = i - l + 1; j >= 0; j--)
Q[cur][j] = (((j == 0) ? 0 : Q[cur][j - 1]) - X[i] * Q[cur][j]) % mod;
for (int i = 0; i <= r - l; i++)
if (Q[cur][i] < 0) Q[cur][i] += mod;
return;
}
son[cur][0] = son.size();
son.push_back(vector<int>(2, -1));
Q.push_back(vector<cat>());
build_tree(l, (l + r) / 2, son[cur][0]);
son[cur][1] = son.size();
son.push_back(vector<int>(2, -1));
Q.push_back(vector<cat>());
build_tree((l + r) / 2, r, son[cur][1]);
vector<unsigned long long> Ql(
max(Q[son[cur][0]].size(), Q[son[cur][1]].size()), 0);
for (int i = 0; i < (int)Q[son[cur][0]].size(); i++)
Ql[i] = Q[son[cur][0]][i];
vector<unsigned long long> Qr(
max(Q[son[cur][0]].size(), Q[son[cur][1]].size()), 0);
for (int i = 0; i < (int)Q[son[cur][1]].size(); i++)
Qr[i] = Q[son[cur][1]][i];
vector<unsigned long long> C = convolution(Ql, Qr);
int sz = Q[son[cur][0]].size() + Q[son[cur][1]].size() - 1;
Q[cur].resize(sz);
for (int i = 0; i < sz; i++) Q[cur][i] = C[i];
}
public:
poly_eval_tree(vector<cat> X, cat mod) : X(X), mod(mod) {
son.reserve(2 * X.size() + 10);
son.push_back(vector<int>(2, -1));
Q.reserve(2 * X.size() + 10);
Q.push_back(vector<cat>());
build_tree(0, X.size(), 0);
}
vector<cat> eval(vector<cat> &P, int l, int r, int cur = 0) {
int N = P.size();
if (N <= 1 || son[cur][0] == -1) {
vector<cat> ret(r - l, 0);
for (int j = l; j < r; j++)
for (int i = N - 1; i >= 0; i--)
ret[j - l] = (ret[j - l] * X[j] + P[i]) % mod;
return ret;
}
vector<cat> Pl = poly_rem(P, Q[son[cur][0]], mod);
vector<cat> Pr = poly_rem(P, Q[son[cur][1]], mod);
vector<cat> retl = eval(Pl, l, (l + r) / 2, son[cur][0]);
vector<cat> retr = eval(Pr, (l + r) / 2, r, son[cur][1]);
for (auto it = retr.begin(); it != retr.end(); it++) retl.push_back(*it);
return retl;
}
};
vector<cat> poly_eval_dedup(vector<cat> P, vector<cat> X, cat mod) {
int x = X.size();
vector<pair<cat, int> > Xs(x);
for (int i = 0; i < x; i++) Xs[i] = {X[i], i};
sort(begin(Xs), end(Xs));
vector<cat> Xu;
for (int i = 0; i < x; i++)
if (i == 0 || Xs[i].first != Xs[i - 1].first) Xu.push_back(Xs[i].first);
poly_eval_tree T(Xu, mod);
vector<cat> ret_dedup = T.eval(P, 0, Xu.size());
int a = 0;
vector<cat> ret(x);
for (int i = 0; i < x; i++)
if (i == 0 || Xs[i].first != Xs[i - 1].first) {
for (int j = i; j < x; j++) {
if (Xs[j].first != Xs[i].first) break;
ret[Xs[j].second] = ret_dedup[a];
}
a++;
}
return ret;
}
void DFS(int R, vector<vector<int> > &G, vector<int> &par, vector<cat> &S) {
S[R] = 1;
for (auto it = G[R].begin(); it != G[R].end(); it++)
if (par[*it] == -1) {
par[*it] = R;
DFS(*it, G, par, S);
S[R] += S[*it];
}
}
void DFS2(int R, vector<vector<int> > &G, vector<int> &par, vector<cat> &val,
vector<cat> &sum_val, cat mod) {
sum_val[R] = val[R];
for (auto it = G[R].begin(); it != G[R].end(); it++)
if (par[*it] == R) {
DFS2(*it, G, par, val, sum_val, mod);
sum_val[R] += sum_val[*it];
}
sum_val[R] %= mod;
}
cat pw(cat a, cat e, cat mod) {
if (e <= 0) return 1;
cat x = pw(a, e / 2);
x = x * x % mod;
if (e & 1) x = x * a % mod;
return x;
}
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(10);
int N, K;
cin >> N >> K;
cat mod = 998244353;
if (K == 1) {
cout << 1LL * N * (N - 1) / 2 % mod << "\n";
return 0;
}
vector<vector<int> > G(N);
for (int i = 0; i < N - 1; i++) {
int u, v;
cin >> u >> v;
G[--u].push_back(--v);
G[v].push_back(u);
}
vector<int> par(N, -1);
vector<cat> S(N);
par[0] = 0;
DFS(0, G, par, S);
vector<cat> inv(K + 1, 1);
for (int i = 1; i <= K; i++) inv[i] = inv[i - 1] * pw(i, mod - 2, mod) % mod;
cat fac = 1;
for (int i = 1; i <= K; i++) fac = fac * i % mod;
vector<cat> cntK_down(N, 0), cntK_up(N, 0);
for (int i = 0; i < N; i++) {
vector<cat> V(G[i].size());
for (int j = 0; j < (int)G[i].size(); j++) {
if (par[G[i][j]] == i)
V[j] = S[G[i][j]];
else
V[j] = N - S[i];
}
vector<cat> P = poly_prod(V, K, mod);
int sz = P.size();
vector<unsigned long long> Pu(sz, 0), Iu(sz, 0);
for (int j = 0; j < sz; j++) Pu[j] = P[j];
for (int j = 0; j < sz; j++) Iu[j] = inv[K + 1 - sz + j];
vector<unsigned long long> C = convolution(Pu, Iu);
for (int j = 0; j < sz; j++) P[j] = C[sz - 1 - j];
vector<cat> X(V.size());
for (int j = 0; j < (int)V.size(); j++) X[j] = (mod - V[j]) % mod;
vector<cat> val = poly_eval_dedup(P, X, mod);
for (int j = 0; j < (int)V.size(); j++) {
cat val_cur = val[j];
if (par[G[i][j]] == i)
cntK_up[G[i][j]] = val_cur * fac % mod;
else
cntK_down[i] = val_cur * fac % mod;
}
}
vector<cat> sum_val(N);
DFS2(0, G, par, cntK_down, sum_val, mod);
cat ans = 0;
for (int i = 0; i < N; i++) {
cat sum = 0;
for (auto it = G[i].begin(); it != G[i].end(); it++)
if (par[*it] == i) {
ans += sum_val[*it] * (sum + cntK_up[*it]) % mod;
sum = (sum + sum_val[*it]) % mod;
}
}
ans %= mod;
if (ans < 0) ans += mod;
cout << ans << "\n";
return 0;
}
| ### Prompt
In CPP, your task is to solve the following problem:
You are given a tree of n vertices. You are to select k (not necessarily distinct) simple paths in such a way that it is possible to split all edges of the tree into three sets: edges not contained in any path, edges that are a part of exactly one of these paths, and edges that are parts of all selected paths, and the latter set should be non-empty.
Compute the number of ways to select k paths modulo 998244353.
The paths are enumerated, in other words, two ways are considered distinct if there are such i (1 ≤ i ≤ k) and an edge that the i-th path contains the edge in one way and does not contain it in the other.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 10^{5}) — the number of vertices in the tree and the desired number of paths.
The next n - 1 lines describe edges of the tree. Each line contains two integers a and b (1 ≤ a, b ≤ n, a ≠ b) — the endpoints of an edge. It is guaranteed that the given edges form a tree.
Output
Print the number of ways to select k enumerated not necessarily distinct simple paths in such a way that for each edge either it is not contained in any path, or it is contained in exactly one path, or it is contained in all k paths, and the intersection of all paths is non-empty.
As the answer can be large, print it modulo 998244353.
Examples
Input
3 2
1 2
2 3
Output
7
Input
5 1
4 1
2 3
4 5
2 1
Output
10
Input
29 29
1 2
1 3
1 4
1 5
5 6
5 7
5 8
8 9
8 10
8 11
11 12
11 13
11 14
14 15
14 16
14 17
17 18
17 19
17 20
20 21
20 22
20 23
23 24
23 25
23 26
26 27
26 28
26 29
Output
125580756
Note
In the first example the following ways are valid:
* ((1,2), (1,2)),
* ((1,2), (1,3)),
* ((1,3), (1,2)),
* ((1,3), (1,3)),
* ((1,3), (2,3)),
* ((2,3), (1,3)),
* ((2,3), (2,3)).
In the second example k=1, so all n ⋅ (n - 1) / 2 = 5 ⋅ 4 / 2 = 10 paths are valid.
In the third example, the answer is ≥ 998244353, so it was taken modulo 998244353, don't forget it!
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using cat = long long;
unsigned long long MOD = 998244353LL;
unsigned long long pw(unsigned long long a, unsigned long long e) {
if (e <= 0) return 1;
unsigned long long x = pw(a, e / 2);
x = (x * x) % MOD;
if (e % 2 != 0) x = (x * a) % MOD;
return x;
}
unsigned long long proot = 3;
vector<vector<unsigned long long> > OM;
vector<unsigned long long> DEP;
vector<unsigned long long> DFT(vector<unsigned long long> &A, int N, int d,
int x, int dir) {
vector<unsigned long long> ret(N, 0);
if (N == 1) {
ret[0] = A[x];
return ret;
}
if (N == 2) {
ret[0] = (A[x] + A[x + d]) % MOD;
ret[1] = (A[x] + MOD - A[x + d]) % MOD;
return ret;
}
int dep = DEP[N];
vector<unsigned long long> DFT0[2];
DFT0[0] = DFT(A, N / 2, d * 2, x, dir);
DFT0[1] = DFT(A, N / 2, d * 2, x + d, dir);
for (int i = 0; i < N / 2; i++) {
unsigned long long com = OM[dep][(N + dir * i) % N];
ret[i] = (DFT0[0][i] + com * DFT0[1][i]) % MOD;
ret[i + N / 2] = (DFT0[0][i] + (MOD - com) * DFT0[1][i]) % MOD;
}
return ret;
}
int SN = 1 << 20;
vector<unsigned long long> convolution(vector<unsigned long long> A,
vector<unsigned long long> B) {
if ((int)A.size() < 50) {
vector<unsigned long long> ret(A.size() * 2 - 1);
unsigned long long mod2 = MOD * MOD * 4;
for (int i = 0; i < (int)A.size(); i++)
for (int j = 0; j < (int)B.size(); j++) {
ret[i + j] += A[i] * B[j];
if (ret[i + j] >= mod2) ret[i + j] -= mod2;
}
for (int i = 0; i < (int)ret.size(); i++) ret[i] %= MOD;
return ret;
}
int Smin = 1;
while (Smin < (int)A.size() * 2) Smin *= 2;
if (OM.empty()) {
OM.resize(21);
for (int i = 0; i < 21; i++) {
unsigned long long root = pw(proot, (MOD - 1) / (1 << i));
OM[i].resize(1 << i, 1);
for (int j = 1; j < (1 << i); j++) OM[i][j] = (OM[i][j - 1] * root) % MOD;
reverse(begin(OM[i]) + 1, end(OM[i]));
}
DEP.resize(SN + 1);
for (int i = 1; i <= SN; i++) {
int dep = 0, n = i;
while (n > 1) n /= 2, dep++;
DEP[i] = dep;
}
}
A.resize(Smin, 0);
B.resize(Smin, 0);
vector<unsigned long long> FA = DFT(A, A.size(), 1, 0, 1);
vector<unsigned long long> FB = DFT(B, B.size(), 1, 0, 1);
vector<unsigned long long> Fret(FA.size(), 0);
for (unsigned int i = 0; i < FA.size(); i++) Fret[i] = (FA[i] * FB[i]) % MOD;
vector<unsigned long long> ret = DFT(Fret, Fret.size(), 1, 0, -1);
unsigned long long inv = pw(ret.size(), MOD - 2);
for (unsigned int i = 0; i < ret.size(); i++) ret[i] = (ret[i] * inv) % MOD;
ret.resize(A.size() * 2 - 1);
return ret;
}
vector<cat> poly_prod(vector<cat> V, int K, cat mod) {
int N = V.size();
if (N <= 10) {
vector<cat> ret(min(K + 1, N + 1), 0);
ret[0] = 1;
for (int i = 0; i < N; i++)
for (int j = min(K, i + 1); j > 0; j--)
ret[j] = (ret[j] + ret[j - 1] * V[i]) % mod;
return ret;
}
vector<cat> Vl, Vr;
for (int i = 0; i < N / 2; i++) Vl.push_back(V[i]);
for (int i = N / 2; i < N; i++) Vr.push_back(V[i]);
vector<cat> Pl = poly_prod(Vl, K, mod), Pr = poly_prod(Vr, K, mod);
vector<unsigned long long> Rl(min(K + 1, (int)max(Pl.size(), Pr.size())), 0);
vector<unsigned long long> Rr(min(K + 1, (int)max(Pl.size(), Pr.size())), 0);
for (int i = 0; i < min(K + 1, (int)Pl.size()); i++) Rl[i] = Pl[i];
for (int i = 0; i < min(K + 1, (int)Pr.size()); i++) Rr[i] = Pr[i];
vector<unsigned long long> R = convolution(Rl, Rr);
vector<cat> ret(min(K + 1, N + 1), 0);
for (int i = 0; i < min(K + 1, N + 1); i++) ret[i] = R[i];
return ret;
}
vector<cat> poly_inv(vector<cat> &P, int deg, cat mod) {
int deg_p2 = deg;
while (deg_p2 & (deg_p2 - 1)) deg_p2++;
vector<unsigned long long> A(1, 1);
for (int l = 1; l < deg_p2; l *= 2) {
vector<unsigned long long> L(l), U(l);
for (int i = 0; i < min((int)P.size(), l); i++) L[i] = P[i];
vector<unsigned long long> C = convolution(L, A);
C.push_back(0);
for (int i = 0; i < l; i++) C[i] = C[i + l];
C.resize(l);
for (int i = l; i < min((int)P.size(), 2 * l); i++) U[i - l] = P[i];
vector<unsigned long long> R = convolution(A, U);
R.resize(l, 0);
for (int i = 0; i < l; i++) R[i] = (R[i] + C[i]) % mod;
vector<unsigned long long> B = convolution(R, A);
A.resize(2 * l, 0);
for (int i = 0; i < l; i++) A[i + l] = (mod - B[i]) % mod;
}
vector<cat> ret(deg);
for (int i = 0; i < deg; i++) ret[i] = A[i];
return ret;
}
vector<cat> poly_rem(vector<cat> &P, vector<cat> Q, cat mod) {
if (P.size() < Q.size()) return P;
int deg = P.size() - Q.size() + 1;
vector<cat> Pi = P;
reverse(begin(Pi), end(Pi));
Pi.resize(min((int)Pi.size(), deg));
vector<cat> Qi = Q;
reverse(begin(Qi), end(Qi));
vector<cat> Qii = poly_inv(Qi, deg, mod);
vector<unsigned long long> pi(max(Pi.size(), Qii.size()), 0);
for (int i = 0; i < (int)Pi.size(); i++) pi[i] = Pi[i];
vector<unsigned long long> qii(max(Pi.size(), Qii.size()), 0);
for (int i = 0; i < (int)Qii.size(); i++) qii[i] = Qii[i];
vector<unsigned long long> D = convolution(pi, qii);
D.resize(deg, 0);
reverse(begin(D), end(D));
D.resize(max(Q.size(), D.size()), 0);
vector<unsigned long long> q(max(Q.size(), D.size()), 0);
for (int i = 0; i < (int)Q.size(); i++) q[i] = Q[i];
vector<unsigned long long> sub = convolution(q, D);
while (sub.size() > P.size() && sub.back() == 0) sub.pop_back();
vector<cat> ret = P;
for (int i = 0; i < (int)sub.size(); i++) {
ret[i] = (ret[i] - (cat)sub[i]) % mod;
if (ret[i] < 0) ret[i] += mod;
}
while ((int)ret.size() > 1 && ret.back() == 0) ret.pop_back();
assert(ret.size() < Q.size());
return ret;
}
class poly_eval_tree {
vector<cat> X;
vector<vector<cat> > Q;
vector<vector<int> > son;
cat mod;
void build_tree(int l, int r, int cur) {
if (r - l <= 2) {
Q[cur].resize(r - l + 1, 0);
Q[cur][0] = 1;
for (int i = l; i < r; i++)
for (int j = i - l + 1; j >= 0; j--)
Q[cur][j] = (((j == 0) ? 0 : Q[cur][j - 1]) - X[i] * Q[cur][j]) % mod;
for (int i = 0; i <= r - l; i++)
if (Q[cur][i] < 0) Q[cur][i] += mod;
return;
}
son[cur][0] = son.size();
son.push_back(vector<int>(2, -1));
Q.push_back(vector<cat>());
build_tree(l, (l + r) / 2, son[cur][0]);
son[cur][1] = son.size();
son.push_back(vector<int>(2, -1));
Q.push_back(vector<cat>());
build_tree((l + r) / 2, r, son[cur][1]);
vector<unsigned long long> Ql(
max(Q[son[cur][0]].size(), Q[son[cur][1]].size()), 0);
for (int i = 0; i < (int)Q[son[cur][0]].size(); i++)
Ql[i] = Q[son[cur][0]][i];
vector<unsigned long long> Qr(
max(Q[son[cur][0]].size(), Q[son[cur][1]].size()), 0);
for (int i = 0; i < (int)Q[son[cur][1]].size(); i++)
Qr[i] = Q[son[cur][1]][i];
vector<unsigned long long> C = convolution(Ql, Qr);
int sz = Q[son[cur][0]].size() + Q[son[cur][1]].size() - 1;
Q[cur].resize(sz);
for (int i = 0; i < sz; i++) Q[cur][i] = C[i];
}
public:
poly_eval_tree(vector<cat> X, cat mod) : X(X), mod(mod) {
son.reserve(2 * X.size() + 10);
son.push_back(vector<int>(2, -1));
Q.reserve(2 * X.size() + 10);
Q.push_back(vector<cat>());
build_tree(0, X.size(), 0);
}
vector<cat> eval(vector<cat> &P, int l, int r, int cur = 0) {
int N = P.size();
if (N <= 1 || son[cur][0] == -1) {
vector<cat> ret(r - l, 0);
for (int j = l; j < r; j++)
for (int i = N - 1; i >= 0; i--)
ret[j - l] = (ret[j - l] * X[j] + P[i]) % mod;
return ret;
}
vector<cat> Pl = poly_rem(P, Q[son[cur][0]], mod);
vector<cat> Pr = poly_rem(P, Q[son[cur][1]], mod);
vector<cat> retl = eval(Pl, l, (l + r) / 2, son[cur][0]);
vector<cat> retr = eval(Pr, (l + r) / 2, r, son[cur][1]);
for (auto it = retr.begin(); it != retr.end(); it++) retl.push_back(*it);
return retl;
}
};
vector<cat> poly_eval_dedup(vector<cat> P, vector<cat> X, cat mod) {
int x = X.size();
vector<pair<cat, int> > Xs(x);
for (int i = 0; i < x; i++) Xs[i] = {X[i], i};
sort(begin(Xs), end(Xs));
vector<cat> Xu;
for (int i = 0; i < x; i++)
if (i == 0 || Xs[i].first != Xs[i - 1].first) Xu.push_back(Xs[i].first);
poly_eval_tree T(Xu, mod);
vector<cat> ret_dedup = T.eval(P, 0, Xu.size());
int a = 0;
vector<cat> ret(x);
for (int i = 0; i < x; i++)
if (i == 0 || Xs[i].first != Xs[i - 1].first) {
for (int j = i; j < x; j++) {
if (Xs[j].first != Xs[i].first) break;
ret[Xs[j].second] = ret_dedup[a];
}
a++;
}
return ret;
}
void DFS(int R, vector<vector<int> > &G, vector<int> &par, vector<cat> &S) {
S[R] = 1;
for (auto it = G[R].begin(); it != G[R].end(); it++)
if (par[*it] == -1) {
par[*it] = R;
DFS(*it, G, par, S);
S[R] += S[*it];
}
}
void DFS2(int R, vector<vector<int> > &G, vector<int> &par, vector<cat> &val,
vector<cat> &sum_val, cat mod) {
sum_val[R] = val[R];
for (auto it = G[R].begin(); it != G[R].end(); it++)
if (par[*it] == R) {
DFS2(*it, G, par, val, sum_val, mod);
sum_val[R] += sum_val[*it];
}
sum_val[R] %= mod;
}
cat pw(cat a, cat e, cat mod) {
if (e <= 0) return 1;
cat x = pw(a, e / 2);
x = x * x % mod;
if (e & 1) x = x * a % mod;
return x;
}
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(10);
int N, K;
cin >> N >> K;
cat mod = 998244353;
if (K == 1) {
cout << 1LL * N * (N - 1) / 2 % mod << "\n";
return 0;
}
vector<vector<int> > G(N);
for (int i = 0; i < N - 1; i++) {
int u, v;
cin >> u >> v;
G[--u].push_back(--v);
G[v].push_back(u);
}
vector<int> par(N, -1);
vector<cat> S(N);
par[0] = 0;
DFS(0, G, par, S);
vector<cat> inv(K + 1, 1);
for (int i = 1; i <= K; i++) inv[i] = inv[i - 1] * pw(i, mod - 2, mod) % mod;
cat fac = 1;
for (int i = 1; i <= K; i++) fac = fac * i % mod;
vector<cat> cntK_down(N, 0), cntK_up(N, 0);
for (int i = 0; i < N; i++) {
vector<cat> V(G[i].size());
for (int j = 0; j < (int)G[i].size(); j++) {
if (par[G[i][j]] == i)
V[j] = S[G[i][j]];
else
V[j] = N - S[i];
}
vector<cat> P = poly_prod(V, K, mod);
int sz = P.size();
vector<unsigned long long> Pu(sz, 0), Iu(sz, 0);
for (int j = 0; j < sz; j++) Pu[j] = P[j];
for (int j = 0; j < sz; j++) Iu[j] = inv[K + 1 - sz + j];
vector<unsigned long long> C = convolution(Pu, Iu);
for (int j = 0; j < sz; j++) P[j] = C[sz - 1 - j];
vector<cat> X(V.size());
for (int j = 0; j < (int)V.size(); j++) X[j] = (mod - V[j]) % mod;
vector<cat> val = poly_eval_dedup(P, X, mod);
for (int j = 0; j < (int)V.size(); j++) {
cat val_cur = val[j];
if (par[G[i][j]] == i)
cntK_up[G[i][j]] = val_cur * fac % mod;
else
cntK_down[i] = val_cur * fac % mod;
}
}
vector<cat> sum_val(N);
DFS2(0, G, par, cntK_down, sum_val, mod);
cat ans = 0;
for (int i = 0; i < N; i++) {
cat sum = 0;
for (auto it = G[i].begin(); it != G[i].end(); it++)
if (par[*it] == i) {
ans += sum_val[*it] * (sum + cntK_up[*it]) % mod;
sum = (sum + sum_val[*it]) % mod;
}
}
ans %= mod;
if (ans < 0) ans += mod;
cout << ans << "\n";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
struct node {
int t, next;
} a[200010];
long long DCXISSOHANDSOME[20][262144], inv[262145], fac[100010], ifac[100010],
f[100010], g[100010], ans;
int head[100010], size[100010], h[100010], n, k, tt, tot;
inline int rd() {
int x = 0;
char ch = getchar();
for (; ch < '0' || ch > '9'; ch = getchar())
;
for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
return x;
}
inline void add(int x, int y) {
a[++tot].t = y;
a[tot].next = head[x];
head[x] = tot;
}
inline long long pls(const long long x, const long long y) {
return (x + y < 998244353LL) ? x + y : x + y - 998244353LL;
}
inline long long mns(const long long x, const long long y) {
return (x - y < 0) ? x - y + 998244353LL : x - y;
}
inline long long ksm(long long x, long long y) {
long long res = 1;
for (; y; y >>= 1, x = x * x % 998244353LL)
if (y & 1) res = res * x % 998244353LL;
return res;
}
inline void pre_gao() {
fac[0] = 1;
for (int i = 1; i <= 100000; i++) fac[i] = fac[i - 1] * i % 998244353LL;
ifac[100000] = ksm(fac[100000], 998244353LL - 2);
for (int i = 99999; ~i; i--) ifac[i] = ifac[i + 1] * (i + 1) % 998244353LL;
inv[1] = 1;
for (int i = 2; i <= 262144; i++)
inv[i] =
(998244353LL - 998244353LL / i) * inv[998244353LL % i] % 998244353LL;
for (int w = 2, hh = 1; w <= 262144; w <<= 1, hh++) {
long long now = ksm(3, (998244353LL - 1) / w);
DCXISSOHANDSOME[hh][0] = 1;
for (int j = 1; j < (w >> 1); j++)
DCXISSOHANDSOME[hh][j] = DCXISSOHANDSOME[hh][j - 1] * now % 998244353LL;
}
}
inline void ntt(long long *a, int n, int on) {
static int rev[262144];
for (int i = 1; i < n; i++)
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) ? (n >> 1) : 0);
for (int i = 1; i < n; i++)
if (i < rev[i]) swap(a[i], a[rev[i]]);
for (int w = 2, hh = 1; w <= n; w <<= 1, hh++)
for (int k = 0; k < n; k += w)
for (int j = k; j < k + (w >> 1); j++) {
long long u = a[j], t = a[j + (w >> 1)] * DCXISSOHANDSOME[hh][j - k] %
998244353LL;
a[j] = pls(u, t);
a[j + (w >> 1)] = mns(u, t);
}
if (on == -1) {
reverse(a + 1, a + n);
for (int i = 0; i < n; i++) a[i] = a[i] * inv[n] % 998244353LL;
}
}
inline long long *gao(int l, int r) {
static long long c[262144], d[262144];
long long *res = new long long[r - l + 2];
if (l == r) {
res[0] = 1;
res[1] = h[l];
return res;
}
int mid = (l + r) >> 1;
long long *h1 = gao(l, mid), *h2 = gao(mid + 1, r);
int len1 = mid - l + 1, len2 = r - mid;
int len = 1;
for (; len <= len1 + len2; len <<= 1)
;
memcpy(c, h1, sizeof(long long) * (len1 + 1));
memcpy(d, h2, sizeof(long long) * (len2 + 1));
memset(c + len1 + 1, 0, sizeof(long long) * (len - len1 - 1));
memset(d + len2 + 1, 0, sizeof(long long) * (len - len2 - 1));
ntt(c, len, 1);
ntt(d, len, 1);
for (int i = 0; i < len; i++) c[i] = c[i] * d[i] % 998244353LL;
ntt(c, len, -1);
memcpy(res, c, sizeof(long long) * (r - l + 2));
delete[] h1;
delete[] h2;
return res;
}
inline void dfs(int x, int y) {
static long long c[262144], d[262144];
g[x] = 0;
size[x] = 1;
for (int i = head[x]; i; i = a[i].next) {
int t = a[i].t;
if (t == y) continue;
dfs(t, x);
size[x] += size[t];
ans = pls(ans, g[x] * g[t] % 998244353LL);
g[x] = pls(g[x], g[t]);
}
tt = 0;
for (int i = head[x]; i; i = a[i].next)
if (a[i].t != y) h[++tt] = size[a[i].t];
if (!tt) {
f[x] = g[x] = 1;
return;
}
long long *res = gao(1, tt);
for (int i = 0; i <= min(k, tt); i++)
f[x] = pls(f[x], res[i] * fac[k] % 998244353LL * ifac[k - i] % 998244353LL);
g[x] = pls(g[x], f[x]);
int m = tt;
sort(h + 1, h + m + 1);
m = unique(h + 1, h + m + 1) - h - 1;
for (int i = tt; i; i--)
c[i] = pls(res[i - 1] * (n - size[x]) % 998244353LL, res[i]);
c[0] = res[0];
for (int hhh = 1; hhh <= m; hhh++) {
d[0] = 1;
for (int i = 1; i <= tt; i++)
d[i] = mns(c[i], d[i - 1] * h[hhh] % 998244353LL);
long long now = 0;
for (int i = 0; i <= min(k, tt); i++)
now = pls(now, d[i] * fac[k] % 998244353LL * ifac[k - i] % 998244353LL);
for (int i = head[x]; i; i = a[i].next)
if (a[i].t != y && size[a[i].t] == h[hhh])
ans = pls(ans, g[a[i].t] * now % 998244353LL);
}
}
int main() {
n = rd();
k = rd();
pre_gao();
tot = 0;
if (k == 1) {
printf("%I64d\n", (long long)n * (n - 1) / 2 % 998244353LL);
return 0;
}
for (int i = 1; i < n; i++) {
int x = rd(), y = rd();
add(x, y);
add(y, x);
}
ans = 0;
dfs(1, 0);
printf("%I64d\n", ans);
return 0;
}
| ### Prompt
Develop a solution in cpp to the problem described below:
You are given a tree of n vertices. You are to select k (not necessarily distinct) simple paths in such a way that it is possible to split all edges of the tree into three sets: edges not contained in any path, edges that are a part of exactly one of these paths, and edges that are parts of all selected paths, and the latter set should be non-empty.
Compute the number of ways to select k paths modulo 998244353.
The paths are enumerated, in other words, two ways are considered distinct if there are such i (1 ≤ i ≤ k) and an edge that the i-th path contains the edge in one way and does not contain it in the other.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 10^{5}) — the number of vertices in the tree and the desired number of paths.
The next n - 1 lines describe edges of the tree. Each line contains two integers a and b (1 ≤ a, b ≤ n, a ≠ b) — the endpoints of an edge. It is guaranteed that the given edges form a tree.
Output
Print the number of ways to select k enumerated not necessarily distinct simple paths in such a way that for each edge either it is not contained in any path, or it is contained in exactly one path, or it is contained in all k paths, and the intersection of all paths is non-empty.
As the answer can be large, print it modulo 998244353.
Examples
Input
3 2
1 2
2 3
Output
7
Input
5 1
4 1
2 3
4 5
2 1
Output
10
Input
29 29
1 2
1 3
1 4
1 5
5 6
5 7
5 8
8 9
8 10
8 11
11 12
11 13
11 14
14 15
14 16
14 17
17 18
17 19
17 20
20 21
20 22
20 23
23 24
23 25
23 26
26 27
26 28
26 29
Output
125580756
Note
In the first example the following ways are valid:
* ((1,2), (1,2)),
* ((1,2), (1,3)),
* ((1,3), (1,2)),
* ((1,3), (1,3)),
* ((1,3), (2,3)),
* ((2,3), (1,3)),
* ((2,3), (2,3)).
In the second example k=1, so all n ⋅ (n - 1) / 2 = 5 ⋅ 4 / 2 = 10 paths are valid.
In the third example, the answer is ≥ 998244353, so it was taken modulo 998244353, don't forget it!
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
struct node {
int t, next;
} a[200010];
long long DCXISSOHANDSOME[20][262144], inv[262145], fac[100010], ifac[100010],
f[100010], g[100010], ans;
int head[100010], size[100010], h[100010], n, k, tt, tot;
inline int rd() {
int x = 0;
char ch = getchar();
for (; ch < '0' || ch > '9'; ch = getchar())
;
for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
return x;
}
inline void add(int x, int y) {
a[++tot].t = y;
a[tot].next = head[x];
head[x] = tot;
}
inline long long pls(const long long x, const long long y) {
return (x + y < 998244353LL) ? x + y : x + y - 998244353LL;
}
inline long long mns(const long long x, const long long y) {
return (x - y < 0) ? x - y + 998244353LL : x - y;
}
inline long long ksm(long long x, long long y) {
long long res = 1;
for (; y; y >>= 1, x = x * x % 998244353LL)
if (y & 1) res = res * x % 998244353LL;
return res;
}
inline void pre_gao() {
fac[0] = 1;
for (int i = 1; i <= 100000; i++) fac[i] = fac[i - 1] * i % 998244353LL;
ifac[100000] = ksm(fac[100000], 998244353LL - 2);
for (int i = 99999; ~i; i--) ifac[i] = ifac[i + 1] * (i + 1) % 998244353LL;
inv[1] = 1;
for (int i = 2; i <= 262144; i++)
inv[i] =
(998244353LL - 998244353LL / i) * inv[998244353LL % i] % 998244353LL;
for (int w = 2, hh = 1; w <= 262144; w <<= 1, hh++) {
long long now = ksm(3, (998244353LL - 1) / w);
DCXISSOHANDSOME[hh][0] = 1;
for (int j = 1; j < (w >> 1); j++)
DCXISSOHANDSOME[hh][j] = DCXISSOHANDSOME[hh][j - 1] * now % 998244353LL;
}
}
inline void ntt(long long *a, int n, int on) {
static int rev[262144];
for (int i = 1; i < n; i++)
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) ? (n >> 1) : 0);
for (int i = 1; i < n; i++)
if (i < rev[i]) swap(a[i], a[rev[i]]);
for (int w = 2, hh = 1; w <= n; w <<= 1, hh++)
for (int k = 0; k < n; k += w)
for (int j = k; j < k + (w >> 1); j++) {
long long u = a[j], t = a[j + (w >> 1)] * DCXISSOHANDSOME[hh][j - k] %
998244353LL;
a[j] = pls(u, t);
a[j + (w >> 1)] = mns(u, t);
}
if (on == -1) {
reverse(a + 1, a + n);
for (int i = 0; i < n; i++) a[i] = a[i] * inv[n] % 998244353LL;
}
}
inline long long *gao(int l, int r) {
static long long c[262144], d[262144];
long long *res = new long long[r - l + 2];
if (l == r) {
res[0] = 1;
res[1] = h[l];
return res;
}
int mid = (l + r) >> 1;
long long *h1 = gao(l, mid), *h2 = gao(mid + 1, r);
int len1 = mid - l + 1, len2 = r - mid;
int len = 1;
for (; len <= len1 + len2; len <<= 1)
;
memcpy(c, h1, sizeof(long long) * (len1 + 1));
memcpy(d, h2, sizeof(long long) * (len2 + 1));
memset(c + len1 + 1, 0, sizeof(long long) * (len - len1 - 1));
memset(d + len2 + 1, 0, sizeof(long long) * (len - len2 - 1));
ntt(c, len, 1);
ntt(d, len, 1);
for (int i = 0; i < len; i++) c[i] = c[i] * d[i] % 998244353LL;
ntt(c, len, -1);
memcpy(res, c, sizeof(long long) * (r - l + 2));
delete[] h1;
delete[] h2;
return res;
}
inline void dfs(int x, int y) {
static long long c[262144], d[262144];
g[x] = 0;
size[x] = 1;
for (int i = head[x]; i; i = a[i].next) {
int t = a[i].t;
if (t == y) continue;
dfs(t, x);
size[x] += size[t];
ans = pls(ans, g[x] * g[t] % 998244353LL);
g[x] = pls(g[x], g[t]);
}
tt = 0;
for (int i = head[x]; i; i = a[i].next)
if (a[i].t != y) h[++tt] = size[a[i].t];
if (!tt) {
f[x] = g[x] = 1;
return;
}
long long *res = gao(1, tt);
for (int i = 0; i <= min(k, tt); i++)
f[x] = pls(f[x], res[i] * fac[k] % 998244353LL * ifac[k - i] % 998244353LL);
g[x] = pls(g[x], f[x]);
int m = tt;
sort(h + 1, h + m + 1);
m = unique(h + 1, h + m + 1) - h - 1;
for (int i = tt; i; i--)
c[i] = pls(res[i - 1] * (n - size[x]) % 998244353LL, res[i]);
c[0] = res[0];
for (int hhh = 1; hhh <= m; hhh++) {
d[0] = 1;
for (int i = 1; i <= tt; i++)
d[i] = mns(c[i], d[i - 1] * h[hhh] % 998244353LL);
long long now = 0;
for (int i = 0; i <= min(k, tt); i++)
now = pls(now, d[i] * fac[k] % 998244353LL * ifac[k - i] % 998244353LL);
for (int i = head[x]; i; i = a[i].next)
if (a[i].t != y && size[a[i].t] == h[hhh])
ans = pls(ans, g[a[i].t] * now % 998244353LL);
}
}
int main() {
n = rd();
k = rd();
pre_gao();
tot = 0;
if (k == 1) {
printf("%I64d\n", (long long)n * (n - 1) / 2 % 998244353LL);
return 0;
}
for (int i = 1; i < n; i++) {
int x = rd(), y = rd();
add(x, y);
add(y, x);
}
ans = 0;
dfs(1, 0);
printf("%I64d\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
template <typename _Tp>
void read(_Tp &x) {
char ch(getchar());
bool f(false);
while (!isdigit(ch)) f |= ch == 45, ch = getchar();
x = ch & 15, ch = getchar();
while (isdigit(ch)) x = x * 10 + (ch & 15), ch = getchar();
if (f) x = -x;
}
template <typename _Tp, typename... Args>
void read(_Tp &t, Args &...args) {
read(t);
read(args...);
}
template <typename _Tp>
inline void chmax(_Tp &a, const _Tp &b) {
a = a < b ? b : a;
}
const int max_len = 1 << 18, N = max_len + 5, mod = 998244353, inf = 0x3f3f3f3f;
template <typename _Tp1, typename _Tp2>
inline void add(_Tp1 &a, _Tp2 b) {
(a += b) >= mod && (a -= mod);
}
template <typename _Tp1, typename _Tp2>
inline void sub(_Tp1 &a, _Tp2 b) {
(a -= b) < 0 && (a += mod);
}
template <typename _Tp>
inline _Tp _sub(_Tp a, const _Tp &b) {
(a += mod - b) >= mod && (a -= mod);
return a;
}
long long ksm(long long a, long long b = mod - 2) {
long long res = 1;
while (b) {
if (b & 1) res = res * a % mod;
a = a * a % mod, b >>= 1;
}
return res;
}
void print(const std::vector<int> &a) {
for (auto it : a) printf("%d ", it);
printf("\n");
}
inline std::vector<int> operator<<(std::vector<int> a, unsigned int b) {
return a.insert(a.begin(), b, 0), a;
}
inline std::vector<int> operator<<=(std::vector<int> &a, unsigned int b) {
return a.insert(a.begin(), b, 0), a;
}
inline std::vector<int> operator>>(const std::vector<int> &a, unsigned int b) {
return b >= a.size() ? std::vector<int>()
: std::vector<int>{a.begin() + b, a.end()};
}
inline std::vector<int> operator>>=(std::vector<int> &a, unsigned int b) {
return a = b >= a.size() ? std::vector<int>()
: std::vector<int>{a.begin() + b, a.end()};
}
std::vector<int> operator+=(std::vector<int> &a, const std::vector<int> &b) {
if (b.size() > a.size()) a.resize(b.size());
for (unsigned int i = 0; i < b.size(); ++i) add(a[i], b[i]);
return a;
}
inline std::vector<int> operator+(const std::vector<int> &a,
const std::vector<int> &b) {
std::vector<int> tmp(a);
tmp += b;
return tmp;
}
std::vector<int> operator-=(std::vector<int> &a, const std::vector<int> &b) {
if (b.size() > a.size()) a.resize(b.size());
for (unsigned int i = 0; i < b.size(); ++i) sub(a[i], b[i]);
return a;
}
inline std::vector<int> operator-(const std::vector<int> &a,
const std::vector<int> &b) {
std::vector<int> tmp(a);
tmp -= b;
return tmp;
}
const unsigned long long Omg = ksm(3, (mod - 1) / max_len);
unsigned long long Omgs[N];
void setup() {
Omgs[max_len / 2] = 1;
for (int i = max_len / 2 + 1; i < max_len; ++i)
Omgs[i] = Omgs[i - 1] * Omg % mod;
for (int i = max_len / 2 - 1; i > 0; --i) Omgs[i] = Omgs[i << 1];
}
unsigned int rev[N];
unsigned int getlen(unsigned int len) {
unsigned int limit = 1;
while (limit < len) limit <<= 1;
for (unsigned int i = 0; i < limit; ++i)
rev[i] = (rev[i >> 1] >> 1) | (i & 1 ? limit >> 1 : 0);
return limit;
}
void dft(unsigned long long *A, unsigned int limit) {
for (unsigned int i = 0; i < limit; ++i)
if (i < rev[i]) std::swap(A[i], A[rev[i]]);
for (unsigned int len = 1; len < limit; len <<= 1) {
if (len == 262144u)
for (unsigned int i = 0; i < limit; ++i) A[i] %= mod;
for (unsigned int i = 0; i < limit; i += len << 1) {
unsigned long long *p = A + i, *q = A + i + len, *w = Omgs + len;
for (unsigned int j = 0; j < len; ++j, ++p, ++q, ++w) {
const unsigned long long tp = *q * *w % mod;
*q = *p + mod - tp, *p += tp;
}
}
}
for (unsigned int i = 0; i < limit; ++i) A[i] %= mod;
}
void idft(unsigned long long *A, unsigned int limit) {
std::reverse(A + 1, A + limit), dft(A, limit);
unsigned long long inv = mod - (mod - 1) / limit;
for (unsigned int i = 0; i < limit; ++i) A[i] = A[i] * inv % mod;
}
unsigned long long _f[N], _g[N], _tp[129];
std::vector<int> operator*(const std::vector<int> &a,
const std::vector<int> &b) {
unsigned int len = a.size() + b.size() - 1;
if (a.size() <= 64u || b.size() <= 64u) {
memset(_tp, 0, len << 3);
unsigned int r = 0;
for (unsigned int i = 0; i < a.size(); ++i) {
for (unsigned int j = 0; j < b.size(); ++j)
_tp[i + j] += 1ULL * a[i] * b[j];
if (++r == 18) {
r = 0;
for (unsigned int j = i - 17; j < i + b.size(); ++j) _tp[j] %= mod;
}
}
if (r)
for (unsigned int j = 0; j < len; ++j) _tp[j] %= mod;
std::vector<int> c(len);
for (unsigned int i = 0; i < len; ++i) c[i] = _tp[i];
return c;
}
unsigned int limit = getlen(len);
memset(_f + a.size(), 0, (limit - a.size()) << 3);
for (unsigned int i = 0; i < a.size(); ++i) _f[i] = a[i];
memset(_g + b.size(), 0, (limit - b.size()) << 3);
for (unsigned int i = 0; i < b.size(); ++i) _g[i] = b[i];
dft(_f, limit), dft(_g, limit);
for (unsigned int i = 0; i < limit; ++i) _f[i] = _f[i] * _g[i] % mod;
idft(_f, limit);
std::vector<int> ans(len);
for (unsigned int i = 0; i < len; ++i) ans[i] = _f[i];
return ans;
}
std::vector<int> mul(const std::vector<int> &a, const std::vector<int> &b,
unsigned int len, bool need = true) {
if (a.size() <= 64u || b.size() <= 64u) {
memset(_tp, 0, len << 3);
unsigned int r = 0;
for (unsigned int i = 0; i < a.size(); ++i) {
for (unsigned int j = 0; j < b.size() && i + j < len; ++j)
_tp[i + j] += 1ULL * a[i] * b[j];
if (++r == 18) {
r = 0;
for (unsigned int j = i - 17; j < len && j < i + b.size(); ++j)
_tp[j] %= mod;
}
}
if (r)
for (unsigned int j = 0; j < len; ++j) _tp[j] %= mod;
std::vector<int> c(len);
for (unsigned int i = 0; i < len; ++i) c[i] = _tp[i];
return c;
}
int limit = getlen(len);
memset(_f + a.size(), 0, (limit - a.size()) << 3);
for (unsigned int i = 0; i < a.size(); ++i) _f[i] = a[i];
dft(_f, limit);
if (need) {
memset(_g + b.size(), 0, (limit - b.size()) << 3);
for (unsigned int i = 0; i < b.size(); ++i) _g[i] = b[i];
dft(_g, limit);
}
for (int i = 0; i < limit; ++i) _f[i] = 1ULL * _f[i] * _g[i] % mod;
idft(_f, limit);
std::vector<int> ans(len);
for (unsigned int i = 0; i < len; ++i) ans[i] = _f[i];
return ans;
}
std::vector<int> mulT(const std::vector<int> &a, const std::vector<int> &b) {
if (a.size() <= 64u || b.size() <= 64u) {
std::vector<int> c(a.size() - b.size() + 1);
for (unsigned int i = 0; i < c.size(); ++i)
for (unsigned int j = 0; j < b.size(); ++j)
c[i] = (c[i] + 1ULL * a[i + j] * b[j]) % mod;
return c;
}
int limit = getlen(a.size());
memset(_f, 0, limit << 3);
for (unsigned int i = 0; i < a.size(); ++i) _f[i] = a[i];
memset(_g, 0, limit << 3);
for (unsigned int i = 0; i < b.size(); ++i) _g[i] = b[i];
std::reverse(_g, _g + b.size()), dft(_f, limit), dft(_g, limit);
for (int i = 0; i < limit; ++i) _f[i] = 1ULL * _f[i] * _g[i] % mod;
idft(_f, limit);
std::vector<int> ans(a.size() - b.size() + 1);
for (unsigned int i = b.size() - 1; i < a.size(); ++i)
ans[i - b.size() + 1] = _f[i];
return ans;
}
inline std::vector<int> operator*=(std::vector<int> &a,
const std::vector<int> &b) {
return a = a * b;
}
template <typename _Tp>
inline std::vector<int> operator*=(std::vector<int> &a, const _Tp &b) {
for (auto &&it : a) it = 1ULL * it * b % mod;
return a;
}
template <typename _Tp>
inline std::vector<int> operator*(std::vector<int> a, const _Tp &b) {
return a *= b;
}
template <typename _Tp>
inline std::vector<int> operator*(const _Tp &b, std::vector<int> a) {
return a *= b;
}
template <typename _Tp>
inline std::vector<int> operator/=(std::vector<int> &a, const _Tp &b) {
unsigned long long inv = ksm(b);
for (auto &&it : a) it = 1ULL * it * inv % mod;
return a;
}
template <typename _Tp>
inline std::vector<int> operator/(std::vector<int> a, const _Tp &b) {
return a /= b;
}
long long inv[N], ifac[N];
std::vector<int> e[N], w[N];
int siz[N], n, k, a[N], _[N];
std::vector<int> f[N << 1];
void sol1(int l, int r, int u) {
if (l == r) return f[u] = {1, a[l]}, void();
int mid = (l + r) >> 1;
sol1(l, mid, u << 1), sol1(mid + 1, r, u << 1 | 1);
f[u] = f[u << 1] * f[u << 1 | 1];
}
void sol2(int l, int r, int u, const std::vector<int> &g) {
if (l == r) return _[l] = g[0], void();
int mid = (l + r) >> 1;
std::vector<int> tp = mulT(g, f[u << 1 | 1]);
if (((int)tp.size()) > mid - l + 1) tp.resize(mid - l + 1);
sol2(l, mid, u << 1, tp);
tp = mulT(g, f[u << 1]);
if (((int)tp.size()) > r - mid) tp.resize(r - mid);
sol2(mid + 1, r, u << 1 | 1, tp);
}
void ___(int x, int fa) {
std::sort(e[x].begin(), e[x].end());
siz[x] = 1;
for (auto v : e[x])
if (v != fa) ___(v, x), siz[x] += siz[v];
int pos = 0;
for (auto v : e[x]) a[pos++] = v == fa ? n - siz[x] : siz[v];
sol1(0, pos - 1, 1);
std::vector<int> A(pos + 1);
for (int i = 0; i <= pos; ++i) A[i] = k - i > 0 ? ifac[k - i] : 0;
sol2(0, pos - 1, 1, A);
w[x].resize(((int)e[x].size()));
for (int i = 0; i < ((int)e[x].size()); ++i) w[x][i] = _[i];
}
bool vis[N];
int S, mn, rt;
int getsize(int x, int fa) {
int siz = 1;
for (auto v : e[x])
if (v != fa && !vis[v]) siz += getsize(v, x);
return siz;
}
void getrt(int x, int fa) {
int tmp = 0;
siz[x] = 1;
for (auto v : e[x])
if (v != fa && !vis[v]) getrt(v, x), chmax(tmp, siz[v]), siz[x] += siz[v];
chmax(tmp, S - siz[x]);
if (tmp < mn) mn = tmp, rt = x;
}
int QAQ, ans;
void dfs1(int x, int fa) {
add(QAQ, w[x][std::lower_bound(e[x].begin(), e[x].end(), fa) - e[x].begin()]);
for (auto v : e[x])
if (v != fa && !vis[v]) dfs1(v, x);
}
void dfs(int x) {
S = getsize(x, 0), mn = inf, getrt(x, 0), vis[x = rt] = true;
int sum = 0;
for (auto v : e[x])
if (!vis[v])
QAQ = 0, dfs1(v, x),
add(ans, 1LL * QAQ *
w[x][std::lower_bound(e[x].begin(), e[x].end(), v) -
e[x].begin()] %
mod),
add(ans, 1LL * sum * QAQ % mod), add(sum, QAQ);
for (auto v : e[x])
if (!vis[v]) dfs(v);
}
int main() {
inv[0] = inv[1] = ifac[0] = ifac[1] = 1;
for (int i = 2; i < N; ++i)
inv[i] = inv[mod % i] * (mod - mod / i) % mod,
ifac[i] = ifac[i - 1] * inv[i] % mod;
setup();
read(n, k);
if (n == 1) return puts("0"), 0;
for (int i = 1, x, y; i < n; ++i)
read(x, y), e[x].push_back(y), e[y].push_back(x);
___(1, 0), dfs(1);
for (int i = 1; i <= k; ++i) ans = 1LL * ans * i % mod * i % mod;
printf("%d\n", ans);
return 0;
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
You are given a tree of n vertices. You are to select k (not necessarily distinct) simple paths in such a way that it is possible to split all edges of the tree into three sets: edges not contained in any path, edges that are a part of exactly one of these paths, and edges that are parts of all selected paths, and the latter set should be non-empty.
Compute the number of ways to select k paths modulo 998244353.
The paths are enumerated, in other words, two ways are considered distinct if there are such i (1 ≤ i ≤ k) and an edge that the i-th path contains the edge in one way and does not contain it in the other.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 10^{5}) — the number of vertices in the tree and the desired number of paths.
The next n - 1 lines describe edges of the tree. Each line contains two integers a and b (1 ≤ a, b ≤ n, a ≠ b) — the endpoints of an edge. It is guaranteed that the given edges form a tree.
Output
Print the number of ways to select k enumerated not necessarily distinct simple paths in such a way that for each edge either it is not contained in any path, or it is contained in exactly one path, or it is contained in all k paths, and the intersection of all paths is non-empty.
As the answer can be large, print it modulo 998244353.
Examples
Input
3 2
1 2
2 3
Output
7
Input
5 1
4 1
2 3
4 5
2 1
Output
10
Input
29 29
1 2
1 3
1 4
1 5
5 6
5 7
5 8
8 9
8 10
8 11
11 12
11 13
11 14
14 15
14 16
14 17
17 18
17 19
17 20
20 21
20 22
20 23
23 24
23 25
23 26
26 27
26 28
26 29
Output
125580756
Note
In the first example the following ways are valid:
* ((1,2), (1,2)),
* ((1,2), (1,3)),
* ((1,3), (1,2)),
* ((1,3), (1,3)),
* ((1,3), (2,3)),
* ((2,3), (1,3)),
* ((2,3), (2,3)).
In the second example k=1, so all n ⋅ (n - 1) / 2 = 5 ⋅ 4 / 2 = 10 paths are valid.
In the third example, the answer is ≥ 998244353, so it was taken modulo 998244353, don't forget it!
### Response
```cpp
#include <bits/stdc++.h>
template <typename _Tp>
void read(_Tp &x) {
char ch(getchar());
bool f(false);
while (!isdigit(ch)) f |= ch == 45, ch = getchar();
x = ch & 15, ch = getchar();
while (isdigit(ch)) x = x * 10 + (ch & 15), ch = getchar();
if (f) x = -x;
}
template <typename _Tp, typename... Args>
void read(_Tp &t, Args &...args) {
read(t);
read(args...);
}
template <typename _Tp>
inline void chmax(_Tp &a, const _Tp &b) {
a = a < b ? b : a;
}
const int max_len = 1 << 18, N = max_len + 5, mod = 998244353, inf = 0x3f3f3f3f;
template <typename _Tp1, typename _Tp2>
inline void add(_Tp1 &a, _Tp2 b) {
(a += b) >= mod && (a -= mod);
}
template <typename _Tp1, typename _Tp2>
inline void sub(_Tp1 &a, _Tp2 b) {
(a -= b) < 0 && (a += mod);
}
template <typename _Tp>
inline _Tp _sub(_Tp a, const _Tp &b) {
(a += mod - b) >= mod && (a -= mod);
return a;
}
long long ksm(long long a, long long b = mod - 2) {
long long res = 1;
while (b) {
if (b & 1) res = res * a % mod;
a = a * a % mod, b >>= 1;
}
return res;
}
void print(const std::vector<int> &a) {
for (auto it : a) printf("%d ", it);
printf("\n");
}
inline std::vector<int> operator<<(std::vector<int> a, unsigned int b) {
return a.insert(a.begin(), b, 0), a;
}
inline std::vector<int> operator<<=(std::vector<int> &a, unsigned int b) {
return a.insert(a.begin(), b, 0), a;
}
inline std::vector<int> operator>>(const std::vector<int> &a, unsigned int b) {
return b >= a.size() ? std::vector<int>()
: std::vector<int>{a.begin() + b, a.end()};
}
inline std::vector<int> operator>>=(std::vector<int> &a, unsigned int b) {
return a = b >= a.size() ? std::vector<int>()
: std::vector<int>{a.begin() + b, a.end()};
}
std::vector<int> operator+=(std::vector<int> &a, const std::vector<int> &b) {
if (b.size() > a.size()) a.resize(b.size());
for (unsigned int i = 0; i < b.size(); ++i) add(a[i], b[i]);
return a;
}
inline std::vector<int> operator+(const std::vector<int> &a,
const std::vector<int> &b) {
std::vector<int> tmp(a);
tmp += b;
return tmp;
}
std::vector<int> operator-=(std::vector<int> &a, const std::vector<int> &b) {
if (b.size() > a.size()) a.resize(b.size());
for (unsigned int i = 0; i < b.size(); ++i) sub(a[i], b[i]);
return a;
}
inline std::vector<int> operator-(const std::vector<int> &a,
const std::vector<int> &b) {
std::vector<int> tmp(a);
tmp -= b;
return tmp;
}
const unsigned long long Omg = ksm(3, (mod - 1) / max_len);
unsigned long long Omgs[N];
void setup() {
Omgs[max_len / 2] = 1;
for (int i = max_len / 2 + 1; i < max_len; ++i)
Omgs[i] = Omgs[i - 1] * Omg % mod;
for (int i = max_len / 2 - 1; i > 0; --i) Omgs[i] = Omgs[i << 1];
}
unsigned int rev[N];
unsigned int getlen(unsigned int len) {
unsigned int limit = 1;
while (limit < len) limit <<= 1;
for (unsigned int i = 0; i < limit; ++i)
rev[i] = (rev[i >> 1] >> 1) | (i & 1 ? limit >> 1 : 0);
return limit;
}
void dft(unsigned long long *A, unsigned int limit) {
for (unsigned int i = 0; i < limit; ++i)
if (i < rev[i]) std::swap(A[i], A[rev[i]]);
for (unsigned int len = 1; len < limit; len <<= 1) {
if (len == 262144u)
for (unsigned int i = 0; i < limit; ++i) A[i] %= mod;
for (unsigned int i = 0; i < limit; i += len << 1) {
unsigned long long *p = A + i, *q = A + i + len, *w = Omgs + len;
for (unsigned int j = 0; j < len; ++j, ++p, ++q, ++w) {
const unsigned long long tp = *q * *w % mod;
*q = *p + mod - tp, *p += tp;
}
}
}
for (unsigned int i = 0; i < limit; ++i) A[i] %= mod;
}
void idft(unsigned long long *A, unsigned int limit) {
std::reverse(A + 1, A + limit), dft(A, limit);
unsigned long long inv = mod - (mod - 1) / limit;
for (unsigned int i = 0; i < limit; ++i) A[i] = A[i] * inv % mod;
}
unsigned long long _f[N], _g[N], _tp[129];
std::vector<int> operator*(const std::vector<int> &a,
const std::vector<int> &b) {
unsigned int len = a.size() + b.size() - 1;
if (a.size() <= 64u || b.size() <= 64u) {
memset(_tp, 0, len << 3);
unsigned int r = 0;
for (unsigned int i = 0; i < a.size(); ++i) {
for (unsigned int j = 0; j < b.size(); ++j)
_tp[i + j] += 1ULL * a[i] * b[j];
if (++r == 18) {
r = 0;
for (unsigned int j = i - 17; j < i + b.size(); ++j) _tp[j] %= mod;
}
}
if (r)
for (unsigned int j = 0; j < len; ++j) _tp[j] %= mod;
std::vector<int> c(len);
for (unsigned int i = 0; i < len; ++i) c[i] = _tp[i];
return c;
}
unsigned int limit = getlen(len);
memset(_f + a.size(), 0, (limit - a.size()) << 3);
for (unsigned int i = 0; i < a.size(); ++i) _f[i] = a[i];
memset(_g + b.size(), 0, (limit - b.size()) << 3);
for (unsigned int i = 0; i < b.size(); ++i) _g[i] = b[i];
dft(_f, limit), dft(_g, limit);
for (unsigned int i = 0; i < limit; ++i) _f[i] = _f[i] * _g[i] % mod;
idft(_f, limit);
std::vector<int> ans(len);
for (unsigned int i = 0; i < len; ++i) ans[i] = _f[i];
return ans;
}
std::vector<int> mul(const std::vector<int> &a, const std::vector<int> &b,
unsigned int len, bool need = true) {
if (a.size() <= 64u || b.size() <= 64u) {
memset(_tp, 0, len << 3);
unsigned int r = 0;
for (unsigned int i = 0; i < a.size(); ++i) {
for (unsigned int j = 0; j < b.size() && i + j < len; ++j)
_tp[i + j] += 1ULL * a[i] * b[j];
if (++r == 18) {
r = 0;
for (unsigned int j = i - 17; j < len && j < i + b.size(); ++j)
_tp[j] %= mod;
}
}
if (r)
for (unsigned int j = 0; j < len; ++j) _tp[j] %= mod;
std::vector<int> c(len);
for (unsigned int i = 0; i < len; ++i) c[i] = _tp[i];
return c;
}
int limit = getlen(len);
memset(_f + a.size(), 0, (limit - a.size()) << 3);
for (unsigned int i = 0; i < a.size(); ++i) _f[i] = a[i];
dft(_f, limit);
if (need) {
memset(_g + b.size(), 0, (limit - b.size()) << 3);
for (unsigned int i = 0; i < b.size(); ++i) _g[i] = b[i];
dft(_g, limit);
}
for (int i = 0; i < limit; ++i) _f[i] = 1ULL * _f[i] * _g[i] % mod;
idft(_f, limit);
std::vector<int> ans(len);
for (unsigned int i = 0; i < len; ++i) ans[i] = _f[i];
return ans;
}
std::vector<int> mulT(const std::vector<int> &a, const std::vector<int> &b) {
if (a.size() <= 64u || b.size() <= 64u) {
std::vector<int> c(a.size() - b.size() + 1);
for (unsigned int i = 0; i < c.size(); ++i)
for (unsigned int j = 0; j < b.size(); ++j)
c[i] = (c[i] + 1ULL * a[i + j] * b[j]) % mod;
return c;
}
int limit = getlen(a.size());
memset(_f, 0, limit << 3);
for (unsigned int i = 0; i < a.size(); ++i) _f[i] = a[i];
memset(_g, 0, limit << 3);
for (unsigned int i = 0; i < b.size(); ++i) _g[i] = b[i];
std::reverse(_g, _g + b.size()), dft(_f, limit), dft(_g, limit);
for (int i = 0; i < limit; ++i) _f[i] = 1ULL * _f[i] * _g[i] % mod;
idft(_f, limit);
std::vector<int> ans(a.size() - b.size() + 1);
for (unsigned int i = b.size() - 1; i < a.size(); ++i)
ans[i - b.size() + 1] = _f[i];
return ans;
}
inline std::vector<int> operator*=(std::vector<int> &a,
const std::vector<int> &b) {
return a = a * b;
}
template <typename _Tp>
inline std::vector<int> operator*=(std::vector<int> &a, const _Tp &b) {
for (auto &&it : a) it = 1ULL * it * b % mod;
return a;
}
template <typename _Tp>
inline std::vector<int> operator*(std::vector<int> a, const _Tp &b) {
return a *= b;
}
template <typename _Tp>
inline std::vector<int> operator*(const _Tp &b, std::vector<int> a) {
return a *= b;
}
template <typename _Tp>
inline std::vector<int> operator/=(std::vector<int> &a, const _Tp &b) {
unsigned long long inv = ksm(b);
for (auto &&it : a) it = 1ULL * it * inv % mod;
return a;
}
template <typename _Tp>
inline std::vector<int> operator/(std::vector<int> a, const _Tp &b) {
return a /= b;
}
long long inv[N], ifac[N];
std::vector<int> e[N], w[N];
int siz[N], n, k, a[N], _[N];
std::vector<int> f[N << 1];
void sol1(int l, int r, int u) {
if (l == r) return f[u] = {1, a[l]}, void();
int mid = (l + r) >> 1;
sol1(l, mid, u << 1), sol1(mid + 1, r, u << 1 | 1);
f[u] = f[u << 1] * f[u << 1 | 1];
}
void sol2(int l, int r, int u, const std::vector<int> &g) {
if (l == r) return _[l] = g[0], void();
int mid = (l + r) >> 1;
std::vector<int> tp = mulT(g, f[u << 1 | 1]);
if (((int)tp.size()) > mid - l + 1) tp.resize(mid - l + 1);
sol2(l, mid, u << 1, tp);
tp = mulT(g, f[u << 1]);
if (((int)tp.size()) > r - mid) tp.resize(r - mid);
sol2(mid + 1, r, u << 1 | 1, tp);
}
void ___(int x, int fa) {
std::sort(e[x].begin(), e[x].end());
siz[x] = 1;
for (auto v : e[x])
if (v != fa) ___(v, x), siz[x] += siz[v];
int pos = 0;
for (auto v : e[x]) a[pos++] = v == fa ? n - siz[x] : siz[v];
sol1(0, pos - 1, 1);
std::vector<int> A(pos + 1);
for (int i = 0; i <= pos; ++i) A[i] = k - i > 0 ? ifac[k - i] : 0;
sol2(0, pos - 1, 1, A);
w[x].resize(((int)e[x].size()));
for (int i = 0; i < ((int)e[x].size()); ++i) w[x][i] = _[i];
}
bool vis[N];
int S, mn, rt;
int getsize(int x, int fa) {
int siz = 1;
for (auto v : e[x])
if (v != fa && !vis[v]) siz += getsize(v, x);
return siz;
}
void getrt(int x, int fa) {
int tmp = 0;
siz[x] = 1;
for (auto v : e[x])
if (v != fa && !vis[v]) getrt(v, x), chmax(tmp, siz[v]), siz[x] += siz[v];
chmax(tmp, S - siz[x]);
if (tmp < mn) mn = tmp, rt = x;
}
int QAQ, ans;
void dfs1(int x, int fa) {
add(QAQ, w[x][std::lower_bound(e[x].begin(), e[x].end(), fa) - e[x].begin()]);
for (auto v : e[x])
if (v != fa && !vis[v]) dfs1(v, x);
}
void dfs(int x) {
S = getsize(x, 0), mn = inf, getrt(x, 0), vis[x = rt] = true;
int sum = 0;
for (auto v : e[x])
if (!vis[v])
QAQ = 0, dfs1(v, x),
add(ans, 1LL * QAQ *
w[x][std::lower_bound(e[x].begin(), e[x].end(), v) -
e[x].begin()] %
mod),
add(ans, 1LL * sum * QAQ % mod), add(sum, QAQ);
for (auto v : e[x])
if (!vis[v]) dfs(v);
}
int main() {
inv[0] = inv[1] = ifac[0] = ifac[1] = 1;
for (int i = 2; i < N; ++i)
inv[i] = inv[mod % i] * (mod - mod / i) % mod,
ifac[i] = ifac[i - 1] * inv[i] % mod;
setup();
read(n, k);
if (n == 1) return puts("0"), 0;
for (int i = 1, x, y; i < n; ++i)
read(x, y), e[x].push_back(y), e[y].push_back(x);
___(1, 0), dfs(1);
for (int i = 1; i <= k; ++i) ans = 1LL * ans * i % mod * i % mod;
printf("%d\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using std::lower_bound;
using std::max;
using std::min;
using std::random_shuffle;
using std::reverse;
using std::sort;
using std::swap;
using std::unique;
using std::upper_bound;
using std::vector;
void open(const char *s) {}
int rd() {
int s = 0, c, b = 0;
while (((c = getchar()) < '0' || c > '9') && c != '-')
;
if (c == '-') {
c = getchar();
b = 1;
}
do {
s = s * 10 + c - '0';
} while ((c = getchar()) >= '0' && c <= '9');
return b ? -s : s;
}
void put(int x) {
if (!x) {
putchar('0');
return;
}
static int c[20];
int t = 0;
while (x) {
c[++t] = x % 10;
x /= 10;
}
while (t) putchar(c[t--] + '0');
}
int upmin(int &a, int b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
int upmax(int &a, int b) {
if (b > a) {
a = b;
return 1;
}
return 0;
}
const long long p = 998244353;
const int N = 100010;
long long fp(long long a, long long b) {
long long s = 1;
for (; b; b >>= 1, a = a * a % p)
if (b & 1) s = s * a % p;
return s;
}
namespace ntt {
const int N = 300000;
const int W = 1 << 18;
long long plus(long long a, long long b) {
a += b;
return a >= p ? a - p : a;
}
long long minus(long long a, long long b) {
a -= b;
return a >= 0 ? a : a + p;
}
long long w[N];
int rev[N];
void ntt(long long *a, int n, int t) {
for (int i = 1; i < n; i++) {
rev[i] = (rev[i >> 1] >> 1) | (i & 1 ? n >> 1 : 0);
if (rev[i] > i) swap(a[i], a[rev[i]]);
}
for (int i = 0; i < n; i++) a[i] = plus(a[i], p);
for (int i = 2; i <= n; i <<= 1)
for (int j = 0; j < n; j += i)
for (int k = 0; k < i / 2; k++) {
long long u = a[j + k];
long long v = a[j + k + i / 2] * w[W / i * k] % p;
a[j + k] = plus(u, v);
a[j + k + i / 2] = minus(u, v);
}
if (t == -1) {
reverse(a + 1, a + n);
long long inv = fp(n, p - 2);
for (int i = 0; i < n; i++) a[i] = a[i] * inv % p;
}
}
void init() {
long long s = fp(3, (p - 1) / W);
w[0] = 1;
for (int i = 1; i < W / 2; i++) w[i] = w[i - 1] * s % p;
}
void mul(long long *a, long long *b, long long *c, int n, int m, int l) {
static long long a1[N], a2[N];
int k = 1;
while (k <= n + m) k <<= 1;
for (int i = 0; i < k; i++) a1[i] = a2[i] = 0;
for (int i = 0; i <= n; i++) a1[i] = a[i];
for (int i = 0; i <= m; i++) a2[i] = b[i];
ntt(a1, k, 1);
ntt(a2, k, 1);
for (int i = 0; i < k; i++) a1[i] = a1[i] * a2[i] % p;
ntt(a1, k, -1);
for (int i = 0; i <= l; i++) c[i] = a1[i];
}
void mul2(long long *a, long long *b, long long *c, long long *d, long long *e,
int n, int m, int l) {
static long long a1[N], a2[N], a3[N], a4[N];
int k = 1;
while (k <= n + m) k <<= 1;
for (int i = 0; i < k; i++) a1[i] = a2[i] = a3[i] = a4[i] = 0;
for (int i = 0; i <= n; i++) {
a1[i] = a[i];
a3[i] = c[i];
}
for (int i = 0; i <= m; i++) {
a2[i] = b[i];
a4[i] = d[i];
}
ntt(a1, k, 1);
ntt(a2, k, 1);
ntt(a3, k, 1);
ntt(a4, k, 1);
for (int i = 0; i < k; i++) a1[i] = a1[i] * a2[i] % p;
for (int i = 0; i < k; i++) a3[i] = a3[i] * a4[i] % p;
ntt(a1, k, -1);
ntt(a3, k, -1);
for (int i = 0; i <= l; i++) e[i] = (a1[i] + a3[i]) % p;
}
} // namespace ntt
vector<int> g[N];
int s[N];
int n, k;
long long ans = 0;
long long fac[N];
long long ifac[N];
void init() {
fac[0] = 1;
for (int i = 1; i <= k; i++) fac[i] = fac[i - 1] * i % p;
ifac[k] = fp(fac[k], p - 2);
for (int i = k; i >= 1; i--) ifac[i - 1] = ifac[i] * i % p;
}
long long binom(int x, int y) {
return x >= y && y >= 0 ? fac[x] * ifac[y] % p * ifac[x - y] % p : 0;
}
long long f1[N];
long long f2[N];
long long c[N];
long long d[N];
int l;
typedef std::pair<long long *, long long *> _;
_ solve(int l, int r) {
_ s;
if (l == r) {
s.first = new long long[2];
s.second = new long long[2];
s.first[0] = 1;
s.first[1] = c[l];
s.second[0] = d[l];
s.second[1] = d[l] * c[0] % p;
return s;
}
int mid = (l + r) >> 1;
_ ls = solve(l, mid);
_ rs = solve(mid + 1, r);
s.first = new long long[r - l + 2];
s.second = new long long[r - l + 2];
ntt::mul(ls.first, rs.first, s.first, mid - l + 1, r - mid, r - l + 1);
ntt::mul2(ls.second, rs.first, ls.first, rs.second, s.second, mid - l + 1,
r - mid, r - l + 1);
return s;
}
void dfs(int x, int fa) {
s[x] = 1;
for (auto v : g[x])
if (v != fa) {
dfs(v, x);
s[x] += s[v];
ans = (ans + f2[x] * f2[v]) % p;
f2[x] = (f2[x] + f2[v]) % p;
}
if (s[x] == 1) {
f1[x] = f2[x] = 1;
return;
}
l = 0;
for (auto v : g[x])
if (v != fa) {
c[++l] = s[v];
d[l] = f2[v];
}
c[0] = n - s[x];
_ e = solve(1, l);
for (int i = 0; i <= l && i <= k; i++) {
f1[x] = (f1[x] + e.first[i] * binom(k, i) % p * fac[i]) % p;
ans = (ans + e.second[i] * binom(k, i) % p * fac[i]) % p;
}
f2[x] = (f2[x] + f1[x]) % p;
}
int main() {
ntt::init();
open("cf981h");
scanf("%d%d", &n, &k);
if (k == 1) {
printf("%I64d\n", (long long)n * (n - 1) / 2 % p);
return 0;
}
init();
int x, y;
for (int i = 1; i < n; i++) {
scanf("%d%d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
}
dfs(1, 0);
printf("%I64d\n", ans);
return 0;
}
| ### Prompt
Develop a solution in CPP to the problem described below:
You are given a tree of n vertices. You are to select k (not necessarily distinct) simple paths in such a way that it is possible to split all edges of the tree into three sets: edges not contained in any path, edges that are a part of exactly one of these paths, and edges that are parts of all selected paths, and the latter set should be non-empty.
Compute the number of ways to select k paths modulo 998244353.
The paths are enumerated, in other words, two ways are considered distinct if there are such i (1 ≤ i ≤ k) and an edge that the i-th path contains the edge in one way and does not contain it in the other.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 10^{5}) — the number of vertices in the tree and the desired number of paths.
The next n - 1 lines describe edges of the tree. Each line contains two integers a and b (1 ≤ a, b ≤ n, a ≠ b) — the endpoints of an edge. It is guaranteed that the given edges form a tree.
Output
Print the number of ways to select k enumerated not necessarily distinct simple paths in such a way that for each edge either it is not contained in any path, or it is contained in exactly one path, or it is contained in all k paths, and the intersection of all paths is non-empty.
As the answer can be large, print it modulo 998244353.
Examples
Input
3 2
1 2
2 3
Output
7
Input
5 1
4 1
2 3
4 5
2 1
Output
10
Input
29 29
1 2
1 3
1 4
1 5
5 6
5 7
5 8
8 9
8 10
8 11
11 12
11 13
11 14
14 15
14 16
14 17
17 18
17 19
17 20
20 21
20 22
20 23
23 24
23 25
23 26
26 27
26 28
26 29
Output
125580756
Note
In the first example the following ways are valid:
* ((1,2), (1,2)),
* ((1,2), (1,3)),
* ((1,3), (1,2)),
* ((1,3), (1,3)),
* ((1,3), (2,3)),
* ((2,3), (1,3)),
* ((2,3), (2,3)).
In the second example k=1, so all n ⋅ (n - 1) / 2 = 5 ⋅ 4 / 2 = 10 paths are valid.
In the third example, the answer is ≥ 998244353, so it was taken modulo 998244353, don't forget it!
### Response
```cpp
#include <bits/stdc++.h>
using std::lower_bound;
using std::max;
using std::min;
using std::random_shuffle;
using std::reverse;
using std::sort;
using std::swap;
using std::unique;
using std::upper_bound;
using std::vector;
void open(const char *s) {}
int rd() {
int s = 0, c, b = 0;
while (((c = getchar()) < '0' || c > '9') && c != '-')
;
if (c == '-') {
c = getchar();
b = 1;
}
do {
s = s * 10 + c - '0';
} while ((c = getchar()) >= '0' && c <= '9');
return b ? -s : s;
}
void put(int x) {
if (!x) {
putchar('0');
return;
}
static int c[20];
int t = 0;
while (x) {
c[++t] = x % 10;
x /= 10;
}
while (t) putchar(c[t--] + '0');
}
int upmin(int &a, int b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
int upmax(int &a, int b) {
if (b > a) {
a = b;
return 1;
}
return 0;
}
const long long p = 998244353;
const int N = 100010;
long long fp(long long a, long long b) {
long long s = 1;
for (; b; b >>= 1, a = a * a % p)
if (b & 1) s = s * a % p;
return s;
}
namespace ntt {
const int N = 300000;
const int W = 1 << 18;
long long plus(long long a, long long b) {
a += b;
return a >= p ? a - p : a;
}
long long minus(long long a, long long b) {
a -= b;
return a >= 0 ? a : a + p;
}
long long w[N];
int rev[N];
void ntt(long long *a, int n, int t) {
for (int i = 1; i < n; i++) {
rev[i] = (rev[i >> 1] >> 1) | (i & 1 ? n >> 1 : 0);
if (rev[i] > i) swap(a[i], a[rev[i]]);
}
for (int i = 0; i < n; i++) a[i] = plus(a[i], p);
for (int i = 2; i <= n; i <<= 1)
for (int j = 0; j < n; j += i)
for (int k = 0; k < i / 2; k++) {
long long u = a[j + k];
long long v = a[j + k + i / 2] * w[W / i * k] % p;
a[j + k] = plus(u, v);
a[j + k + i / 2] = minus(u, v);
}
if (t == -1) {
reverse(a + 1, a + n);
long long inv = fp(n, p - 2);
for (int i = 0; i < n; i++) a[i] = a[i] * inv % p;
}
}
void init() {
long long s = fp(3, (p - 1) / W);
w[0] = 1;
for (int i = 1; i < W / 2; i++) w[i] = w[i - 1] * s % p;
}
void mul(long long *a, long long *b, long long *c, int n, int m, int l) {
static long long a1[N], a2[N];
int k = 1;
while (k <= n + m) k <<= 1;
for (int i = 0; i < k; i++) a1[i] = a2[i] = 0;
for (int i = 0; i <= n; i++) a1[i] = a[i];
for (int i = 0; i <= m; i++) a2[i] = b[i];
ntt(a1, k, 1);
ntt(a2, k, 1);
for (int i = 0; i < k; i++) a1[i] = a1[i] * a2[i] % p;
ntt(a1, k, -1);
for (int i = 0; i <= l; i++) c[i] = a1[i];
}
void mul2(long long *a, long long *b, long long *c, long long *d, long long *e,
int n, int m, int l) {
static long long a1[N], a2[N], a3[N], a4[N];
int k = 1;
while (k <= n + m) k <<= 1;
for (int i = 0; i < k; i++) a1[i] = a2[i] = a3[i] = a4[i] = 0;
for (int i = 0; i <= n; i++) {
a1[i] = a[i];
a3[i] = c[i];
}
for (int i = 0; i <= m; i++) {
a2[i] = b[i];
a4[i] = d[i];
}
ntt(a1, k, 1);
ntt(a2, k, 1);
ntt(a3, k, 1);
ntt(a4, k, 1);
for (int i = 0; i < k; i++) a1[i] = a1[i] * a2[i] % p;
for (int i = 0; i < k; i++) a3[i] = a3[i] * a4[i] % p;
ntt(a1, k, -1);
ntt(a3, k, -1);
for (int i = 0; i <= l; i++) e[i] = (a1[i] + a3[i]) % p;
}
} // namespace ntt
vector<int> g[N];
int s[N];
int n, k;
long long ans = 0;
long long fac[N];
long long ifac[N];
void init() {
fac[0] = 1;
for (int i = 1; i <= k; i++) fac[i] = fac[i - 1] * i % p;
ifac[k] = fp(fac[k], p - 2);
for (int i = k; i >= 1; i--) ifac[i - 1] = ifac[i] * i % p;
}
long long binom(int x, int y) {
return x >= y && y >= 0 ? fac[x] * ifac[y] % p * ifac[x - y] % p : 0;
}
long long f1[N];
long long f2[N];
long long c[N];
long long d[N];
int l;
typedef std::pair<long long *, long long *> _;
_ solve(int l, int r) {
_ s;
if (l == r) {
s.first = new long long[2];
s.second = new long long[2];
s.first[0] = 1;
s.first[1] = c[l];
s.second[0] = d[l];
s.second[1] = d[l] * c[0] % p;
return s;
}
int mid = (l + r) >> 1;
_ ls = solve(l, mid);
_ rs = solve(mid + 1, r);
s.first = new long long[r - l + 2];
s.second = new long long[r - l + 2];
ntt::mul(ls.first, rs.first, s.first, mid - l + 1, r - mid, r - l + 1);
ntt::mul2(ls.second, rs.first, ls.first, rs.second, s.second, mid - l + 1,
r - mid, r - l + 1);
return s;
}
void dfs(int x, int fa) {
s[x] = 1;
for (auto v : g[x])
if (v != fa) {
dfs(v, x);
s[x] += s[v];
ans = (ans + f2[x] * f2[v]) % p;
f2[x] = (f2[x] + f2[v]) % p;
}
if (s[x] == 1) {
f1[x] = f2[x] = 1;
return;
}
l = 0;
for (auto v : g[x])
if (v != fa) {
c[++l] = s[v];
d[l] = f2[v];
}
c[0] = n - s[x];
_ e = solve(1, l);
for (int i = 0; i <= l && i <= k; i++) {
f1[x] = (f1[x] + e.first[i] * binom(k, i) % p * fac[i]) % p;
ans = (ans + e.second[i] * binom(k, i) % p * fac[i]) % p;
}
f2[x] = (f2[x] + f1[x]) % p;
}
int main() {
ntt::init();
open("cf981h");
scanf("%d%d", &n, &k);
if (k == 1) {
printf("%I64d\n", (long long)n * (n - 1) / 2 % p);
return 0;
}
init();
int x, y;
for (int i = 1; i < n; i++) {
scanf("%d%d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
}
dfs(1, 0);
printf("%I64d\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const int Mod = 998244353, rt = 3;
void add(int &x, int y) {
x += y;
if (x >= Mod) x -= Mod;
}
int Pow(int x, int e) {
int ret = 1;
while (e) {
if (e & 1) ret = 1ll * ret * x % Mod;
x = 1ll * x * x % Mod;
e >>= 1;
}
return ret;
}
int rev[N];
void DFT(int *A, int n, int wn) {
static int w[N];
for (int i = 0; i <= n - 1; i++)
if (i < rev[i]) swap(A[i], A[rev[i]]);
for (int i = 1; i < n; i <<= 1) {
w[0] = 1, w[1] = Pow(rt, (Mod - 1) / (i << 1));
if (wn == -1) w[1] = Pow(w[1], Mod - 2);
for (int j = 2; j < i; ++j) w[j] = 1ll * w[j - 1] * w[1] % Mod;
for (int j = 0; j < n; j += i << 1)
for (int k = 0; k < i; ++k) {
int x = A[j + k], y = 1ll * A[i + j + k] * w[k] % Mod;
A[j + k] = (x + y) % Mod, A[i + j + k] = (x + Mod - y) % Mod;
}
}
}
void mul(vector<int> &a, const vector<int> &b) {
int x = a.size(), y = b.size(), n = 1;
while (n < x + y) n <<= 1;
for (int i = 0; i <= n - 1; i++)
rev[i] = (rev[i >> 1] >> 1) | (i & 1 ? n / 2 : 0);
static int A[N << 1], B[N << 1];
for (int i = 0; i <= n - 1; i++) A[i] = i < x ? a[i] : 0;
for (int i = 0; i <= n - 1; i++) B[i] = i < y ? b[i] : 0;
DFT(A, n, 1), DFT(B, n, 1);
for (int i = 0; i <= n - 1; i++) A[i] = 1ll * A[i] * B[i] % Mod;
DFT(A, n, -1);
int inv = Pow(n, Mod - 2);
for (int i = 0; i <= n - 1; i++) A[i] = 1ll * inv * A[i] % Mod;
a.resize(x + y - 1);
for (int i = 0; i <= x + y - 2; i++) a[i] = A[i];
}
vector<int> A[N];
void mult(int *f, int *x, int n) {
for (int i = 1; i <= n; i++)
A[i].clear(), A[i].push_back(1), A[i].push_back(x[i]);
for (int i = 1; i < n; i <<= 1)
for (int j = 1; j + i <= n; j += i << 1) mul(A[j], A[j + i]);
for (int i = 0; i <= n; i++) f[i] = A[1][i];
}
int Begin[N], Next[N << 1], to[N << 1], e;
void adde(int u, int v) { to[++e] = v, Next[e] = Begin[u], Begin[u] = e; }
int n, m, k;
int fac[N], rfac[N];
int sz[N];
int coe[N], f[N], dp[N];
int P[N], val[N];
int ans;
void DFS_work(int o, int fa = 0) {
sz[o] = 1;
for (int i = Begin[o]; i; i = Next[i]) {
int u = to[i];
if (u == fa) continue;
DFS_work(u, o);
ans = (ans + 1ll * dp[u] * dp[o]) % Mod;
add(dp[o], dp[u]);
sz[o] += sz[u];
}
m = 0;
if (fa) coe[m = 1] = n - sz[o];
for (int i = Begin[o]; i; i = Next[i]) {
int u = to[i];
if (u == fa) continue;
coe[++m] = sz[u];
}
sort(coe + 1, coe + m + 1);
mult(f, coe, m);
for (int i = 1; i <= m; i++)
if (coe[i] != coe[i - 1]) {
for (int j = 0; j <= m; j++) P[j] = f[j];
for (int j = 0; j <= m; j++)
P[j + 1] = ((P[j + 1] - 1ll * coe[i] * P[j]) % Mod + Mod) % Mod;
int r = min(k, m - 1), &w = val[coe[i]];
w = 0;
for (int j = 0; j <= r; j++) w = (w + 1ll * P[j] * rfac[k - j]) % Mod;
w = 1ll * w * fac[k] % Mod;
}
for (int i = Begin[o]; i; i = Next[i]) {
int u = to[i];
if (u == fa) continue;
ans = (ans + 1ll * dp[u] * val[sz[u]]) % Mod;
}
add(dp[o], val[n - sz[o]]);
}
int main() {
scanf("%d%d", &n, &k);
if (n == 1) {
puts("0");
return 0;
}
if (k == 1) {
int res = 1ll * n * (n - 1) / 2 % Mod;
printf("%d\n", res);
return 0;
}
for (int i = 2; i <= n; i++) {
int u, v;
scanf("%d%d", &u, &v);
adde(u, v), adde(v, u);
}
int s = max(n, k) + 5;
fac[0] = 1;
for (int i = 1; i <= s; i++) fac[i] = 1ll * fac[i - 1] * i % Mod;
rfac[s] = Pow(fac[s], Mod - 2);
for (int i = s; i >= 1; i--) rfac[i - 1] = 1ll * rfac[i] * i % Mod;
DFS_work(1);
printf("%d\n", ans);
return 0;
}
| ### Prompt
Please formulate a cpp solution to the following problem:
You are given a tree of n vertices. You are to select k (not necessarily distinct) simple paths in such a way that it is possible to split all edges of the tree into three sets: edges not contained in any path, edges that are a part of exactly one of these paths, and edges that are parts of all selected paths, and the latter set should be non-empty.
Compute the number of ways to select k paths modulo 998244353.
The paths are enumerated, in other words, two ways are considered distinct if there are such i (1 ≤ i ≤ k) and an edge that the i-th path contains the edge in one way and does not contain it in the other.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 10^{5}) — the number of vertices in the tree and the desired number of paths.
The next n - 1 lines describe edges of the tree. Each line contains two integers a and b (1 ≤ a, b ≤ n, a ≠ b) — the endpoints of an edge. It is guaranteed that the given edges form a tree.
Output
Print the number of ways to select k enumerated not necessarily distinct simple paths in such a way that for each edge either it is not contained in any path, or it is contained in exactly one path, or it is contained in all k paths, and the intersection of all paths is non-empty.
As the answer can be large, print it modulo 998244353.
Examples
Input
3 2
1 2
2 3
Output
7
Input
5 1
4 1
2 3
4 5
2 1
Output
10
Input
29 29
1 2
1 3
1 4
1 5
5 6
5 7
5 8
8 9
8 10
8 11
11 12
11 13
11 14
14 15
14 16
14 17
17 18
17 19
17 20
20 21
20 22
20 23
23 24
23 25
23 26
26 27
26 28
26 29
Output
125580756
Note
In the first example the following ways are valid:
* ((1,2), (1,2)),
* ((1,2), (1,3)),
* ((1,3), (1,2)),
* ((1,3), (1,3)),
* ((1,3), (2,3)),
* ((2,3), (1,3)),
* ((2,3), (2,3)).
In the second example k=1, so all n ⋅ (n - 1) / 2 = 5 ⋅ 4 / 2 = 10 paths are valid.
In the third example, the answer is ≥ 998244353, so it was taken modulo 998244353, don't forget it!
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const int Mod = 998244353, rt = 3;
void add(int &x, int y) {
x += y;
if (x >= Mod) x -= Mod;
}
int Pow(int x, int e) {
int ret = 1;
while (e) {
if (e & 1) ret = 1ll * ret * x % Mod;
x = 1ll * x * x % Mod;
e >>= 1;
}
return ret;
}
int rev[N];
void DFT(int *A, int n, int wn) {
static int w[N];
for (int i = 0; i <= n - 1; i++)
if (i < rev[i]) swap(A[i], A[rev[i]]);
for (int i = 1; i < n; i <<= 1) {
w[0] = 1, w[1] = Pow(rt, (Mod - 1) / (i << 1));
if (wn == -1) w[1] = Pow(w[1], Mod - 2);
for (int j = 2; j < i; ++j) w[j] = 1ll * w[j - 1] * w[1] % Mod;
for (int j = 0; j < n; j += i << 1)
for (int k = 0; k < i; ++k) {
int x = A[j + k], y = 1ll * A[i + j + k] * w[k] % Mod;
A[j + k] = (x + y) % Mod, A[i + j + k] = (x + Mod - y) % Mod;
}
}
}
void mul(vector<int> &a, const vector<int> &b) {
int x = a.size(), y = b.size(), n = 1;
while (n < x + y) n <<= 1;
for (int i = 0; i <= n - 1; i++)
rev[i] = (rev[i >> 1] >> 1) | (i & 1 ? n / 2 : 0);
static int A[N << 1], B[N << 1];
for (int i = 0; i <= n - 1; i++) A[i] = i < x ? a[i] : 0;
for (int i = 0; i <= n - 1; i++) B[i] = i < y ? b[i] : 0;
DFT(A, n, 1), DFT(B, n, 1);
for (int i = 0; i <= n - 1; i++) A[i] = 1ll * A[i] * B[i] % Mod;
DFT(A, n, -1);
int inv = Pow(n, Mod - 2);
for (int i = 0; i <= n - 1; i++) A[i] = 1ll * inv * A[i] % Mod;
a.resize(x + y - 1);
for (int i = 0; i <= x + y - 2; i++) a[i] = A[i];
}
vector<int> A[N];
void mult(int *f, int *x, int n) {
for (int i = 1; i <= n; i++)
A[i].clear(), A[i].push_back(1), A[i].push_back(x[i]);
for (int i = 1; i < n; i <<= 1)
for (int j = 1; j + i <= n; j += i << 1) mul(A[j], A[j + i]);
for (int i = 0; i <= n; i++) f[i] = A[1][i];
}
int Begin[N], Next[N << 1], to[N << 1], e;
void adde(int u, int v) { to[++e] = v, Next[e] = Begin[u], Begin[u] = e; }
int n, m, k;
int fac[N], rfac[N];
int sz[N];
int coe[N], f[N], dp[N];
int P[N], val[N];
int ans;
void DFS_work(int o, int fa = 0) {
sz[o] = 1;
for (int i = Begin[o]; i; i = Next[i]) {
int u = to[i];
if (u == fa) continue;
DFS_work(u, o);
ans = (ans + 1ll * dp[u] * dp[o]) % Mod;
add(dp[o], dp[u]);
sz[o] += sz[u];
}
m = 0;
if (fa) coe[m = 1] = n - sz[o];
for (int i = Begin[o]; i; i = Next[i]) {
int u = to[i];
if (u == fa) continue;
coe[++m] = sz[u];
}
sort(coe + 1, coe + m + 1);
mult(f, coe, m);
for (int i = 1; i <= m; i++)
if (coe[i] != coe[i - 1]) {
for (int j = 0; j <= m; j++) P[j] = f[j];
for (int j = 0; j <= m; j++)
P[j + 1] = ((P[j + 1] - 1ll * coe[i] * P[j]) % Mod + Mod) % Mod;
int r = min(k, m - 1), &w = val[coe[i]];
w = 0;
for (int j = 0; j <= r; j++) w = (w + 1ll * P[j] * rfac[k - j]) % Mod;
w = 1ll * w * fac[k] % Mod;
}
for (int i = Begin[o]; i; i = Next[i]) {
int u = to[i];
if (u == fa) continue;
ans = (ans + 1ll * dp[u] * val[sz[u]]) % Mod;
}
add(dp[o], val[n - sz[o]]);
}
int main() {
scanf("%d%d", &n, &k);
if (n == 1) {
puts("0");
return 0;
}
if (k == 1) {
int res = 1ll * n * (n - 1) / 2 % Mod;
printf("%d\n", res);
return 0;
}
for (int i = 2; i <= n; i++) {
int u, v;
scanf("%d%d", &u, &v);
adde(u, v), adde(v, u);
}
int s = max(n, k) + 5;
fac[0] = 1;
for (int i = 1; i <= s; i++) fac[i] = 1ll * fac[i - 1] * i % Mod;
rfac[s] = Pow(fac[s], Mod - 2);
for (int i = s; i >= 1; i--) rfac[i - 1] = 1ll * rfac[i] * i % Mod;
DFS_work(1);
printf("%d\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
mt19937 rnd(239);
const long double pi = acos(-1.0);
const long long INF = 1e18 + 239;
const int BIG = 1e9 + 239;
const int M = 2e5 + 239;
const int T = (1 << 19);
const long long MOD = 998244353;
const long long root = 3;
const long long sub = 15311432;
long long st[T];
inline long long power(long long a, long long b) {
if (b == 0) return 1;
long long t = power(a, (b >> 1));
t = (t * t) % MOD;
if (b & 1) return (t * a) % MOD;
return t;
}
inline int inv(int n, int b) {
int ans = 0;
for (int i = 0; i < b; i++) {
ans = (ans << 1) + (n & 1);
n >>= 1;
}
return ans;
}
int p;
long long w;
map<vector<long long>, vector<long long> > pq;
inline void fft(const vector<long long> &a, vector<long long> &b) {
if (pq.count(a)) {
b = pq[a];
return;
}
b.resize(a.size());
for (int i = 0; i < a.size(); i++) b[i] = a[inv(i, p)];
for (int z = 0; z < p; z++) {
int u = (1 << z);
for (int i = 0; i < (1 << p); i++) {
int ps = i >> z;
if ((ps & 1) == 0) {
int t = i & (u - 1);
long long bi = (b[i] + st[t << (p - z - 1)] * b[i + u]) % MOD;
long long bii = (b[i] + st[(t + u) << (p - z - 1)] * b[i + u]) % MOD;
b[i] = bi;
b[i + u] = bii;
}
}
}
pq[a] = b;
}
inline vector<long long> sum(vector<long long> a, vector<long long> b) {
int n = max((int)a.size(), (int)b.size());
vector<long long> ans(n, 0);
while (a.size() < n) a.push_back(0);
while (b.size() < n) b.push_back(0);
for (int i = 0; i < n; i++) ans[i] = (a[i] + b[i]) % MOD;
return ans;
}
vector<long long> ta, tb;
vector<long long> as;
inline vector<long long> multiply(vector<long long> a, vector<long long> b) {
int k = (a.size() + b.size() + 1);
p = trunc(log2(k)) + 1;
while (a.size() < (1 << p)) a.push_back(0);
while (b.size() < (1 << p)) b.push_back(0);
w = power(sub, (1 << (23 - p)));
st[0] = 1LL;
for (int i = 1; i < (1 << p); i++) st[i] = (st[i - 1] * w) % MOD;
fft(a, ta);
fft(b, tb);
vector<long long> v;
for (int i = 0; i < (1 << p); i++) v.push_back((ta[i] * tb[i]) % MOD);
fft(v, as);
long long d = power((1 << p), MOD - 2);
for (int i = 0; i < (1 << p); i++) as[i] = (d * as[i]) % MOD;
reverse(as.begin() + 1, as.end());
while (as.back() == 0) as.pop_back();
return as;
}
int n, k, kol[M];
vector<int> v[M];
long long f[M], ans;
long long mlt;
int sz;
long long a[M], s[M];
long long u, kf[M], fact[M];
inline pair<vector<long long>, vector<long long> > func(int l, int r) {
if (r - l == 1) return {{s[l], (u * s[l]) % MOD}, {1, a[l]}};
int mid = (l + r) >> 1;
pair<vector<long long>, vector<long long> > a1 = func(l, mid);
pair<vector<long long>, vector<long long> > a2 = func(mid, r);
return make_pair(
sum(multiply(a1.first, a2.second), multiply(a1.second, a2.first)),
multiply(a1.second, a2.second));
}
long long dfs(int p, int pr) {
kol[p] = 1;
int szt = 0;
vector<long long> aa, sa;
for (int i : v[p])
if (pr != i) {
long long now = dfs(i, p);
kol[p] += kol[i];
aa.push_back(kol[i]);
sa.push_back(now);
}
sz = aa.size();
for (int i = 0; i < sz; i++) {
a[i] = aa[i];
s[i] = sa[i];
}
u = n - kol[p];
long long sum = 0;
for (int i = 0; i < sz; i++) sum = (sum + s[i]) % MOD;
long long sqr = 0;
for (int i = 0; i < sz; i++) sqr = (sqr + s[i] * s[i]) % MOD;
sqr = (sum * sum - sqr) % MOD;
if (sqr < 0) sqr += MOD;
ans = (ans + sqr * mlt) % MOD;
if (sz == 0) {
return 1;
}
pair<vector<long long>, vector<long long> > t = func(0, sz);
int s = (int)t.first.size();
for (int i = 0; i < min(s, k + 1); i++) {
ans = (ans + kf[i] * t.first[i]) % MOD;
sum = (sum + kf[i] * t.second[i]) % MOD;
}
return sum;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
if (k == 1) {
cout << (((long long)n * (long long)(n - 1)) / 2) % MOD;
return 0;
}
for (int i = 0; i < n - 1; i++) {
int s, f;
cin >> s >> f;
s--, f--;
v[s].push_back(f);
v[f].push_back(s);
}
fact[0] = 1;
for (int i = 0; i < k; i++)
fact[i + 1] = (fact[i] * (long long)(i + 1)) % MOD;
for (int i = 0; i <= k; i++)
kf[i] = (fact[k] * power(fact[k - i], MOD - 2)) % MOD;
ans = 0;
mlt = power(2, MOD - 2);
dfs(0, -1);
cout << ans;
return 0;
}
| ### Prompt
Construct a Cpp code solution to the problem outlined:
You are given a tree of n vertices. You are to select k (not necessarily distinct) simple paths in such a way that it is possible to split all edges of the tree into three sets: edges not contained in any path, edges that are a part of exactly one of these paths, and edges that are parts of all selected paths, and the latter set should be non-empty.
Compute the number of ways to select k paths modulo 998244353.
The paths are enumerated, in other words, two ways are considered distinct if there are such i (1 ≤ i ≤ k) and an edge that the i-th path contains the edge in one way and does not contain it in the other.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 10^{5}) — the number of vertices in the tree and the desired number of paths.
The next n - 1 lines describe edges of the tree. Each line contains two integers a and b (1 ≤ a, b ≤ n, a ≠ b) — the endpoints of an edge. It is guaranteed that the given edges form a tree.
Output
Print the number of ways to select k enumerated not necessarily distinct simple paths in such a way that for each edge either it is not contained in any path, or it is contained in exactly one path, or it is contained in all k paths, and the intersection of all paths is non-empty.
As the answer can be large, print it modulo 998244353.
Examples
Input
3 2
1 2
2 3
Output
7
Input
5 1
4 1
2 3
4 5
2 1
Output
10
Input
29 29
1 2
1 3
1 4
1 5
5 6
5 7
5 8
8 9
8 10
8 11
11 12
11 13
11 14
14 15
14 16
14 17
17 18
17 19
17 20
20 21
20 22
20 23
23 24
23 25
23 26
26 27
26 28
26 29
Output
125580756
Note
In the first example the following ways are valid:
* ((1,2), (1,2)),
* ((1,2), (1,3)),
* ((1,3), (1,2)),
* ((1,3), (1,3)),
* ((1,3), (2,3)),
* ((2,3), (1,3)),
* ((2,3), (2,3)).
In the second example k=1, so all n ⋅ (n - 1) / 2 = 5 ⋅ 4 / 2 = 10 paths are valid.
In the third example, the answer is ≥ 998244353, so it was taken modulo 998244353, don't forget it!
### Response
```cpp
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
mt19937 rnd(239);
const long double pi = acos(-1.0);
const long long INF = 1e18 + 239;
const int BIG = 1e9 + 239;
const int M = 2e5 + 239;
const int T = (1 << 19);
const long long MOD = 998244353;
const long long root = 3;
const long long sub = 15311432;
long long st[T];
inline long long power(long long a, long long b) {
if (b == 0) return 1;
long long t = power(a, (b >> 1));
t = (t * t) % MOD;
if (b & 1) return (t * a) % MOD;
return t;
}
inline int inv(int n, int b) {
int ans = 0;
for (int i = 0; i < b; i++) {
ans = (ans << 1) + (n & 1);
n >>= 1;
}
return ans;
}
int p;
long long w;
map<vector<long long>, vector<long long> > pq;
inline void fft(const vector<long long> &a, vector<long long> &b) {
if (pq.count(a)) {
b = pq[a];
return;
}
b.resize(a.size());
for (int i = 0; i < a.size(); i++) b[i] = a[inv(i, p)];
for (int z = 0; z < p; z++) {
int u = (1 << z);
for (int i = 0; i < (1 << p); i++) {
int ps = i >> z;
if ((ps & 1) == 0) {
int t = i & (u - 1);
long long bi = (b[i] + st[t << (p - z - 1)] * b[i + u]) % MOD;
long long bii = (b[i] + st[(t + u) << (p - z - 1)] * b[i + u]) % MOD;
b[i] = bi;
b[i + u] = bii;
}
}
}
pq[a] = b;
}
inline vector<long long> sum(vector<long long> a, vector<long long> b) {
int n = max((int)a.size(), (int)b.size());
vector<long long> ans(n, 0);
while (a.size() < n) a.push_back(0);
while (b.size() < n) b.push_back(0);
for (int i = 0; i < n; i++) ans[i] = (a[i] + b[i]) % MOD;
return ans;
}
vector<long long> ta, tb;
vector<long long> as;
inline vector<long long> multiply(vector<long long> a, vector<long long> b) {
int k = (a.size() + b.size() + 1);
p = trunc(log2(k)) + 1;
while (a.size() < (1 << p)) a.push_back(0);
while (b.size() < (1 << p)) b.push_back(0);
w = power(sub, (1 << (23 - p)));
st[0] = 1LL;
for (int i = 1; i < (1 << p); i++) st[i] = (st[i - 1] * w) % MOD;
fft(a, ta);
fft(b, tb);
vector<long long> v;
for (int i = 0; i < (1 << p); i++) v.push_back((ta[i] * tb[i]) % MOD);
fft(v, as);
long long d = power((1 << p), MOD - 2);
for (int i = 0; i < (1 << p); i++) as[i] = (d * as[i]) % MOD;
reverse(as.begin() + 1, as.end());
while (as.back() == 0) as.pop_back();
return as;
}
int n, k, kol[M];
vector<int> v[M];
long long f[M], ans;
long long mlt;
int sz;
long long a[M], s[M];
long long u, kf[M], fact[M];
inline pair<vector<long long>, vector<long long> > func(int l, int r) {
if (r - l == 1) return {{s[l], (u * s[l]) % MOD}, {1, a[l]}};
int mid = (l + r) >> 1;
pair<vector<long long>, vector<long long> > a1 = func(l, mid);
pair<vector<long long>, vector<long long> > a2 = func(mid, r);
return make_pair(
sum(multiply(a1.first, a2.second), multiply(a1.second, a2.first)),
multiply(a1.second, a2.second));
}
long long dfs(int p, int pr) {
kol[p] = 1;
int szt = 0;
vector<long long> aa, sa;
for (int i : v[p])
if (pr != i) {
long long now = dfs(i, p);
kol[p] += kol[i];
aa.push_back(kol[i]);
sa.push_back(now);
}
sz = aa.size();
for (int i = 0; i < sz; i++) {
a[i] = aa[i];
s[i] = sa[i];
}
u = n - kol[p];
long long sum = 0;
for (int i = 0; i < sz; i++) sum = (sum + s[i]) % MOD;
long long sqr = 0;
for (int i = 0; i < sz; i++) sqr = (sqr + s[i] * s[i]) % MOD;
sqr = (sum * sum - sqr) % MOD;
if (sqr < 0) sqr += MOD;
ans = (ans + sqr * mlt) % MOD;
if (sz == 0) {
return 1;
}
pair<vector<long long>, vector<long long> > t = func(0, sz);
int s = (int)t.first.size();
for (int i = 0; i < min(s, k + 1); i++) {
ans = (ans + kf[i] * t.first[i]) % MOD;
sum = (sum + kf[i] * t.second[i]) % MOD;
}
return sum;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
if (k == 1) {
cout << (((long long)n * (long long)(n - 1)) / 2) % MOD;
return 0;
}
for (int i = 0; i < n - 1; i++) {
int s, f;
cin >> s >> f;
s--, f--;
v[s].push_back(f);
v[f].push_back(s);
}
fact[0] = 1;
for (int i = 0; i < k; i++)
fact[i + 1] = (fact[i] * (long long)(i + 1)) % MOD;
for (int i = 0; i <= k; i++)
kf[i] = (fact[k] * power(fact[k - i], MOD - 2)) % MOD;
ans = 0;
mlt = power(2, MOD - 2);
dfs(0, -1);
cout << ans;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void smin(T &a, U b) {
if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, U b) {
if (a < b) a = b;
}
template <class T>
inline void gn(T &first) {
char c, sg = 0;
while (c = getchar(), (c > '9' || c < '0') && c != '-')
;
for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9';
c = getchar())
first = (first << 1) + (first << 3) + c - '0';
if (sg) first = -first;
}
template <class T, class T1>
inline void gn(T &first, T1 &second) {
gn(first);
gn(second);
}
template <class T, class T1, class T2>
inline void gn(T &first, T1 &second, T2 &z) {
gn(first);
gn(second);
gn(z);
}
template <class T>
inline void print(T first) {
if (first < 0) {
putchar('-');
return print(-first);
}
if (first < 10) {
putchar('0' + first);
return;
}
print(first / 10);
putchar(first % 10 + '0');
}
template <class T>
inline void printsp(T first) {
print(first);
putchar(' ');
}
template <class T>
inline void println(T first) {
print(first);
putchar('\n');
}
template <class T, class U>
inline void print(T first, U second) {
printsp(first);
println(second);
}
template <class T, class U, class V>
inline void print(T first, U second, V z) {
printsp(first);
printsp(second);
println(z);
}
int power(int a, int b, int m, int ans = 1) {
for (; b; b >>= 1, a = 1LL * a * a % m)
if (b & 1) ans = 1LL * ans * a % m;
return ans;
}
vector<int> adj[101010], child[101010], tmp;
int ans, sum[101010], n, sz[101010], N1, E, F, I;
int A[1 << 18], B[1 << 18], k, flag, tval[101010], vst[101010];
inline void add(int &u, int v) {
u += v;
if (u >= 998244353) u -= 998244353;
}
inline int mul(int a, int b) {
return (int)((long long)a * b % 998244353);
unsigned long long first = (long long)a * b;
unsigned xh = (unsigned)(first >> 32), xl = (unsigned)first, d, m;
asm("divl %4; \n\t" : "=a"(d), "=d"(m) : "d"(xh), "a"(xl), "r"(998244353));
return m;
}
void fft(int *A, int P, int pw) {
for (int m = N1, h; h = m >> 1, m >= 2; pw = (long long)pw * pw % P, m = h) {
for (int i = 0, w = 1; i < h; i++, w = (long long)w * pw % P) {
for (int j = i; j < N1; j += m) {
int k = j + h, first = A[j] + 998244353 - A[k];
if (first >= 998244353) first -= 998244353;
A[j] += A[k] - 998244353;
A[k] = mul(first, w);
if (A[j] < 0) A[j] += 998244353;
}
}
}
for (int i = 0, j = 1; j < N1 - 1; j++) {
for (int k = N1 / 2; k > (i ^= k); k >>= 1)
;
if (i > j) swap(A[i], A[j]);
}
}
vector<int> multiply(const vector<int> &a, const vector<int> &b) {
int n = a.size() + b.size() - 1;
for (N1 = 1; N1 < n; N1 <<= 1)
;
for (int i = 0; i < a.size(); i++) A[i] = a[i];
for (int i = a.size(); i < N1; i++) A[i] = 0;
for (int i = 0; i < b.size(); i++) B[i] = b[i];
for (int i = b.size(); i < N1; i++) B[i] = 0;
E = power(3, (998244353 - 1) / N1, 998244353);
F = power(E, 998244353 - 2, 998244353);
I = power(N1, 998244353 - 2, 998244353);
fft(A, 998244353, E);
fft(B, 998244353, E);
for (int i = 0; i < N1; i++) A[i] = (long long)A[i] * B[i] % 998244353;
fft(A, 998244353, F);
vector<int> c(n);
for (int i = 0; i < n; i++) c[i] = (long long)A[i] * I % 998244353;
return c;
}
vector<int> calc(int st, int ed) {
if (st == ed) return {1};
if (st + 1 == ed) return {1, sz[tmp[st]]};
return multiply(calc(st, ed + st >> 1), calc(st + ed >> 1, ed));
}
void dfs(int u, int pa = -1) {
sz[u] = 1;
for (int i = 0; i < adj[u].size(); i++) {
int v = adj[u][i];
if (v == pa) continue;
dfs(v, u);
child[u].push_back(v);
sz[u] += sz[v];
add(ans, mul(sum[u], sum[v]));
add(sum[u], sum[v]);
}
tmp = child[u];
vector<int> res = calc(0, tmp.size());
int mid = 1, val = 0;
for (int i = 0, ed = min(k, (int)res.size() - 1); i <= ed; i++) {
add(val, mul(mid, res[i]));
mid = mul(mid, k - i);
}
add(sum[u], val);
if (pa != -1) res = multiply(res, (vector<int>){1, n - sz[u]});
++flag;
for (int i = 0; i < child[u].size(); i++) {
int v = child[u][i], a = sz[v];
int &m = tval[a];
if (vst[a] != flag) {
vst[a] = flag;
int cur = 0, b = 1;
m = 0;
for (int j = 0, ed = min(k, (int)res.size() - 1); j <= ed; j++) {
cur = mul(cur, a);
cur = (res[j] - cur + 998244353) % 998244353;
add(m, mul(b, cur));
b = mul(b, k - j);
}
}
add(ans, mul(m, sum[v]));
}
}
int main() {
int u, v;
gn(n, k);
for (int i = 1; i < n; i++) {
gn(u, v);
adj[u].push_back(v);
adj[v].push_back(u);
}
if (k == 1) {
println((long long)n * (n - 1) / 2 % 998244353);
return 0;
}
dfs(1);
println(ans);
return 0;
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
You are given a tree of n vertices. You are to select k (not necessarily distinct) simple paths in such a way that it is possible to split all edges of the tree into three sets: edges not contained in any path, edges that are a part of exactly one of these paths, and edges that are parts of all selected paths, and the latter set should be non-empty.
Compute the number of ways to select k paths modulo 998244353.
The paths are enumerated, in other words, two ways are considered distinct if there are such i (1 ≤ i ≤ k) and an edge that the i-th path contains the edge in one way and does not contain it in the other.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 10^{5}) — the number of vertices in the tree and the desired number of paths.
The next n - 1 lines describe edges of the tree. Each line contains two integers a and b (1 ≤ a, b ≤ n, a ≠ b) — the endpoints of an edge. It is guaranteed that the given edges form a tree.
Output
Print the number of ways to select k enumerated not necessarily distinct simple paths in such a way that for each edge either it is not contained in any path, or it is contained in exactly one path, or it is contained in all k paths, and the intersection of all paths is non-empty.
As the answer can be large, print it modulo 998244353.
Examples
Input
3 2
1 2
2 3
Output
7
Input
5 1
4 1
2 3
4 5
2 1
Output
10
Input
29 29
1 2
1 3
1 4
1 5
5 6
5 7
5 8
8 9
8 10
8 11
11 12
11 13
11 14
14 15
14 16
14 17
17 18
17 19
17 20
20 21
20 22
20 23
23 24
23 25
23 26
26 27
26 28
26 29
Output
125580756
Note
In the first example the following ways are valid:
* ((1,2), (1,2)),
* ((1,2), (1,3)),
* ((1,3), (1,2)),
* ((1,3), (1,3)),
* ((1,3), (2,3)),
* ((2,3), (1,3)),
* ((2,3), (2,3)).
In the second example k=1, so all n ⋅ (n - 1) / 2 = 5 ⋅ 4 / 2 = 10 paths are valid.
In the third example, the answer is ≥ 998244353, so it was taken modulo 998244353, don't forget it!
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void smin(T &a, U b) {
if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, U b) {
if (a < b) a = b;
}
template <class T>
inline void gn(T &first) {
char c, sg = 0;
while (c = getchar(), (c > '9' || c < '0') && c != '-')
;
for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9';
c = getchar())
first = (first << 1) + (first << 3) + c - '0';
if (sg) first = -first;
}
template <class T, class T1>
inline void gn(T &first, T1 &second) {
gn(first);
gn(second);
}
template <class T, class T1, class T2>
inline void gn(T &first, T1 &second, T2 &z) {
gn(first);
gn(second);
gn(z);
}
template <class T>
inline void print(T first) {
if (first < 0) {
putchar('-');
return print(-first);
}
if (first < 10) {
putchar('0' + first);
return;
}
print(first / 10);
putchar(first % 10 + '0');
}
template <class T>
inline void printsp(T first) {
print(first);
putchar(' ');
}
template <class T>
inline void println(T first) {
print(first);
putchar('\n');
}
template <class T, class U>
inline void print(T first, U second) {
printsp(first);
println(second);
}
template <class T, class U, class V>
inline void print(T first, U second, V z) {
printsp(first);
printsp(second);
println(z);
}
int power(int a, int b, int m, int ans = 1) {
for (; b; b >>= 1, a = 1LL * a * a % m)
if (b & 1) ans = 1LL * ans * a % m;
return ans;
}
vector<int> adj[101010], child[101010], tmp;
int ans, sum[101010], n, sz[101010], N1, E, F, I;
int A[1 << 18], B[1 << 18], k, flag, tval[101010], vst[101010];
inline void add(int &u, int v) {
u += v;
if (u >= 998244353) u -= 998244353;
}
inline int mul(int a, int b) {
return (int)((long long)a * b % 998244353);
unsigned long long first = (long long)a * b;
unsigned xh = (unsigned)(first >> 32), xl = (unsigned)first, d, m;
asm("divl %4; \n\t" : "=a"(d), "=d"(m) : "d"(xh), "a"(xl), "r"(998244353));
return m;
}
void fft(int *A, int P, int pw) {
for (int m = N1, h; h = m >> 1, m >= 2; pw = (long long)pw * pw % P, m = h) {
for (int i = 0, w = 1; i < h; i++, w = (long long)w * pw % P) {
for (int j = i; j < N1; j += m) {
int k = j + h, first = A[j] + 998244353 - A[k];
if (first >= 998244353) first -= 998244353;
A[j] += A[k] - 998244353;
A[k] = mul(first, w);
if (A[j] < 0) A[j] += 998244353;
}
}
}
for (int i = 0, j = 1; j < N1 - 1; j++) {
for (int k = N1 / 2; k > (i ^= k); k >>= 1)
;
if (i > j) swap(A[i], A[j]);
}
}
vector<int> multiply(const vector<int> &a, const vector<int> &b) {
int n = a.size() + b.size() - 1;
for (N1 = 1; N1 < n; N1 <<= 1)
;
for (int i = 0; i < a.size(); i++) A[i] = a[i];
for (int i = a.size(); i < N1; i++) A[i] = 0;
for (int i = 0; i < b.size(); i++) B[i] = b[i];
for (int i = b.size(); i < N1; i++) B[i] = 0;
E = power(3, (998244353 - 1) / N1, 998244353);
F = power(E, 998244353 - 2, 998244353);
I = power(N1, 998244353 - 2, 998244353);
fft(A, 998244353, E);
fft(B, 998244353, E);
for (int i = 0; i < N1; i++) A[i] = (long long)A[i] * B[i] % 998244353;
fft(A, 998244353, F);
vector<int> c(n);
for (int i = 0; i < n; i++) c[i] = (long long)A[i] * I % 998244353;
return c;
}
vector<int> calc(int st, int ed) {
if (st == ed) return {1};
if (st + 1 == ed) return {1, sz[tmp[st]]};
return multiply(calc(st, ed + st >> 1), calc(st + ed >> 1, ed));
}
void dfs(int u, int pa = -1) {
sz[u] = 1;
for (int i = 0; i < adj[u].size(); i++) {
int v = adj[u][i];
if (v == pa) continue;
dfs(v, u);
child[u].push_back(v);
sz[u] += sz[v];
add(ans, mul(sum[u], sum[v]));
add(sum[u], sum[v]);
}
tmp = child[u];
vector<int> res = calc(0, tmp.size());
int mid = 1, val = 0;
for (int i = 0, ed = min(k, (int)res.size() - 1); i <= ed; i++) {
add(val, mul(mid, res[i]));
mid = mul(mid, k - i);
}
add(sum[u], val);
if (pa != -1) res = multiply(res, (vector<int>){1, n - sz[u]});
++flag;
for (int i = 0; i < child[u].size(); i++) {
int v = child[u][i], a = sz[v];
int &m = tval[a];
if (vst[a] != flag) {
vst[a] = flag;
int cur = 0, b = 1;
m = 0;
for (int j = 0, ed = min(k, (int)res.size() - 1); j <= ed; j++) {
cur = mul(cur, a);
cur = (res[j] - cur + 998244353) % 998244353;
add(m, mul(b, cur));
b = mul(b, k - j);
}
}
add(ans, mul(m, sum[v]));
}
}
int main() {
int u, v;
gn(n, k);
for (int i = 1; i < n; i++) {
gn(u, v);
adj[u].push_back(v);
adj[v].push_back(u);
}
if (k == 1) {
println((long long)n * (n - 1) / 2 % 998244353);
return 0;
}
dfs(1);
println(ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const int MOD = 998244353;
const int g = 3;
inline int Mul(int a, int b) {
unsigned long long x = (long long)a * b;
unsigned xh = (unsigned)(x >> 32), xl = (unsigned)x, d, m;
asm("divl %4;\n\t" : "=a"(d), "=d"(m) : "d"(xh), "a"(xl), "r"(MOD));
return m;
}
int Qpow(int x, int y = MOD - 2) {
int res = 1;
for (; y; y >>= 1, x = Mul(x, x))
if (y & 1) res = Mul(res, x);
return res;
}
int n, K, jc[N], njc[N];
int Ar(int n, int m) { return Mul(jc[n], njc[n - m]); }
int sz[N], sm[N];
vector<int> G[N];
int U(int x, int y) { return ((x += y) >= MOD) ? (x - MOD) : x; }
void SU(int& x, int y) { ((x += y) >= MOD) ? (x -= MOD) : 0; }
int ans;
vector<int> t;
int w[N] = {1};
void FFT(vector<int>& A, bool fl) {
int L = A.size();
if (fl) {
int t = Qpow(L);
for (int& v : A) v = Mul(v, t);
reverse(A.begin() + 1, A.end());
}
for (int i = 1, j = L >> 1, k; i < L; ++i, j ^= k) {
if (i < j) swap(A[i], A[j]);
k = L >> 1;
while (j & k) {
j ^= k;
k >>= 1;
}
}
for (int i = 1; i < L; i <<= 1) {
int t = Qpow(g, (MOD - 1) / (i << 1));
for (int j = 1; j < i; ++j) w[j] = Mul(w[j - 1], t);
for (int j = 0; j < L; j += (i << 1)) {
for (int k = 0; k < i; ++k) {
t = Mul(A[i + j + k], w[k]);
A[i + j + k] = U(A[j + k], MOD - t);
SU(A[j + k], t);
}
}
}
}
vector<int> operator*(vector<int> A, vector<int> B) {
int need = A.size() + B.size() - 1, L;
for (L = 1; L < need; L <<= 1)
;
A.resize(L);
FFT(A, false);
B.resize(L);
FFT(B, false);
for (int i = 0; i < L; ++i) A[i] = Mul(A[i], B[i]);
FFT(A, true);
A.resize(min(K + 1, need));
return A;
}
vector<int> DivCalc(int l, int r) {
if (l < r)
return DivCalc(l, (l + r) >> 1) * DivCalc(((l + r) >> 1) + 1, r);
else if (l == r)
return {1, t[l]};
else
return {1};
}
int Dark(vector<int> p, int x, int y) {
for (int i = 1; i <= (int)p.size() - 1; ++i) SU(p[i], MOD - Mul(y, p[i - 1]));
for (int i = p.size() - 1; i > 0; --i) SU(p[i], Mul(x, p[i - 1]));
int res = 0;
for (int i = 1; i < (int)p.size(); ++i) SU(res, Mul(p[i], Ar(K, i)));
return res;
}
int rec[N], vis[N], cc;
void Dfs(int u, int fa = 0) {
sz[u] = sm[u] = 1;
for (int v : G[u])
if (v != fa) {
Dfs(v, u);
SU(ans, Mul(sm[u], sm[v]));
SU(sm[u], sm[v]);
sz[u] += sz[v];
}
t.clear();
for (int v : G[u])
if (v != fa) t.emplace_back(sz[v]);
static vector<int> p;
p = DivCalc(0, t.size() - 1);
if ((int)p.size() - 1 > K) p.resize(K + 1);
for (int i = 1; i < (int)p.size(); ++i) SU(sm[u], Mul(Ar(K, i), p[i]));
++cc;
for (int v : G[u])
if (v != fa) {
if (vis[sz[v]] != cc) {
rec[sz[v]] = Dark(p, n - sz[u], sz[v]);
vis[sz[v]] = cc;
}
SU(ans, Mul(rec[sz[v]], sm[v]));
}
}
void Prework() {
jc[0] = 1;
for (int i = 1; i <= K; ++i) jc[i] = Mul(jc[i - 1], i);
njc[K] = Qpow(jc[K]);
for (int i = K - 1; ~i; --i) njc[i] = Mul(njc[i + 1], i + 1);
}
int main() {
scanf("%d%d", &n, &K);
if (K == 1) {
printf("%lld\n", (long long)n * (n - 1) / 2 % MOD);
return 0;
}
Prework();
for (int i = 1, u, v; i < n; ++i) {
scanf("%d%d", &u, &v);
G[u].emplace_back(v);
G[v].emplace_back(u);
}
Dfs(1);
printf("%d\n", ans);
return 0;
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
You are given a tree of n vertices. You are to select k (not necessarily distinct) simple paths in such a way that it is possible to split all edges of the tree into three sets: edges not contained in any path, edges that are a part of exactly one of these paths, and edges that are parts of all selected paths, and the latter set should be non-empty.
Compute the number of ways to select k paths modulo 998244353.
The paths are enumerated, in other words, two ways are considered distinct if there are such i (1 ≤ i ≤ k) and an edge that the i-th path contains the edge in one way and does not contain it in the other.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 10^{5}) — the number of vertices in the tree and the desired number of paths.
The next n - 1 lines describe edges of the tree. Each line contains two integers a and b (1 ≤ a, b ≤ n, a ≠ b) — the endpoints of an edge. It is guaranteed that the given edges form a tree.
Output
Print the number of ways to select k enumerated not necessarily distinct simple paths in such a way that for each edge either it is not contained in any path, or it is contained in exactly one path, or it is contained in all k paths, and the intersection of all paths is non-empty.
As the answer can be large, print it modulo 998244353.
Examples
Input
3 2
1 2
2 3
Output
7
Input
5 1
4 1
2 3
4 5
2 1
Output
10
Input
29 29
1 2
1 3
1 4
1 5
5 6
5 7
5 8
8 9
8 10
8 11
11 12
11 13
11 14
14 15
14 16
14 17
17 18
17 19
17 20
20 21
20 22
20 23
23 24
23 25
23 26
26 27
26 28
26 29
Output
125580756
Note
In the first example the following ways are valid:
* ((1,2), (1,2)),
* ((1,2), (1,3)),
* ((1,3), (1,2)),
* ((1,3), (1,3)),
* ((1,3), (2,3)),
* ((2,3), (1,3)),
* ((2,3), (2,3)).
In the second example k=1, so all n ⋅ (n - 1) / 2 = 5 ⋅ 4 / 2 = 10 paths are valid.
In the third example, the answer is ≥ 998244353, so it was taken modulo 998244353, don't forget it!
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const int MOD = 998244353;
const int g = 3;
inline int Mul(int a, int b) {
unsigned long long x = (long long)a * b;
unsigned xh = (unsigned)(x >> 32), xl = (unsigned)x, d, m;
asm("divl %4;\n\t" : "=a"(d), "=d"(m) : "d"(xh), "a"(xl), "r"(MOD));
return m;
}
int Qpow(int x, int y = MOD - 2) {
int res = 1;
for (; y; y >>= 1, x = Mul(x, x))
if (y & 1) res = Mul(res, x);
return res;
}
int n, K, jc[N], njc[N];
int Ar(int n, int m) { return Mul(jc[n], njc[n - m]); }
int sz[N], sm[N];
vector<int> G[N];
int U(int x, int y) { return ((x += y) >= MOD) ? (x - MOD) : x; }
void SU(int& x, int y) { ((x += y) >= MOD) ? (x -= MOD) : 0; }
int ans;
vector<int> t;
int w[N] = {1};
void FFT(vector<int>& A, bool fl) {
int L = A.size();
if (fl) {
int t = Qpow(L);
for (int& v : A) v = Mul(v, t);
reverse(A.begin() + 1, A.end());
}
for (int i = 1, j = L >> 1, k; i < L; ++i, j ^= k) {
if (i < j) swap(A[i], A[j]);
k = L >> 1;
while (j & k) {
j ^= k;
k >>= 1;
}
}
for (int i = 1; i < L; i <<= 1) {
int t = Qpow(g, (MOD - 1) / (i << 1));
for (int j = 1; j < i; ++j) w[j] = Mul(w[j - 1], t);
for (int j = 0; j < L; j += (i << 1)) {
for (int k = 0; k < i; ++k) {
t = Mul(A[i + j + k], w[k]);
A[i + j + k] = U(A[j + k], MOD - t);
SU(A[j + k], t);
}
}
}
}
vector<int> operator*(vector<int> A, vector<int> B) {
int need = A.size() + B.size() - 1, L;
for (L = 1; L < need; L <<= 1)
;
A.resize(L);
FFT(A, false);
B.resize(L);
FFT(B, false);
for (int i = 0; i < L; ++i) A[i] = Mul(A[i], B[i]);
FFT(A, true);
A.resize(min(K + 1, need));
return A;
}
vector<int> DivCalc(int l, int r) {
if (l < r)
return DivCalc(l, (l + r) >> 1) * DivCalc(((l + r) >> 1) + 1, r);
else if (l == r)
return {1, t[l]};
else
return {1};
}
int Dark(vector<int> p, int x, int y) {
for (int i = 1; i <= (int)p.size() - 1; ++i) SU(p[i], MOD - Mul(y, p[i - 1]));
for (int i = p.size() - 1; i > 0; --i) SU(p[i], Mul(x, p[i - 1]));
int res = 0;
for (int i = 1; i < (int)p.size(); ++i) SU(res, Mul(p[i], Ar(K, i)));
return res;
}
int rec[N], vis[N], cc;
void Dfs(int u, int fa = 0) {
sz[u] = sm[u] = 1;
for (int v : G[u])
if (v != fa) {
Dfs(v, u);
SU(ans, Mul(sm[u], sm[v]));
SU(sm[u], sm[v]);
sz[u] += sz[v];
}
t.clear();
for (int v : G[u])
if (v != fa) t.emplace_back(sz[v]);
static vector<int> p;
p = DivCalc(0, t.size() - 1);
if ((int)p.size() - 1 > K) p.resize(K + 1);
for (int i = 1; i < (int)p.size(); ++i) SU(sm[u], Mul(Ar(K, i), p[i]));
++cc;
for (int v : G[u])
if (v != fa) {
if (vis[sz[v]] != cc) {
rec[sz[v]] = Dark(p, n - sz[u], sz[v]);
vis[sz[v]] = cc;
}
SU(ans, Mul(rec[sz[v]], sm[v]));
}
}
void Prework() {
jc[0] = 1;
for (int i = 1; i <= K; ++i) jc[i] = Mul(jc[i - 1], i);
njc[K] = Qpow(jc[K]);
for (int i = K - 1; ~i; --i) njc[i] = Mul(njc[i + 1], i + 1);
}
int main() {
scanf("%d%d", &n, &K);
if (K == 1) {
printf("%lld\n", (long long)n * (n - 1) / 2 % MOD);
return 0;
}
Prework();
for (int i = 1, u, v; i < n; ++i) {
scanf("%d%d", &u, &v);
G[u].emplace_back(v);
G[v].emplace_back(u);
}
Dfs(1);
printf("%d\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(228);
const int M = 998244353;
const int P = 15311432;
const int B = (1 << 23);
const int N = 1e6 + 7;
const int K = 18;
int w[K][N];
int fact[N];
int rev_fact[N];
int sz[N];
int dp[N];
vector<int> g[N];
vector<int> ans[N];
vector<int> res[N];
inline int add(int a, int b) {
return (a + b >= M ? a + b - M : a + b < 0 ? a + b + M : a + b);
}
inline int mul(int a, int b) { return (a * (long long)b) % M; }
inline int bin(int a, int n) {
int res = 1;
while (n) {
if (n % 2 == 0) {
a = mul(a, a);
n /= 2;
} else {
res = mul(res, a);
n--;
}
}
return res;
}
int rev(int x, int bit) {
int cur = 0;
for (int i = 0; i < bit; i++) {
if ((x >> i) & 1) {
cur |= (1 << (bit - 1 - i));
}
}
return cur;
}
vector<int> fft(const vector<int> &a, int bit) {
int n = a.size();
vector<int> f(n);
for (int i = 0; i < n; i++) {
f[rev(i, bit)] = a[i];
}
int bin = 0;
for (int len = 1; len < n; len *= 2) {
for (int st = 0; st < n; st += 2 * len) {
for (int i = st; i < st + len; i++) {
int t = f[i], b = mul(f[i + len], w[bin][i - st]);
f[i] = add(t, b);
f[i + len] = add(t, -b);
}
}
bin++;
}
return f;
}
vector<int> mul(vector<int> a, vector<int> b) {
int n = a.size(), m = b.size();
int k = n + m;
int len = 1;
int cnt = 0;
while (len < k) {
len *= 2;
cnt++;
}
k = len;
while ((int)a.size() < k) {
a.push_back(0);
}
while ((int)b.size() < k) {
b.push_back(0);
}
a = fft(a, cnt);
b = fft(b, cnt);
for (int i = 0; i < k; i++) {
a[i] = mul(a[i], b[i]);
}
a = fft(a, cnt);
int rev = bin(k, M - 2);
for (int i = 0; i < k; i++) {
a[i] = mul(a[i], rev);
}
reverse(a.begin() + 1, a.end());
while (!a.empty() && a.back() == 0) {
a.pop_back();
}
return a;
}
vector<int> solve(int l, int r, const vector<int> &sz) {
if (r - l == 1) {
return {1, sz[l]};
} else {
int m = (l + r) / 2;
return mul(solve(l, m, sz), solve(m, r, sz));
}
}
int ret = 0;
int n, k;
void dfs(int v, int pr) {
sz[v] = 1;
vector<int> cur;
for (int to : g[v]) {
if (to != pr) {
dfs(to, v);
sz[v] += sz[to];
cur.push_back(sz[to]);
}
}
int have = (int)cur.size();
if (!cur.empty()) {
cur = solve(0, have, cur);
} else {
cur = {1};
}
ans[v] = cur;
for (int i = 0; i < (int)cur.size() && i <= k; i++) {
int vert = k - i;
int ans = cur[i];
int cnt = mul(fact[k], mul(ans, rev_fact[vert]));
dp[v] = add(dp[v], cnt);
}
}
void zhfs(int v, int pr, int have, int del_sum) {
ret = add(ret, -mul(dp[v], del_sum));
ret = add(ret, mul(dp[v], have));
vector<int> new_cur = ans[v];
new_cur.push_back(0);
for (int i = (int)new_cur.size() - 2; i >= 0; i--) {
new_cur[i + 1] = add(new_cur[i + 1], mul(new_cur[i], n - sz[v]));
}
map<int, int> press;
for (int to : g[v]) {
if (to != pr) {
if (!press.count(sz[to])) {
int calc_dp = 0;
vector<int> me = new_cur;
vector<int> cur = me;
for (int i = 0; i < (int)cur.size(); i++) {
if (i == 0) {
cur[i] = me[i];
} else {
cur[i] = add(me[i], -mul(cur[i - 1], sz[to]));
}
}
for (int i = 0; i < (int)cur.size() && i <= k; i++) {
int vert = k - i;
int ans = cur[i];
int cnt = mul(fact[k], mul(ans, rev_fact[vert]));
calc_dp = add(calc_dp, cnt);
}
press[sz[to]] = calc_dp;
}
}
}
for (int to : g[v]) {
if (to != pr) {
zhfs(to, v, add(have, press[sz[to]]), add(del_sum, dp[v]));
}
}
}
int main() {
for (int j = 0; j < K; j++) {
int cur = (1 << (j + 1));
w[j][0] = 1;
w[j][1] = P;
for (int i = cur; i < B; i *= 2) {
w[j][1] = mul(w[j][1], w[j][1]);
}
for (int i = 2; i < cur; i++) {
w[j][i] = mul(w[j][i - 1], w[j][1]);
}
}
fact[0] = 1;
for (int i = 1; i < N; i++) {
fact[i] = mul(fact[i - 1], i);
}
rev_fact[N - 1] = bin(fact[N - 1], M - 2);
for (int i = N - 2; i >= 0; i--) {
rev_fact[i] = mul(rev_fact[i + 1], i + 1);
}
cin >> n >> k;
if (k == 1) {
cout << n * (long long)(n - 1) / 2 % M << '\n';
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);
}
dfs(0, -1);
int me = 0;
for (int i = 0; i < n; i++) {
ret = add(ret, mul(me, dp[i]));
me = add(me, dp[i]);
}
zhfs(0, -1, 0, 0);
cout << ret << '\n';
}
| ### Prompt
Construct a Cpp code solution to the problem outlined:
You are given a tree of n vertices. You are to select k (not necessarily distinct) simple paths in such a way that it is possible to split all edges of the tree into three sets: edges not contained in any path, edges that are a part of exactly one of these paths, and edges that are parts of all selected paths, and the latter set should be non-empty.
Compute the number of ways to select k paths modulo 998244353.
The paths are enumerated, in other words, two ways are considered distinct if there are such i (1 ≤ i ≤ k) and an edge that the i-th path contains the edge in one way and does not contain it in the other.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 10^{5}) — the number of vertices in the tree and the desired number of paths.
The next n - 1 lines describe edges of the tree. Each line contains two integers a and b (1 ≤ a, b ≤ n, a ≠ b) — the endpoints of an edge. It is guaranteed that the given edges form a tree.
Output
Print the number of ways to select k enumerated not necessarily distinct simple paths in such a way that for each edge either it is not contained in any path, or it is contained in exactly one path, or it is contained in all k paths, and the intersection of all paths is non-empty.
As the answer can be large, print it modulo 998244353.
Examples
Input
3 2
1 2
2 3
Output
7
Input
5 1
4 1
2 3
4 5
2 1
Output
10
Input
29 29
1 2
1 3
1 4
1 5
5 6
5 7
5 8
8 9
8 10
8 11
11 12
11 13
11 14
14 15
14 16
14 17
17 18
17 19
17 20
20 21
20 22
20 23
23 24
23 25
23 26
26 27
26 28
26 29
Output
125580756
Note
In the first example the following ways are valid:
* ((1,2), (1,2)),
* ((1,2), (1,3)),
* ((1,3), (1,2)),
* ((1,3), (1,3)),
* ((1,3), (2,3)),
* ((2,3), (1,3)),
* ((2,3), (2,3)).
In the second example k=1, so all n ⋅ (n - 1) / 2 = 5 ⋅ 4 / 2 = 10 paths are valid.
In the third example, the answer is ≥ 998244353, so it was taken modulo 998244353, don't forget it!
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(228);
const int M = 998244353;
const int P = 15311432;
const int B = (1 << 23);
const int N = 1e6 + 7;
const int K = 18;
int w[K][N];
int fact[N];
int rev_fact[N];
int sz[N];
int dp[N];
vector<int> g[N];
vector<int> ans[N];
vector<int> res[N];
inline int add(int a, int b) {
return (a + b >= M ? a + b - M : a + b < 0 ? a + b + M : a + b);
}
inline int mul(int a, int b) { return (a * (long long)b) % M; }
inline int bin(int a, int n) {
int res = 1;
while (n) {
if (n % 2 == 0) {
a = mul(a, a);
n /= 2;
} else {
res = mul(res, a);
n--;
}
}
return res;
}
int rev(int x, int bit) {
int cur = 0;
for (int i = 0; i < bit; i++) {
if ((x >> i) & 1) {
cur |= (1 << (bit - 1 - i));
}
}
return cur;
}
vector<int> fft(const vector<int> &a, int bit) {
int n = a.size();
vector<int> f(n);
for (int i = 0; i < n; i++) {
f[rev(i, bit)] = a[i];
}
int bin = 0;
for (int len = 1; len < n; len *= 2) {
for (int st = 0; st < n; st += 2 * len) {
for (int i = st; i < st + len; i++) {
int t = f[i], b = mul(f[i + len], w[bin][i - st]);
f[i] = add(t, b);
f[i + len] = add(t, -b);
}
}
bin++;
}
return f;
}
vector<int> mul(vector<int> a, vector<int> b) {
int n = a.size(), m = b.size();
int k = n + m;
int len = 1;
int cnt = 0;
while (len < k) {
len *= 2;
cnt++;
}
k = len;
while ((int)a.size() < k) {
a.push_back(0);
}
while ((int)b.size() < k) {
b.push_back(0);
}
a = fft(a, cnt);
b = fft(b, cnt);
for (int i = 0; i < k; i++) {
a[i] = mul(a[i], b[i]);
}
a = fft(a, cnt);
int rev = bin(k, M - 2);
for (int i = 0; i < k; i++) {
a[i] = mul(a[i], rev);
}
reverse(a.begin() + 1, a.end());
while (!a.empty() && a.back() == 0) {
a.pop_back();
}
return a;
}
vector<int> solve(int l, int r, const vector<int> &sz) {
if (r - l == 1) {
return {1, sz[l]};
} else {
int m = (l + r) / 2;
return mul(solve(l, m, sz), solve(m, r, sz));
}
}
int ret = 0;
int n, k;
void dfs(int v, int pr) {
sz[v] = 1;
vector<int> cur;
for (int to : g[v]) {
if (to != pr) {
dfs(to, v);
sz[v] += sz[to];
cur.push_back(sz[to]);
}
}
int have = (int)cur.size();
if (!cur.empty()) {
cur = solve(0, have, cur);
} else {
cur = {1};
}
ans[v] = cur;
for (int i = 0; i < (int)cur.size() && i <= k; i++) {
int vert = k - i;
int ans = cur[i];
int cnt = mul(fact[k], mul(ans, rev_fact[vert]));
dp[v] = add(dp[v], cnt);
}
}
void zhfs(int v, int pr, int have, int del_sum) {
ret = add(ret, -mul(dp[v], del_sum));
ret = add(ret, mul(dp[v], have));
vector<int> new_cur = ans[v];
new_cur.push_back(0);
for (int i = (int)new_cur.size() - 2; i >= 0; i--) {
new_cur[i + 1] = add(new_cur[i + 1], mul(new_cur[i], n - sz[v]));
}
map<int, int> press;
for (int to : g[v]) {
if (to != pr) {
if (!press.count(sz[to])) {
int calc_dp = 0;
vector<int> me = new_cur;
vector<int> cur = me;
for (int i = 0; i < (int)cur.size(); i++) {
if (i == 0) {
cur[i] = me[i];
} else {
cur[i] = add(me[i], -mul(cur[i - 1], sz[to]));
}
}
for (int i = 0; i < (int)cur.size() && i <= k; i++) {
int vert = k - i;
int ans = cur[i];
int cnt = mul(fact[k], mul(ans, rev_fact[vert]));
calc_dp = add(calc_dp, cnt);
}
press[sz[to]] = calc_dp;
}
}
}
for (int to : g[v]) {
if (to != pr) {
zhfs(to, v, add(have, press[sz[to]]), add(del_sum, dp[v]));
}
}
}
int main() {
for (int j = 0; j < K; j++) {
int cur = (1 << (j + 1));
w[j][0] = 1;
w[j][1] = P;
for (int i = cur; i < B; i *= 2) {
w[j][1] = mul(w[j][1], w[j][1]);
}
for (int i = 2; i < cur; i++) {
w[j][i] = mul(w[j][i - 1], w[j][1]);
}
}
fact[0] = 1;
for (int i = 1; i < N; i++) {
fact[i] = mul(fact[i - 1], i);
}
rev_fact[N - 1] = bin(fact[N - 1], M - 2);
for (int i = N - 2; i >= 0; i--) {
rev_fact[i] = mul(rev_fact[i + 1], i + 1);
}
cin >> n >> k;
if (k == 1) {
cout << n * (long long)(n - 1) / 2 % M << '\n';
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);
}
dfs(0, -1);
int me = 0;
for (int i = 0; i < n; i++) {
ret = add(ret, mul(me, dp[i]));
me = add(me, dp[i]);
}
zhfs(0, -1, 0, 0);
cout << ret << '\n';
}
``` |
#include <bits/stdc++.h>
using namespace std;
namespace Base {
const int inf = 0x3f3f3f3f, INF = 0x7fffffff;
const long long infll = 0x3f3f3f3f3f3f3f3fll, INFll = 0x7fffffffffffffffll;
template <typename T>
void read(T &x) {
x = 0;
int fh = 1;
double num = 1.0;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') fh = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
if (ch == '.') {
ch = getchar();
while (isdigit(ch)) {
num /= 10;
x = x + num * (ch - '0');
ch = getchar();
}
}
x = x * fh;
}
template <typename T>
void chmax(T &x, T y) {
x = x < y ? y : x;
}
template <typename T>
void chmin(T &x, T y) {
x = x > y ? y : x;
}
} // namespace Base
using namespace Base;
const int P = 998244353, G = 3, N = 500010;
struct Edge {
int data, next;
} e[N * 2];
int a[N], b[N], c[N], num[N], nxt[N], p[N], size[N], dad[N], head[N], place, k,
mul[N], inv[N], n, s[N], ans;
map<int, int> mp[N];
int power(int x, int y) {
int i = x;
x = 1;
while (y > 0) {
if (y % 2 == 1) x = 1ll * i * x % P;
i = 1ll * i * i % P;
y /= 2;
}
return x;
}
int C(int n, int m) {
if (m > n) return 0;
return 1ll * mul[n] * inv[n - m] % P;
}
void update(int &x, int y) {
x += y;
x = (x >= P) ? x - P : x;
}
void build(int u, int v) {
e[++place].data = v;
e[place].next = head[u];
head[u] = place;
}
void NTT(int *a, int l, int tag) {
for (int i = 0, j = 0; i < l; i++) {
if (i > j) swap(a[i], a[j]);
for (int k = (l >> 1); (j ^= k) < k; k >>= 1)
;
}
for (int i = 1; i < l; i <<= 1) {
int wn = power(G, (P - 1) / (i * 2));
if (tag == -1) wn = power(wn, P - 2);
for (int j = 0; j < l; j += i + i) {
int w = 1;
for (int k = 0; k < i; k++, w = 1ll * w * wn % P) {
int x = a[k + j], y = 1ll * w * a[k + j + i] % P;
a[k + j] = (x + y) % P, a[k + j + i] = (x - y + P) % P;
}
}
}
if (tag == -1) {
int in = power(l, P - 2);
for (int i = 0; i < l; i++) a[i] = 1ll * a[i] * in % P;
}
}
void doit(int *t1, int *t2, int *c, int l) {
for (int i = 0; i < l; i++) a[i] = t1[i], b[i] = t2[i];
for (int i = l; i < 2 * l; i++) a[i] = b[i] = 0;
for (int i = 0; i < 2 * l; i++) c[i] = 0;
NTT(a, l * 2, 1);
NTT(b, l * 2, 1);
for (int i = 0; i < 2 * l; i++) c[i] = 1ll * a[i] * b[i] % P;
NTT(c, l * 2, -1);
}
void dfs(int x, int fa) {
size[x] = 1;
dad[x] = fa;
int id = 0;
for (int ed = head[x]; ed != 0; ed = e[ed].next)
if (e[ed].data != fa) {
dfs(e[ed].data, x);
size[x] += size[e[ed].data];
update(s[x], s[e[ed].data]);
}
for (int ed = head[x]; ed != 0; ed = e[ed].next)
if (e[ed].data != fa) {
num[id] = 1, num[id + 1] = size[e[ed].data];
id += 2;
}
int tmp = 1;
while (tmp < id) tmp <<= 1;
for (int i = id; i < tmp; i += 2) num[i] = 1, num[i + 1] = 0;
id = tmp;
for (int i = 2; i < tmp; i <<= 1) {
for (int k = 0; k < tmp; k += 2 * i) {
doit(num + k, num + k + i, nxt, i);
for (int t = 0; t < 2 * i; t++) num[k + t] = nxt[t];
}
}
for (int i = 0; i <= min(k, id - 1); i++)
update(p[x], 1ll * num[i] * C(k, i) % P);
for (int i = 0; i < id + 2; i++) nxt[i] = 0;
for (int i = 0; i < id; i++) {
update(nxt[i], num[i]);
update(nxt[i + 1], 1ll * num[i] * (n - size[x]) % P);
}
id += 2;
for (int i = 0; i < id; i++) num[i] = nxt[i];
for (int ed = head[x]; ed != 0; ed = e[ed].next)
if (e[ed].data != fa) {
if (mp[x].find(size[e[ed].data]) == mp[x].end()) {
for (int i = 0; i < id; i++) nxt[i] = 0;
int in = power(size[e[ed].data], P - 2), las = 0;
for (int i = id - 1; i >= 1; i--) {
las = 1ll * (num[i] - las + P) % P * in % P;
nxt[i - 1] = las;
}
tmp = 0;
for (int i = 0; i <= min(k, id - 1); i++)
update(tmp, 1ll * nxt[i] * C(k, i) % P);
mp[x][size[e[ed].data]] = tmp;
}
}
update(s[x], p[x]);
}
void solve(int x, int fa, int sum) {
ans = (ans + 1ll * sum * p[x]) % P;
int now = 0;
for (int ed = head[x]; ed != 0; ed = e[ed].next) {
if (e[ed].data == fa) continue;
update(ans, 1ll * mp[x][size[e[ed].data]] * s[e[ed].data] % P);
update(now, s[e[ed].data]);
}
for (int ed = head[x]; ed != 0; ed = e[ed].next) {
if (e[ed].data == fa) continue;
solve(e[ed].data, x,
(1ll * sum + mp[x][size[e[ed].data]] + now - s[e[ed].data] + P) % P);
}
}
int main() {
read(n), read(k);
if (k == 1) {
cout << (1ll * n * (n - 1) / 2) % P << endl;
return 0;
}
mul[0] = 1;
for (int i = 1; i <= k; i++) mul[i] = 1ll * mul[i - 1] * i % P;
inv[k] = power(mul[k], P - 2);
for (int i = k - 1; i >= 0; i--) inv[i] = 1ll * inv[i + 1] * (i + 1) % P;
for (int i = 1; i < n; i++) {
int u = 1, v = i + 1;
read(u);
read(v);
build(u, v);
build(v, u);
}
dfs(1, 0);
solve(1, 0, 0);
ans = 1ll * ans * power(2, P - 2) % P;
printf("%d\n", ans);
return 0;
}
| ### Prompt
In CPP, your task is to solve the following problem:
You are given a tree of n vertices. You are to select k (not necessarily distinct) simple paths in such a way that it is possible to split all edges of the tree into three sets: edges not contained in any path, edges that are a part of exactly one of these paths, and edges that are parts of all selected paths, and the latter set should be non-empty.
Compute the number of ways to select k paths modulo 998244353.
The paths are enumerated, in other words, two ways are considered distinct if there are such i (1 ≤ i ≤ k) and an edge that the i-th path contains the edge in one way and does not contain it in the other.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 10^{5}) — the number of vertices in the tree and the desired number of paths.
The next n - 1 lines describe edges of the tree. Each line contains two integers a and b (1 ≤ a, b ≤ n, a ≠ b) — the endpoints of an edge. It is guaranteed that the given edges form a tree.
Output
Print the number of ways to select k enumerated not necessarily distinct simple paths in such a way that for each edge either it is not contained in any path, or it is contained in exactly one path, or it is contained in all k paths, and the intersection of all paths is non-empty.
As the answer can be large, print it modulo 998244353.
Examples
Input
3 2
1 2
2 3
Output
7
Input
5 1
4 1
2 3
4 5
2 1
Output
10
Input
29 29
1 2
1 3
1 4
1 5
5 6
5 7
5 8
8 9
8 10
8 11
11 12
11 13
11 14
14 15
14 16
14 17
17 18
17 19
17 20
20 21
20 22
20 23
23 24
23 25
23 26
26 27
26 28
26 29
Output
125580756
Note
In the first example the following ways are valid:
* ((1,2), (1,2)),
* ((1,2), (1,3)),
* ((1,3), (1,2)),
* ((1,3), (1,3)),
* ((1,3), (2,3)),
* ((2,3), (1,3)),
* ((2,3), (2,3)).
In the second example k=1, so all n ⋅ (n - 1) / 2 = 5 ⋅ 4 / 2 = 10 paths are valid.
In the third example, the answer is ≥ 998244353, so it was taken modulo 998244353, don't forget it!
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
namespace Base {
const int inf = 0x3f3f3f3f, INF = 0x7fffffff;
const long long infll = 0x3f3f3f3f3f3f3f3fll, INFll = 0x7fffffffffffffffll;
template <typename T>
void read(T &x) {
x = 0;
int fh = 1;
double num = 1.0;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') fh = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
if (ch == '.') {
ch = getchar();
while (isdigit(ch)) {
num /= 10;
x = x + num * (ch - '0');
ch = getchar();
}
}
x = x * fh;
}
template <typename T>
void chmax(T &x, T y) {
x = x < y ? y : x;
}
template <typename T>
void chmin(T &x, T y) {
x = x > y ? y : x;
}
} // namespace Base
using namespace Base;
const int P = 998244353, G = 3, N = 500010;
struct Edge {
int data, next;
} e[N * 2];
int a[N], b[N], c[N], num[N], nxt[N], p[N], size[N], dad[N], head[N], place, k,
mul[N], inv[N], n, s[N], ans;
map<int, int> mp[N];
int power(int x, int y) {
int i = x;
x = 1;
while (y > 0) {
if (y % 2 == 1) x = 1ll * i * x % P;
i = 1ll * i * i % P;
y /= 2;
}
return x;
}
int C(int n, int m) {
if (m > n) return 0;
return 1ll * mul[n] * inv[n - m] % P;
}
void update(int &x, int y) {
x += y;
x = (x >= P) ? x - P : x;
}
void build(int u, int v) {
e[++place].data = v;
e[place].next = head[u];
head[u] = place;
}
void NTT(int *a, int l, int tag) {
for (int i = 0, j = 0; i < l; i++) {
if (i > j) swap(a[i], a[j]);
for (int k = (l >> 1); (j ^= k) < k; k >>= 1)
;
}
for (int i = 1; i < l; i <<= 1) {
int wn = power(G, (P - 1) / (i * 2));
if (tag == -1) wn = power(wn, P - 2);
for (int j = 0; j < l; j += i + i) {
int w = 1;
for (int k = 0; k < i; k++, w = 1ll * w * wn % P) {
int x = a[k + j], y = 1ll * w * a[k + j + i] % P;
a[k + j] = (x + y) % P, a[k + j + i] = (x - y + P) % P;
}
}
}
if (tag == -1) {
int in = power(l, P - 2);
for (int i = 0; i < l; i++) a[i] = 1ll * a[i] * in % P;
}
}
void doit(int *t1, int *t2, int *c, int l) {
for (int i = 0; i < l; i++) a[i] = t1[i], b[i] = t2[i];
for (int i = l; i < 2 * l; i++) a[i] = b[i] = 0;
for (int i = 0; i < 2 * l; i++) c[i] = 0;
NTT(a, l * 2, 1);
NTT(b, l * 2, 1);
for (int i = 0; i < 2 * l; i++) c[i] = 1ll * a[i] * b[i] % P;
NTT(c, l * 2, -1);
}
void dfs(int x, int fa) {
size[x] = 1;
dad[x] = fa;
int id = 0;
for (int ed = head[x]; ed != 0; ed = e[ed].next)
if (e[ed].data != fa) {
dfs(e[ed].data, x);
size[x] += size[e[ed].data];
update(s[x], s[e[ed].data]);
}
for (int ed = head[x]; ed != 0; ed = e[ed].next)
if (e[ed].data != fa) {
num[id] = 1, num[id + 1] = size[e[ed].data];
id += 2;
}
int tmp = 1;
while (tmp < id) tmp <<= 1;
for (int i = id; i < tmp; i += 2) num[i] = 1, num[i + 1] = 0;
id = tmp;
for (int i = 2; i < tmp; i <<= 1) {
for (int k = 0; k < tmp; k += 2 * i) {
doit(num + k, num + k + i, nxt, i);
for (int t = 0; t < 2 * i; t++) num[k + t] = nxt[t];
}
}
for (int i = 0; i <= min(k, id - 1); i++)
update(p[x], 1ll * num[i] * C(k, i) % P);
for (int i = 0; i < id + 2; i++) nxt[i] = 0;
for (int i = 0; i < id; i++) {
update(nxt[i], num[i]);
update(nxt[i + 1], 1ll * num[i] * (n - size[x]) % P);
}
id += 2;
for (int i = 0; i < id; i++) num[i] = nxt[i];
for (int ed = head[x]; ed != 0; ed = e[ed].next)
if (e[ed].data != fa) {
if (mp[x].find(size[e[ed].data]) == mp[x].end()) {
for (int i = 0; i < id; i++) nxt[i] = 0;
int in = power(size[e[ed].data], P - 2), las = 0;
for (int i = id - 1; i >= 1; i--) {
las = 1ll * (num[i] - las + P) % P * in % P;
nxt[i - 1] = las;
}
tmp = 0;
for (int i = 0; i <= min(k, id - 1); i++)
update(tmp, 1ll * nxt[i] * C(k, i) % P);
mp[x][size[e[ed].data]] = tmp;
}
}
update(s[x], p[x]);
}
void solve(int x, int fa, int sum) {
ans = (ans + 1ll * sum * p[x]) % P;
int now = 0;
for (int ed = head[x]; ed != 0; ed = e[ed].next) {
if (e[ed].data == fa) continue;
update(ans, 1ll * mp[x][size[e[ed].data]] * s[e[ed].data] % P);
update(now, s[e[ed].data]);
}
for (int ed = head[x]; ed != 0; ed = e[ed].next) {
if (e[ed].data == fa) continue;
solve(e[ed].data, x,
(1ll * sum + mp[x][size[e[ed].data]] + now - s[e[ed].data] + P) % P);
}
}
int main() {
read(n), read(k);
if (k == 1) {
cout << (1ll * n * (n - 1) / 2) % P << endl;
return 0;
}
mul[0] = 1;
for (int i = 1; i <= k; i++) mul[i] = 1ll * mul[i - 1] * i % P;
inv[k] = power(mul[k], P - 2);
for (int i = k - 1; i >= 0; i--) inv[i] = 1ll * inv[i + 1] * (i + 1) % P;
for (int i = 1; i < n; i++) {
int u = 1, v = i + 1;
read(u);
read(v);
build(u, v);
build(v, u);
}
dfs(1, 0);
solve(1, 0, 0);
ans = 1ll * ans * power(2, P - 2) % P;
printf("%d\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
namespace gbd_ns {
template <typename C>
struct is_iterable {
template <class T>
static long check(...);
template <class T>
static char check(int, typename T::const_iterator = C().end());
enum {
value = sizeof(check<C>(0)) == sizeof(char),
neg_value = sizeof(check<C>(0)) != sizeof(char)
};
};
template <class T>
ostream &_out_str(ostream &os, const T &x) {
return os << '"' << x << '"';
}
template <bool B, typename T = void>
using eit = typename enable_if<B, T>::type;
template <class T>
struct _gbd3C;
template <class T>
ostream &_gbd3(ostream &os, const T &x) {
return _gbd3C<T>::call(os, x);
}
template <class T>
struct _gbd3C {
template <class U = T>
static ostream &call(ostream &os, eit<is_iterable<U>::value, const T> &V) {
os << "{";
bool ff = 0;
for (const auto &E : V) _gbd3<decltype(E)>(ff ? os << "," : os, E), ff = 1;
return os << "}";
}
template <class U = T>
static ostream &call(ostream &os,
eit<is_iterable<U>::neg_value, const T> &x) {
return os << x;
}
};
template <>
struct _gbd3C<string> {
static ostream &call(ostream &os, const string &s) {
return os << '"' << s << '"';
}
};
template <>
struct _gbd3C<char *> {
static ostream &call(ostream &os, char *const &s) {
return os << '"' << s << '"';
}
};
template <class S, class T>
struct _gbd3C<pair<S, T> > {
static ostream &call(ostream &os, const pair<S, T> &p) {
_gbd3(os << '(', p.first);
_gbd3(os << ',', p.second);
return os << ')';
}
};
template <class T, typename... Args>
ostream &_gbd2(ostream &os, vector<string>::iterator nm, const T &x,
Args &&...args);
ostream &_gbd2(ostream &os, vector<string>::iterator) { return os; }
template <typename... Args>
ostream &_gbd2(ostream &os, vector<string>::iterator nm, const char *x,
Args &&...args) {
return _gbd2(os << " " << x, nm + 1, args...);
}
template <class T, typename... Args>
ostream &_gbd2(ostream &os, vector<string>::iterator nm, const T &x,
Args &&...args) {
return _gbd2(_gbd3<T>(os << " " << *nm << "=", x), nm + 1, args...);
}
vector<string> split(string s) {
vector<string> Z;
string z = "";
s += ',';
int dep = 0;
for (char c : s) {
if (c == ',' && !dep)
Z.push_back(z), z = "";
else
z += c;
if (c == '(') ++dep;
if (c == ')') --dep;
}
return Z;
}
template <typename... Args>
ostream &_gbd1(int ln, const string &nm, Args &&...args) {
auto nms = split(nm);
_gbd2(cerr << "L" << ln << ":", nms.begin(), args...);
return cerr << endl;
}
} // namespace gbd_ns
inline void nop() {}
const int e3 = 1000;
const int e6 = e3 * e3;
const int e9 = e6 * e3;
const long double tau = 2 * acosl(-1);
typedef long double ld;
const ld eps = (ld)1e-8;
const int inf = e9 + 99;
const long long linf = 1LL * e9 * e9 + 99;
const int P = 998244353;
void add(int &x, int y) {
x += y;
if (x >= P) x -= P;
}
void sub(int &x, int y) {
x -= y;
if (x < 0) x += P;
}
unsigned long long powq_ull(unsigned long long x, unsigned long long e,
unsigned long long p) {
if (!e) return 1;
if (e & 1) return x * powq_ull(x, e - 1, p) % p;
x = powq_ull(x, e >> 1, p);
return x * x % p;
}
const unsigned long long PP = (119ULL << 23) + 1;
const int AA = 119;
const unsigned long long ROOT = 3;
const unsigned long long INV_ROOT = 332748118;
const unsigned long long TWO_INV = 499122177;
vector<unsigned long long> fft(const vector<unsigned long long> &input,
const bool inv = 0) {
const unsigned long long P = PP;
const int A = AA;
const unsigned long long RT = ROOT;
const unsigned long long IRT = INV_ROOT;
const unsigned long long I2 = TWO_INV;
const int N = (int)input.size();
vector<unsigned long long> arr(input);
for (unsigned long long &x : arr) x %= P;
for (int half = inv ? 1 : N / 2; 1 <= half && half < N;
inv ? half <<= 1 : half >>= 1) {
unsigned long long wm = inv ? IRT : RT;
wm = powq_ull(wm, A, P);
for (int ord = int((P - 1) / A); ord > 2 * half; ord >>= 1)
wm = (wm * wm) % P;
for (int st = 0; st < N; st += 2 * half) {
unsigned long long w = 1;
for (int j = st; j < st + half; ++j) {
if (inv) arr[j + half] = arr[j + half] * w % P;
unsigned long long a = arr[j], b = arr[j + half];
arr[j] = a + b;
if (arr[j] >= P) arr[j] -= P;
arr[j + half] = a + P - b;
if (arr[j + half] >= P) arr[j + half] -= P;
if (!inv) arr[j + half] = arr[j + half] * w % P;
w = (w * wm) % P;
}
}
}
if (inv) {
unsigned long long size_inv = 1;
for (int sz = 2; sz <= N; sz <<= 1) size_inv = (size_inv * I2) % P;
for (unsigned long long &x : arr) x = (x * size_inv) % P;
}
return arr;
}
vector<int> mulfst(const vector<int> &A, const vector<int> &B) {
int m = (int((A).size())), n = (int((B).size()));
int w = m + n - 1;
for (; w & (w - 1);) ++w;
vector<unsigned long long> AA(w, 0), BB(w, 0);
for (int i = 0; i < m; i++) AA[i] = A[i];
for (int i = 0; i < n; i++) BB[i] = B[i];
AA = fft(AA);
BB = fft(BB);
for (int i = 0; i < w; i++) AA[i] *= BB[i];
AA = fft(AA, 1);
for (; (int((AA).size())) > m + n - 1;) assert(!AA.back()), AA.pop_back();
vector<int> Z;
for (unsigned long long x : AA) Z.push_back((int)x);
return Z;
}
vector<int> mulslo(const vector<int> &A, const vector<int> &B) {
int n = (int((A).size())), m = (int((B).size()));
int w = m + n - 1;
vector<int> Z(w, 0);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) add(Z[i + j], int(1LL * A[i] * B[j] % P));
return Z;
}
const int N = 100 << 10;
int n, k;
set<int> adj[N];
int p[N], sz[N];
int nn;
int dfs_sz(int u, int _p = -1) {
p[u] = _p;
sz[u] = 1;
for (int v : adj[u])
if (v != _p) sz[u] += dfs_sz(v, u);
if (!~_p) nn = sz[u];
return sz[u];
}
int siz(int u, int _p) {
if (p[u] == _p) return sz[u];
assert(p[_p] == u);
return nn - sz[_p];
}
vector<int> ml(const vector<int> &szs, int l, int r) {
if (l == r) return {1, szs[l]};
int m = (l + r) >> 1;
return mulfst(ml(szs, l, m), ml(szs, m + 1, r));
}
int ff(int x) {
if (!x) return 1;
static bool seen[N];
static int dp[N];
bool &sn = seen[x];
int &ans = dp[x];
if (sn) return ans;
sn = 1;
return ans = int(1LL * ff(x - 1) * x % P);
}
int iff(int x) {
if (x == N - 3) return (int)powq_ull(ff(x), P - 2, P);
static bool seen[N];
static int dp[N];
bool &sn = seen[x];
int &ans = dp[x];
if (sn) return ans;
sn = 1;
return ans = int(1LL * iff(x + 1) * (x + 1) % P);
}
int slow_eval(const vector<int> &p, int x) {
int Z = 0;
int qq = 0;
for (int i = 0; i < (int((p).size())); i++) {
qq = int((p[i] + P - 1LL * qq * x % P) % P);
int fxqq = int(1LL * qq * ff(k) % P * iff(k - i) % P);
add(Z, fxqq);
}
return Z;
}
map<int, int> WW[N];
int RR[N];
void solve(int u) {
vector<int> sizs;
for (int v : adj[u]) sizs.push_back(siz(v, u));
vector<int> poly = ml(sizs, 0, (int((sizs).size())) - 1);
for (; (int((poly).size())) > k + 1;) poly.pop_back();
RR[u] = 0;
for (int x : poly) add(RR[u], x);
sort(sizs.begin(), sizs.end());
sizs.resize(unique(sizs.begin(), sizs.end()) - sizs.begin());
map<int, int> edp;
for (int x : sizs) edp[x] = slow_eval(poly, x);
for (int v : adj[u]) {
int zz = edp[siz(v, u)];
WW[u][v] = zz;
}
}
int dfs_ins(int u, int p) {
assert(WW[u].count(p));
int z = WW[u][p];
for (int v : adj[u])
if (v != p) add(z, dfs_ins(v, u));
return z;
}
int FF;
int dfs_zz(int u, int p) {
assert(WW[u].count(p));
int Z = int(1LL * WW[u][p] * FF % P);
for (int v : adj[u])
if (v != p) add(Z, dfs_zz(v, u));
return Z;
}
int decomp(int u) {
dfs_sz(u);
loop:;
for (int v : adj[u])
if (siz(v, u) > siz(u, v)) {
u = v;
goto loop;
}
int Z = 0;
FF = 0;
for (int v : adj[u]) {
add(Z, dfs_zz(v, u));
int r = dfs_ins(v, u);
add(FF, r);
add(Z, int(1LL * r * WW[u][v] % P));
}
for (int v : adj[u]) adj[v].erase(u), add(Z, decomp(v));
adj[u].clear();
return Z;
}
int32_t main() {
gbd_ns::_gbd1(344, "ff(3), ff(7), iff(3), iff(2), iff(1)", ff(3), ff(7),
iff(3), iff(2), iff(1));
assert(P == PP);
scanf("%d%d", &n, &k);
for (int i = 1; i <= n - 1; i++) {
int u, v;
scanf("%d%d", &u, &v);
adj[u].insert(v);
adj[v].insert(u);
}
if (n == 1) return printf("0\n"), 0;
if (k == 1) return printf("%d\n", int((1LL * n * (n - 1) / 2) % P)), 0;
dfs_sz(1);
assert(n == nn);
for (int u = 1; u <= n; u++) solve(u);
printf("%d\n", decomp(1));
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
You are given a tree of n vertices. You are to select k (not necessarily distinct) simple paths in such a way that it is possible to split all edges of the tree into three sets: edges not contained in any path, edges that are a part of exactly one of these paths, and edges that are parts of all selected paths, and the latter set should be non-empty.
Compute the number of ways to select k paths modulo 998244353.
The paths are enumerated, in other words, two ways are considered distinct if there are such i (1 ≤ i ≤ k) and an edge that the i-th path contains the edge in one way and does not contain it in the other.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 10^{5}) — the number of vertices in the tree and the desired number of paths.
The next n - 1 lines describe edges of the tree. Each line contains two integers a and b (1 ≤ a, b ≤ n, a ≠ b) — the endpoints of an edge. It is guaranteed that the given edges form a tree.
Output
Print the number of ways to select k enumerated not necessarily distinct simple paths in such a way that for each edge either it is not contained in any path, or it is contained in exactly one path, or it is contained in all k paths, and the intersection of all paths is non-empty.
As the answer can be large, print it modulo 998244353.
Examples
Input
3 2
1 2
2 3
Output
7
Input
5 1
4 1
2 3
4 5
2 1
Output
10
Input
29 29
1 2
1 3
1 4
1 5
5 6
5 7
5 8
8 9
8 10
8 11
11 12
11 13
11 14
14 15
14 16
14 17
17 18
17 19
17 20
20 21
20 22
20 23
23 24
23 25
23 26
26 27
26 28
26 29
Output
125580756
Note
In the first example the following ways are valid:
* ((1,2), (1,2)),
* ((1,2), (1,3)),
* ((1,3), (1,2)),
* ((1,3), (1,3)),
* ((1,3), (2,3)),
* ((2,3), (1,3)),
* ((2,3), (2,3)).
In the second example k=1, so all n ⋅ (n - 1) / 2 = 5 ⋅ 4 / 2 = 10 paths are valid.
In the third example, the answer is ≥ 998244353, so it was taken modulo 998244353, don't forget it!
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
namespace gbd_ns {
template <typename C>
struct is_iterable {
template <class T>
static long check(...);
template <class T>
static char check(int, typename T::const_iterator = C().end());
enum {
value = sizeof(check<C>(0)) == sizeof(char),
neg_value = sizeof(check<C>(0)) != sizeof(char)
};
};
template <class T>
ostream &_out_str(ostream &os, const T &x) {
return os << '"' << x << '"';
}
template <bool B, typename T = void>
using eit = typename enable_if<B, T>::type;
template <class T>
struct _gbd3C;
template <class T>
ostream &_gbd3(ostream &os, const T &x) {
return _gbd3C<T>::call(os, x);
}
template <class T>
struct _gbd3C {
template <class U = T>
static ostream &call(ostream &os, eit<is_iterable<U>::value, const T> &V) {
os << "{";
bool ff = 0;
for (const auto &E : V) _gbd3<decltype(E)>(ff ? os << "," : os, E), ff = 1;
return os << "}";
}
template <class U = T>
static ostream &call(ostream &os,
eit<is_iterable<U>::neg_value, const T> &x) {
return os << x;
}
};
template <>
struct _gbd3C<string> {
static ostream &call(ostream &os, const string &s) {
return os << '"' << s << '"';
}
};
template <>
struct _gbd3C<char *> {
static ostream &call(ostream &os, char *const &s) {
return os << '"' << s << '"';
}
};
template <class S, class T>
struct _gbd3C<pair<S, T> > {
static ostream &call(ostream &os, const pair<S, T> &p) {
_gbd3(os << '(', p.first);
_gbd3(os << ',', p.second);
return os << ')';
}
};
template <class T, typename... Args>
ostream &_gbd2(ostream &os, vector<string>::iterator nm, const T &x,
Args &&...args);
ostream &_gbd2(ostream &os, vector<string>::iterator) { return os; }
template <typename... Args>
ostream &_gbd2(ostream &os, vector<string>::iterator nm, const char *x,
Args &&...args) {
return _gbd2(os << " " << x, nm + 1, args...);
}
template <class T, typename... Args>
ostream &_gbd2(ostream &os, vector<string>::iterator nm, const T &x,
Args &&...args) {
return _gbd2(_gbd3<T>(os << " " << *nm << "=", x), nm + 1, args...);
}
vector<string> split(string s) {
vector<string> Z;
string z = "";
s += ',';
int dep = 0;
for (char c : s) {
if (c == ',' && !dep)
Z.push_back(z), z = "";
else
z += c;
if (c == '(') ++dep;
if (c == ')') --dep;
}
return Z;
}
template <typename... Args>
ostream &_gbd1(int ln, const string &nm, Args &&...args) {
auto nms = split(nm);
_gbd2(cerr << "L" << ln << ":", nms.begin(), args...);
return cerr << endl;
}
} // namespace gbd_ns
inline void nop() {}
const int e3 = 1000;
const int e6 = e3 * e3;
const int e9 = e6 * e3;
const long double tau = 2 * acosl(-1);
typedef long double ld;
const ld eps = (ld)1e-8;
const int inf = e9 + 99;
const long long linf = 1LL * e9 * e9 + 99;
const int P = 998244353;
void add(int &x, int y) {
x += y;
if (x >= P) x -= P;
}
void sub(int &x, int y) {
x -= y;
if (x < 0) x += P;
}
unsigned long long powq_ull(unsigned long long x, unsigned long long e,
unsigned long long p) {
if (!e) return 1;
if (e & 1) return x * powq_ull(x, e - 1, p) % p;
x = powq_ull(x, e >> 1, p);
return x * x % p;
}
const unsigned long long PP = (119ULL << 23) + 1;
const int AA = 119;
const unsigned long long ROOT = 3;
const unsigned long long INV_ROOT = 332748118;
const unsigned long long TWO_INV = 499122177;
vector<unsigned long long> fft(const vector<unsigned long long> &input,
const bool inv = 0) {
const unsigned long long P = PP;
const int A = AA;
const unsigned long long RT = ROOT;
const unsigned long long IRT = INV_ROOT;
const unsigned long long I2 = TWO_INV;
const int N = (int)input.size();
vector<unsigned long long> arr(input);
for (unsigned long long &x : arr) x %= P;
for (int half = inv ? 1 : N / 2; 1 <= half && half < N;
inv ? half <<= 1 : half >>= 1) {
unsigned long long wm = inv ? IRT : RT;
wm = powq_ull(wm, A, P);
for (int ord = int((P - 1) / A); ord > 2 * half; ord >>= 1)
wm = (wm * wm) % P;
for (int st = 0; st < N; st += 2 * half) {
unsigned long long w = 1;
for (int j = st; j < st + half; ++j) {
if (inv) arr[j + half] = arr[j + half] * w % P;
unsigned long long a = arr[j], b = arr[j + half];
arr[j] = a + b;
if (arr[j] >= P) arr[j] -= P;
arr[j + half] = a + P - b;
if (arr[j + half] >= P) arr[j + half] -= P;
if (!inv) arr[j + half] = arr[j + half] * w % P;
w = (w * wm) % P;
}
}
}
if (inv) {
unsigned long long size_inv = 1;
for (int sz = 2; sz <= N; sz <<= 1) size_inv = (size_inv * I2) % P;
for (unsigned long long &x : arr) x = (x * size_inv) % P;
}
return arr;
}
vector<int> mulfst(const vector<int> &A, const vector<int> &B) {
int m = (int((A).size())), n = (int((B).size()));
int w = m + n - 1;
for (; w & (w - 1);) ++w;
vector<unsigned long long> AA(w, 0), BB(w, 0);
for (int i = 0; i < m; i++) AA[i] = A[i];
for (int i = 0; i < n; i++) BB[i] = B[i];
AA = fft(AA);
BB = fft(BB);
for (int i = 0; i < w; i++) AA[i] *= BB[i];
AA = fft(AA, 1);
for (; (int((AA).size())) > m + n - 1;) assert(!AA.back()), AA.pop_back();
vector<int> Z;
for (unsigned long long x : AA) Z.push_back((int)x);
return Z;
}
vector<int> mulslo(const vector<int> &A, const vector<int> &B) {
int n = (int((A).size())), m = (int((B).size()));
int w = m + n - 1;
vector<int> Z(w, 0);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) add(Z[i + j], int(1LL * A[i] * B[j] % P));
return Z;
}
const int N = 100 << 10;
int n, k;
set<int> adj[N];
int p[N], sz[N];
int nn;
int dfs_sz(int u, int _p = -1) {
p[u] = _p;
sz[u] = 1;
for (int v : adj[u])
if (v != _p) sz[u] += dfs_sz(v, u);
if (!~_p) nn = sz[u];
return sz[u];
}
int siz(int u, int _p) {
if (p[u] == _p) return sz[u];
assert(p[_p] == u);
return nn - sz[_p];
}
vector<int> ml(const vector<int> &szs, int l, int r) {
if (l == r) return {1, szs[l]};
int m = (l + r) >> 1;
return mulfst(ml(szs, l, m), ml(szs, m + 1, r));
}
int ff(int x) {
if (!x) return 1;
static bool seen[N];
static int dp[N];
bool &sn = seen[x];
int &ans = dp[x];
if (sn) return ans;
sn = 1;
return ans = int(1LL * ff(x - 1) * x % P);
}
int iff(int x) {
if (x == N - 3) return (int)powq_ull(ff(x), P - 2, P);
static bool seen[N];
static int dp[N];
bool &sn = seen[x];
int &ans = dp[x];
if (sn) return ans;
sn = 1;
return ans = int(1LL * iff(x + 1) * (x + 1) % P);
}
int slow_eval(const vector<int> &p, int x) {
int Z = 0;
int qq = 0;
for (int i = 0; i < (int((p).size())); i++) {
qq = int((p[i] + P - 1LL * qq * x % P) % P);
int fxqq = int(1LL * qq * ff(k) % P * iff(k - i) % P);
add(Z, fxqq);
}
return Z;
}
map<int, int> WW[N];
int RR[N];
void solve(int u) {
vector<int> sizs;
for (int v : adj[u]) sizs.push_back(siz(v, u));
vector<int> poly = ml(sizs, 0, (int((sizs).size())) - 1);
for (; (int((poly).size())) > k + 1;) poly.pop_back();
RR[u] = 0;
for (int x : poly) add(RR[u], x);
sort(sizs.begin(), sizs.end());
sizs.resize(unique(sizs.begin(), sizs.end()) - sizs.begin());
map<int, int> edp;
for (int x : sizs) edp[x] = slow_eval(poly, x);
for (int v : adj[u]) {
int zz = edp[siz(v, u)];
WW[u][v] = zz;
}
}
int dfs_ins(int u, int p) {
assert(WW[u].count(p));
int z = WW[u][p];
for (int v : adj[u])
if (v != p) add(z, dfs_ins(v, u));
return z;
}
int FF;
int dfs_zz(int u, int p) {
assert(WW[u].count(p));
int Z = int(1LL * WW[u][p] * FF % P);
for (int v : adj[u])
if (v != p) add(Z, dfs_zz(v, u));
return Z;
}
int decomp(int u) {
dfs_sz(u);
loop:;
for (int v : adj[u])
if (siz(v, u) > siz(u, v)) {
u = v;
goto loop;
}
int Z = 0;
FF = 0;
for (int v : adj[u]) {
add(Z, dfs_zz(v, u));
int r = dfs_ins(v, u);
add(FF, r);
add(Z, int(1LL * r * WW[u][v] % P));
}
for (int v : adj[u]) adj[v].erase(u), add(Z, decomp(v));
adj[u].clear();
return Z;
}
int32_t main() {
gbd_ns::_gbd1(344, "ff(3), ff(7), iff(3), iff(2), iff(1)", ff(3), ff(7),
iff(3), iff(2), iff(1));
assert(P == PP);
scanf("%d%d", &n, &k);
for (int i = 1; i <= n - 1; i++) {
int u, v;
scanf("%d%d", &u, &v);
adj[u].insert(v);
adj[v].insert(u);
}
if (n == 1) return printf("0\n"), 0;
if (k == 1) return printf("%d\n", int((1LL * n * (n - 1) / 2) % P)), 0;
dfs_sz(1);
assert(n == nn);
for (int u = 1; u <= n; u++) solve(u);
printf("%d\n", decomp(1));
}
``` |
#include <bits/stdc++.h>
using namespace std;
int get() {
char ch;
while (ch = getchar(), (ch < '0' || ch > '9') && ch != '-')
;
if (ch == '-') {
int s = 0;
while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0';
return -s;
}
int s = ch - '0';
while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0';
return s;
}
const int MAXN = (1 << 18) + 5;
const int maxn = (1 << 18);
const int mo = 998244353;
const int g0 = 3;
long long quickmi(long long x, long long tim) {
long long ret = 1;
for (; tim; tim /= 2, x = x * x % mo)
if (tim & 1) ret = ret * x % mo;
return ret;
}
long long add(long long x, long long y) {
return x + y >= mo ? x + y - mo : x + y;
}
long long dec(long long x, long long y) { return x < y ? x - y + mo : x - y; }
long long mi[MAXN], A[MAXN], B[MAXN], ny;
int N;
long long js[MAXN], inv[MAXN];
void DFT(long long *a) {
for (int i = 0, j = 0; i < N; i++) {
if (i < j) swap(a[i], a[j]);
int x = N >> 1;
for (; (j ^ x) < j; x >>= 1) j ^= x;
j ^= x;
}
for (int i = 1, d = 1; i < N; i <<= 1, d++)
for (int j = 0; j < N; j += (i << 1))
for (int k = 0; k < i; k++) {
long long l = a[j + k], r = a[i + j + k] * mi[(maxn >> d) * k] % mo;
a[i + j + k] = l < r ? l - r + mo : l - r;
a[j + k] = l + r >= mo ? l + r - mo : l + r;
}
}
void IDFT(long long *a) {
DFT(a);
reverse(a + 1, a + N);
for (int i = 0; i <= N - 1; i++) a[i] = a[i] * ny % mo;
}
void prepare() {
mi[0] = 1;
mi[1] = quickmi(g0, (mo - 1) / maxn);
for (int i = 2; i <= maxn; i++) mi[i] = mi[i - 1] * mi[1] % mo;
js[0] = js[1] = 1;
for (int i = 2; i <= maxn; i++) js[i] = js[i - 1] * i % mo;
inv[0] = inv[1] = 1;
for (int i = 2; i <= maxn; i++)
inv[i] = 1ll * (mo - mo / i) * inv[mo % i] % mo;
for (int i = 2; i <= maxn; i++) inv[i] = inv[i] * inv[i - 1] % mo;
}
int n, k;
struct edge {
int x, nxt;
} e[MAXN * 2];
int h[MAXN], tot;
void inse(int x, int y) {
e[++tot].x = y;
e[tot].nxt = h[x];
h[x] = tot;
}
int siz[MAXN], fa[MAXN];
int lef[MAXN], rig[MAXN];
void dfs1(int x) {
siz[x] = 1;
for (int p = h[x]; p; p = e[p].nxt)
if (!siz[e[p].x]) {
fa[e[p].x] = x;
rig[e[p].x] = lef[x];
lef[x] = e[p].x;
dfs1(e[p].x);
siz[x] += siz[e[p].x];
}
}
int key[MAXN], u;
long long poly[MAXN];
int st[MAXN], len[MAXN];
void getpoly() {
int l = 0;
for (int i = 1; i <= u; i++) {
poly[++l] = key[i];
st[i] = l;
len[i] = 1;
}
N = 2;
for (; u > 1; N <<= 1) {
ny = quickmi(N, mo - 2);
int u_ = 0;
for (int i = 1; i <= u;) {
int j = i + 1;
for (; j <= u && len[i] + len[j] < N; j++) {
for (int x = 0; x <= N - 1; x++) A[x] = B[x] = 0;
A[0] = B[0] = 1;
for (int x = 1; x <= len[i]; x++) A[x] = poly[st[i] + x - 1];
for (int x = 1; x <= len[j]; x++) B[x] = poly[st[j] + x - 1];
DFT(A), DFT(B);
for (int x = 0; x <= N - 1; x++) A[x] = A[x] * B[x] % mo;
IDFT(A);
for (int x = 1; x <= len[i] + len[j]; x++) poly[st[i] + x - 1] = A[x];
len[i] += len[j];
}
st[++u_] = st[i];
len[u_] = len[i];
i = j;
}
u = u_;
}
}
long long tmp[MAXN];
long long val[MAXN];
long long f[MAXN], g[MAXN], vf[MAXN], vg[MAXN];
long long ans;
void dfs2(int x) {
vf[x] = f[x];
for (int y = lef[x]; y; y = rig[y]) {
dfs2(y);
vf[x] = add(vf[x], vf[y]);
}
}
void dfs3(int x) {
ans = add(ans, f[x] * vg[x] % mo);
long long pre = vg[x];
for (int y = lef[x]; y; y = rig[y]) {
vg[y] = add(pre, g[y]);
dfs3(y);
pre = add(pre, vf[y]);
}
}
int main() {
n = get();
k = get();
if (k == 1) return printf("%I64d\n", 1ll * n * (n - 1) / 2 % mo), 0;
for (int i = 2; i <= n; i++) {
int x = get(), y = get();
inse(x, y), inse(y, x);
}
prepare();
dfs1(1);
for (int ti = 1; ti <= n; ti++) {
int st = ti;
u = 0;
for (int y = lef[st]; y; y = rig[y]) key[++u] = siz[y];
if (fa[st]) key[++u] = n - siz[st];
int pu = u;
getpoly();
int Len = len[1];
poly[0] = 1;
u = pu;
sort(key + 1, key + 1 + u);
int u_ = 1;
for (int i = 2; i <= u; i++)
if (key[i] > key[i - 1]) key[++u_] = key[i];
u = u_;
for (int i = 1; i <= u; i++) {
int v = key[i];
tmp[0] = poly[0];
for (int j = 1; j <= Len; j++)
tmp[j] = (poly[j] + mo - tmp[j - 1] * v % mo) % mo;
val[v] = 0;
for (int j = 0; j <= min(k, Len); j++)
val[v] = add(val[v], tmp[j] * inv[k - j] % mo);
val[v] = val[v] * js[k] % mo;
}
for (int y = lef[st]; y; y = rig[y]) g[y] = val[siz[y]];
if (fa[st]) f[st] = val[n - siz[st]];
}
ans = 0;
dfs2(1);
dfs3(1);
printf("%I64d\n", ans);
return 0;
}
| ### Prompt
Generate a cpp solution to the following problem:
You are given a tree of n vertices. You are to select k (not necessarily distinct) simple paths in such a way that it is possible to split all edges of the tree into three sets: edges not contained in any path, edges that are a part of exactly one of these paths, and edges that are parts of all selected paths, and the latter set should be non-empty.
Compute the number of ways to select k paths modulo 998244353.
The paths are enumerated, in other words, two ways are considered distinct if there are such i (1 ≤ i ≤ k) and an edge that the i-th path contains the edge in one way and does not contain it in the other.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 10^{5}) — the number of vertices in the tree and the desired number of paths.
The next n - 1 lines describe edges of the tree. Each line contains two integers a and b (1 ≤ a, b ≤ n, a ≠ b) — the endpoints of an edge. It is guaranteed that the given edges form a tree.
Output
Print the number of ways to select k enumerated not necessarily distinct simple paths in such a way that for each edge either it is not contained in any path, or it is contained in exactly one path, or it is contained in all k paths, and the intersection of all paths is non-empty.
As the answer can be large, print it modulo 998244353.
Examples
Input
3 2
1 2
2 3
Output
7
Input
5 1
4 1
2 3
4 5
2 1
Output
10
Input
29 29
1 2
1 3
1 4
1 5
5 6
5 7
5 8
8 9
8 10
8 11
11 12
11 13
11 14
14 15
14 16
14 17
17 18
17 19
17 20
20 21
20 22
20 23
23 24
23 25
23 26
26 27
26 28
26 29
Output
125580756
Note
In the first example the following ways are valid:
* ((1,2), (1,2)),
* ((1,2), (1,3)),
* ((1,3), (1,2)),
* ((1,3), (1,3)),
* ((1,3), (2,3)),
* ((2,3), (1,3)),
* ((2,3), (2,3)).
In the second example k=1, so all n ⋅ (n - 1) / 2 = 5 ⋅ 4 / 2 = 10 paths are valid.
In the third example, the answer is ≥ 998244353, so it was taken modulo 998244353, don't forget it!
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int get() {
char ch;
while (ch = getchar(), (ch < '0' || ch > '9') && ch != '-')
;
if (ch == '-') {
int s = 0;
while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0';
return -s;
}
int s = ch - '0';
while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0';
return s;
}
const int MAXN = (1 << 18) + 5;
const int maxn = (1 << 18);
const int mo = 998244353;
const int g0 = 3;
long long quickmi(long long x, long long tim) {
long long ret = 1;
for (; tim; tim /= 2, x = x * x % mo)
if (tim & 1) ret = ret * x % mo;
return ret;
}
long long add(long long x, long long y) {
return x + y >= mo ? x + y - mo : x + y;
}
long long dec(long long x, long long y) { return x < y ? x - y + mo : x - y; }
long long mi[MAXN], A[MAXN], B[MAXN], ny;
int N;
long long js[MAXN], inv[MAXN];
void DFT(long long *a) {
for (int i = 0, j = 0; i < N; i++) {
if (i < j) swap(a[i], a[j]);
int x = N >> 1;
for (; (j ^ x) < j; x >>= 1) j ^= x;
j ^= x;
}
for (int i = 1, d = 1; i < N; i <<= 1, d++)
for (int j = 0; j < N; j += (i << 1))
for (int k = 0; k < i; k++) {
long long l = a[j + k], r = a[i + j + k] * mi[(maxn >> d) * k] % mo;
a[i + j + k] = l < r ? l - r + mo : l - r;
a[j + k] = l + r >= mo ? l + r - mo : l + r;
}
}
void IDFT(long long *a) {
DFT(a);
reverse(a + 1, a + N);
for (int i = 0; i <= N - 1; i++) a[i] = a[i] * ny % mo;
}
void prepare() {
mi[0] = 1;
mi[1] = quickmi(g0, (mo - 1) / maxn);
for (int i = 2; i <= maxn; i++) mi[i] = mi[i - 1] * mi[1] % mo;
js[0] = js[1] = 1;
for (int i = 2; i <= maxn; i++) js[i] = js[i - 1] * i % mo;
inv[0] = inv[1] = 1;
for (int i = 2; i <= maxn; i++)
inv[i] = 1ll * (mo - mo / i) * inv[mo % i] % mo;
for (int i = 2; i <= maxn; i++) inv[i] = inv[i] * inv[i - 1] % mo;
}
int n, k;
struct edge {
int x, nxt;
} e[MAXN * 2];
int h[MAXN], tot;
void inse(int x, int y) {
e[++tot].x = y;
e[tot].nxt = h[x];
h[x] = tot;
}
int siz[MAXN], fa[MAXN];
int lef[MAXN], rig[MAXN];
void dfs1(int x) {
siz[x] = 1;
for (int p = h[x]; p; p = e[p].nxt)
if (!siz[e[p].x]) {
fa[e[p].x] = x;
rig[e[p].x] = lef[x];
lef[x] = e[p].x;
dfs1(e[p].x);
siz[x] += siz[e[p].x];
}
}
int key[MAXN], u;
long long poly[MAXN];
int st[MAXN], len[MAXN];
void getpoly() {
int l = 0;
for (int i = 1; i <= u; i++) {
poly[++l] = key[i];
st[i] = l;
len[i] = 1;
}
N = 2;
for (; u > 1; N <<= 1) {
ny = quickmi(N, mo - 2);
int u_ = 0;
for (int i = 1; i <= u;) {
int j = i + 1;
for (; j <= u && len[i] + len[j] < N; j++) {
for (int x = 0; x <= N - 1; x++) A[x] = B[x] = 0;
A[0] = B[0] = 1;
for (int x = 1; x <= len[i]; x++) A[x] = poly[st[i] + x - 1];
for (int x = 1; x <= len[j]; x++) B[x] = poly[st[j] + x - 1];
DFT(A), DFT(B);
for (int x = 0; x <= N - 1; x++) A[x] = A[x] * B[x] % mo;
IDFT(A);
for (int x = 1; x <= len[i] + len[j]; x++) poly[st[i] + x - 1] = A[x];
len[i] += len[j];
}
st[++u_] = st[i];
len[u_] = len[i];
i = j;
}
u = u_;
}
}
long long tmp[MAXN];
long long val[MAXN];
long long f[MAXN], g[MAXN], vf[MAXN], vg[MAXN];
long long ans;
void dfs2(int x) {
vf[x] = f[x];
for (int y = lef[x]; y; y = rig[y]) {
dfs2(y);
vf[x] = add(vf[x], vf[y]);
}
}
void dfs3(int x) {
ans = add(ans, f[x] * vg[x] % mo);
long long pre = vg[x];
for (int y = lef[x]; y; y = rig[y]) {
vg[y] = add(pre, g[y]);
dfs3(y);
pre = add(pre, vf[y]);
}
}
int main() {
n = get();
k = get();
if (k == 1) return printf("%I64d\n", 1ll * n * (n - 1) / 2 % mo), 0;
for (int i = 2; i <= n; i++) {
int x = get(), y = get();
inse(x, y), inse(y, x);
}
prepare();
dfs1(1);
for (int ti = 1; ti <= n; ti++) {
int st = ti;
u = 0;
for (int y = lef[st]; y; y = rig[y]) key[++u] = siz[y];
if (fa[st]) key[++u] = n - siz[st];
int pu = u;
getpoly();
int Len = len[1];
poly[0] = 1;
u = pu;
sort(key + 1, key + 1 + u);
int u_ = 1;
for (int i = 2; i <= u; i++)
if (key[i] > key[i - 1]) key[++u_] = key[i];
u = u_;
for (int i = 1; i <= u; i++) {
int v = key[i];
tmp[0] = poly[0];
for (int j = 1; j <= Len; j++)
tmp[j] = (poly[j] + mo - tmp[j - 1] * v % mo) % mo;
val[v] = 0;
for (int j = 0; j <= min(k, Len); j++)
val[v] = add(val[v], tmp[j] * inv[k - j] % mo);
val[v] = val[v] * js[k] % mo;
}
for (int y = lef[st]; y; y = rig[y]) g[y] = val[siz[y]];
if (fa[st]) f[st] = val[n - siz[st]];
}
ans = 0;
dfs2(1);
dfs3(1);
printf("%I64d\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
namespace NTT {
int mbase, base, root;
int w[1 << 19];
int rev[1 << 19];
int modPow(int b, int e) {
int r = 1;
while (e > 0) {
if (e & 1) r = ((long long int)r * b) % 998244353;
e >>= 1;
b = ((long long int)b * b) % 998244353;
}
return r;
}
int setBase(int nbase) {
int i, j;
mbase = 1;
while (!(998244353 & (1 << mbase))) mbase++;
root = 2;
while ((modPow(root, 1 << mbase) != 1) ||
(modPow(root, 1 << (mbase - 1)) == 1))
root++;
base = nbase;
for (i = 0; i < (1 << base); i++)
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (base - 1));
w[0] = 0, w[1] = 1;
for (i = 1; i < nbase; i++) {
int z = modPow(root, 1 << (mbase - i - 1));
for (j = (1 << (i - 1)); j < (1 << i); j++)
w[j << 1] = w[j], w[(j << 1) | 1] = ((long long int)w[j] * z) % 998244353;
}
return 0;
}
int FFT(vector<int> &a, int inv) {
int i, j, k;
int l = 0;
while ((1 << l) < a.size()) l++;
int s = base - l;
for (i = 0; i < a.size(); i++) {
if (i < (rev[i] >> s)) swap(a[i], a[rev[i] >> s]);
}
for (k = 1; k < a.size(); k <<= 1) {
for (i = 0; i < a.size(); i += (k << 1)) {
for (j = 0; j < k; j++) {
int z = ((long long int)a[i + j + k] * w[j + k]) % 998244353;
a[i + j + k] = a[i + j] - z, a[i + j] += z;
if (a[i + j + k] < 0) a[i + j + k] += 998244353;
if (a[i + j] >= 998244353) a[i + j] -= 998244353;
}
}
}
return 0;
}
vector<int> multiply(vector<int> A, vector<int> B) {
int i, n = 1;
while (n < A.size() + B.size() - 1) n <<= 1;
vector<int> a(n, 0), b(n, 0);
for (i = 0; i < A.size(); i++) a[i] = A[i];
for (i = 0; i < B.size(); i++) b[i] = B[i];
FFT(a, 0), FFT(b, 0);
int x = modPow(n, 998244353 - 2);
for (i = 0; i < n; i++)
a[i] = ((((long long int)a[i] * b[i]) % 998244353) * x) % 998244353;
reverse(a.begin() + 1, a.end()), FFT(a, 1), a.resize(A.size() + B.size() - 1);
return a;
}
} // namespace NTT
vector<int> adjList[100000];
int parent[100000], size[100000];
int doDFS(int u, int p) {
int i;
parent[u] = p, size[u] = 1;
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i];
if (v != p) size[u] += doDFS(v, u);
}
return size[u];
}
int down[100000], up[100000];
vector<int> mult(vector<vector<int> > &v, int l, int r) {
if (l == r)
return v[l];
else if (l > r)
return vector<int>(1, 1);
int i;
int mid = (l + r) / 2;
vector<int> ll = mult(v, l, mid), rr = mult(v, mid + 1, r), ans;
vector<int> temp = NTT::multiply(ll, rr);
for (i = 0; i < temp.size(); i++) ans.push_back(temp[i] % 998244353);
return ans;
}
vector<int> p[100000];
int ans = 0;
int doDFS2(int u, int p) {
int i;
int s = 0;
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i];
if (v != p) {
doDFS2(v, u);
down[u] += down[v], down[u] %= 998244353;
ans += ((long long int)down[v] * up[v]) % 998244353, ans %= 998244353;
ans += ((long long int)s * down[v]) % 998244353, ans %= 998244353;
s += down[v], s %= 998244353;
}
}
return 0;
}
int main() {
int i;
int n, k, a, b;
scanf("%d %d", &n, &k);
for (i = 0; i < n - 1; i++) {
scanf("%d %d", &a, &b);
a--, b--;
adjList[a].push_back(b);
adjList[b].push_back(a);
}
if (k == 1) {
printf("%d\n", ((long long int)n * (n - 1) / 2) % 998244353);
return 0;
}
int j, l;
doDFS(0, -1);
NTT::setBase(17);
for (i = 0; i < n; i++) {
vector<vector<int> > vv;
for (j = 0; j < adjList[i].size(); j++) {
int v = adjList[i][j];
if (v != parent[i]) {
vector<int> vvv(2);
vvv[0] = size[v], vvv[1] = 1;
vv.push_back(vvv);
} else {
vector<int> vvv(2);
vvv[0] = n - size[i], vvv[1] = 1;
vv.push_back(vvv);
}
}
p[i] = mult(vv, 0, (int)vv.size() - 1);
}
for (i = 0; i < n; i++) {
map<int, int> M;
vector<int> poly = p[i];
if (i > 0) {
for (j = poly.size() - 1; j >= 1; j--)
poly[j - 1] +=
998244353 - (((long long int)(n - size[i]) * poly[j]) % 998244353),
poly[j - 1] %= 998244353;
for (j = 0; j < poly.size() - 1; j++) poly[j] = poly[j + 1];
poly.pop_back();
}
long long int c = 1;
for (j = 0; j < poly.size(); j++) {
down[i] += (poly[poly.size() - j - 1] * c) % 998244353,
down[i] %= 998244353;
c *= k - j, c %= 998244353;
}
M[n - size[i]] = down[i];
for (j = 0; j < adjList[i].size(); j++) {
int v = adjList[i][j];
if (v != parent[i]) {
if (M.count(size[v]))
up[v] = M[size[v]];
else {
vector<int> poly = p[i];
for (l = poly.size() - 1; l >= 1; l--)
poly[l - 1] +=
998244353 - (((long long int)size[v] * poly[l]) % 998244353),
poly[l - 1] %= 998244353;
for (l = 0; l < poly.size() - 1; l++) poly[l] = poly[l + 1];
poly.pop_back();
long long int c = 1;
for (l = 0; l < poly.size(); l++) {
up[v] += (poly[poly.size() - l - 1] * c) % 998244353,
up[v] %= 998244353;
c *= k - l, c %= 998244353;
}
M[size[v]] = up[v];
}
}
}
}
doDFS2(0, -1);
printf("%d\n", ans);
return 0;
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
You are given a tree of n vertices. You are to select k (not necessarily distinct) simple paths in such a way that it is possible to split all edges of the tree into three sets: edges not contained in any path, edges that are a part of exactly one of these paths, and edges that are parts of all selected paths, and the latter set should be non-empty.
Compute the number of ways to select k paths modulo 998244353.
The paths are enumerated, in other words, two ways are considered distinct if there are such i (1 ≤ i ≤ k) and an edge that the i-th path contains the edge in one way and does not contain it in the other.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 10^{5}) — the number of vertices in the tree and the desired number of paths.
The next n - 1 lines describe edges of the tree. Each line contains two integers a and b (1 ≤ a, b ≤ n, a ≠ b) — the endpoints of an edge. It is guaranteed that the given edges form a tree.
Output
Print the number of ways to select k enumerated not necessarily distinct simple paths in such a way that for each edge either it is not contained in any path, or it is contained in exactly one path, or it is contained in all k paths, and the intersection of all paths is non-empty.
As the answer can be large, print it modulo 998244353.
Examples
Input
3 2
1 2
2 3
Output
7
Input
5 1
4 1
2 3
4 5
2 1
Output
10
Input
29 29
1 2
1 3
1 4
1 5
5 6
5 7
5 8
8 9
8 10
8 11
11 12
11 13
11 14
14 15
14 16
14 17
17 18
17 19
17 20
20 21
20 22
20 23
23 24
23 25
23 26
26 27
26 28
26 29
Output
125580756
Note
In the first example the following ways are valid:
* ((1,2), (1,2)),
* ((1,2), (1,3)),
* ((1,3), (1,2)),
* ((1,3), (1,3)),
* ((1,3), (2,3)),
* ((2,3), (1,3)),
* ((2,3), (2,3)).
In the second example k=1, so all n ⋅ (n - 1) / 2 = 5 ⋅ 4 / 2 = 10 paths are valid.
In the third example, the answer is ≥ 998244353, so it was taken modulo 998244353, don't forget it!
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
namespace NTT {
int mbase, base, root;
int w[1 << 19];
int rev[1 << 19];
int modPow(int b, int e) {
int r = 1;
while (e > 0) {
if (e & 1) r = ((long long int)r * b) % 998244353;
e >>= 1;
b = ((long long int)b * b) % 998244353;
}
return r;
}
int setBase(int nbase) {
int i, j;
mbase = 1;
while (!(998244353 & (1 << mbase))) mbase++;
root = 2;
while ((modPow(root, 1 << mbase) != 1) ||
(modPow(root, 1 << (mbase - 1)) == 1))
root++;
base = nbase;
for (i = 0; i < (1 << base); i++)
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (base - 1));
w[0] = 0, w[1] = 1;
for (i = 1; i < nbase; i++) {
int z = modPow(root, 1 << (mbase - i - 1));
for (j = (1 << (i - 1)); j < (1 << i); j++)
w[j << 1] = w[j], w[(j << 1) | 1] = ((long long int)w[j] * z) % 998244353;
}
return 0;
}
int FFT(vector<int> &a, int inv) {
int i, j, k;
int l = 0;
while ((1 << l) < a.size()) l++;
int s = base - l;
for (i = 0; i < a.size(); i++) {
if (i < (rev[i] >> s)) swap(a[i], a[rev[i] >> s]);
}
for (k = 1; k < a.size(); k <<= 1) {
for (i = 0; i < a.size(); i += (k << 1)) {
for (j = 0; j < k; j++) {
int z = ((long long int)a[i + j + k] * w[j + k]) % 998244353;
a[i + j + k] = a[i + j] - z, a[i + j] += z;
if (a[i + j + k] < 0) a[i + j + k] += 998244353;
if (a[i + j] >= 998244353) a[i + j] -= 998244353;
}
}
}
return 0;
}
vector<int> multiply(vector<int> A, vector<int> B) {
int i, n = 1;
while (n < A.size() + B.size() - 1) n <<= 1;
vector<int> a(n, 0), b(n, 0);
for (i = 0; i < A.size(); i++) a[i] = A[i];
for (i = 0; i < B.size(); i++) b[i] = B[i];
FFT(a, 0), FFT(b, 0);
int x = modPow(n, 998244353 - 2);
for (i = 0; i < n; i++)
a[i] = ((((long long int)a[i] * b[i]) % 998244353) * x) % 998244353;
reverse(a.begin() + 1, a.end()), FFT(a, 1), a.resize(A.size() + B.size() - 1);
return a;
}
} // namespace NTT
vector<int> adjList[100000];
int parent[100000], size[100000];
int doDFS(int u, int p) {
int i;
parent[u] = p, size[u] = 1;
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i];
if (v != p) size[u] += doDFS(v, u);
}
return size[u];
}
int down[100000], up[100000];
vector<int> mult(vector<vector<int> > &v, int l, int r) {
if (l == r)
return v[l];
else if (l > r)
return vector<int>(1, 1);
int i;
int mid = (l + r) / 2;
vector<int> ll = mult(v, l, mid), rr = mult(v, mid + 1, r), ans;
vector<int> temp = NTT::multiply(ll, rr);
for (i = 0; i < temp.size(); i++) ans.push_back(temp[i] % 998244353);
return ans;
}
vector<int> p[100000];
int ans = 0;
int doDFS2(int u, int p) {
int i;
int s = 0;
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i];
if (v != p) {
doDFS2(v, u);
down[u] += down[v], down[u] %= 998244353;
ans += ((long long int)down[v] * up[v]) % 998244353, ans %= 998244353;
ans += ((long long int)s * down[v]) % 998244353, ans %= 998244353;
s += down[v], s %= 998244353;
}
}
return 0;
}
int main() {
int i;
int n, k, a, b;
scanf("%d %d", &n, &k);
for (i = 0; i < n - 1; i++) {
scanf("%d %d", &a, &b);
a--, b--;
adjList[a].push_back(b);
adjList[b].push_back(a);
}
if (k == 1) {
printf("%d\n", ((long long int)n * (n - 1) / 2) % 998244353);
return 0;
}
int j, l;
doDFS(0, -1);
NTT::setBase(17);
for (i = 0; i < n; i++) {
vector<vector<int> > vv;
for (j = 0; j < adjList[i].size(); j++) {
int v = adjList[i][j];
if (v != parent[i]) {
vector<int> vvv(2);
vvv[0] = size[v], vvv[1] = 1;
vv.push_back(vvv);
} else {
vector<int> vvv(2);
vvv[0] = n - size[i], vvv[1] = 1;
vv.push_back(vvv);
}
}
p[i] = mult(vv, 0, (int)vv.size() - 1);
}
for (i = 0; i < n; i++) {
map<int, int> M;
vector<int> poly = p[i];
if (i > 0) {
for (j = poly.size() - 1; j >= 1; j--)
poly[j - 1] +=
998244353 - (((long long int)(n - size[i]) * poly[j]) % 998244353),
poly[j - 1] %= 998244353;
for (j = 0; j < poly.size() - 1; j++) poly[j] = poly[j + 1];
poly.pop_back();
}
long long int c = 1;
for (j = 0; j < poly.size(); j++) {
down[i] += (poly[poly.size() - j - 1] * c) % 998244353,
down[i] %= 998244353;
c *= k - j, c %= 998244353;
}
M[n - size[i]] = down[i];
for (j = 0; j < adjList[i].size(); j++) {
int v = adjList[i][j];
if (v != parent[i]) {
if (M.count(size[v]))
up[v] = M[size[v]];
else {
vector<int> poly = p[i];
for (l = poly.size() - 1; l >= 1; l--)
poly[l - 1] +=
998244353 - (((long long int)size[v] * poly[l]) % 998244353),
poly[l - 1] %= 998244353;
for (l = 0; l < poly.size() - 1; l++) poly[l] = poly[l + 1];
poly.pop_back();
long long int c = 1;
for (l = 0; l < poly.size(); l++) {
up[v] += (poly[poly.size() - l - 1] * c) % 998244353,
up[v] %= 998244353;
c *= k - l, c %= 998244353;
}
M[size[v]] = up[v];
}
}
}
}
doDFS2(0, -1);
printf("%d\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 262333, mod = 998244353, G = 3, inv2 = (mod + 1) >> 1;
int read() {
int ret = 0;
char c = getchar();
while (!isdigit(c)) c = getchar();
while (isdigit(c)) ret = ret * 10 + (c ^ 48), c = getchar();
return ret;
}
namespace Math {
int fac[N], ifac[N], inv[N];
int add(int x) { return x >= mod ? x - mod : x; }
int sub(int x) { return x < 0 ? x + mod : x; }
void Add(int &x, int y) { x = add(x + y); }
void Sub(int &x, int y) { x = sub(x - y); }
int mul(int x, int y) { return 1ll * x * y % mod; }
int qpow(int x, int y) {
int res = 1;
for (; y; y >>= 1, x = mul(x, x))
if (y & 1) res = mul(res, x);
return res;
}
int getinv(int x) { return qpow(x, mod - 2); }
void initmath() {
fac[0] = 1;
for (int i = 1; i < N; ++i) fac[i] = mul(fac[i - 1], i);
ifac[N - 1] = getinv(fac[N - 1]);
for (int i = N - 2; ~i; --i) ifac[i] = mul(ifac[i + 1], i + 1);
inv[0] = inv[1] = 1;
for (int i = 2; i < N; ++i) inv[i] = mul(mod - mod / i, inv[mod % i]);
}
int P(int x, int y) { return 1ll * fac[x] * ifac[x - y] % mod; }
} // namespace Math
using namespace Math;
namespace Poly {
int m, L, rev[N];
void ntt(int *a, int n, int op) {
for (int i = 0; i < n; ++i)
if (i < rev[i]) swap(a[i], a[rev[i]]);
for (int i = 1; i < n; i <<= 1) {
int wn = qpow(G, (mod - 1) / (i << 1));
if (!~op) wn = getinv(wn);
for (int j = 0; j < n; j += i << 1) {
int w = 1;
for (int k = 0; k < i; ++k, w = mul(w, wn)) {
int x = a[j + k], y = mul(w, a[i + j + k]);
a[j + k] = add(x + y);
a[i + j + k] = sub(x - y);
}
}
}
if (!~op)
for (int i = 0, iv = getinv(n); i < n; ++i) a[i] = mul(iv, a[i]);
}
void reget(int n) {
for (m = 1, L = 0; m < n; m <<= 1, ++L)
;
for (int i = 0; i < m; ++i)
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (L - 1));
}
void polymul(int *a, int *b, int *c) {
ntt(a, m, 1);
ntt(b, m, 1);
for (int i = 0; i < m; ++i) c[i] = mul(a[i], b[i]);
ntt(c, m, -1);
}
void polymult(int *a, int *b, int *c, int dega, int degb) {
static int A[N], B[N];
reget(dega + degb - 1);
copy(a, a + dega, A);
copy(b, b + degb, B);
polymul(A, B, c);
fill(c + dega + degb - 1, c + m, 0);
fill(A, A + m, 0);
fill(B, B + m, 0);
}
void polydec(int *a, int deg, int v) {
static int A[N];
int coe = getinv(v), iv;
for (int i = 0; i < deg; ++i) A[i] = a[i], a[i] = 0;
for (int i = deg - 1; i; --i) {
if (A[i]) {
a[i - 1] = iv = mul(A[i], coe);
Sub(A[i], mul(iv, coe));
Sub(A[i - 1], iv);
}
}
fill(A, A + deg, 0);
}
void polyadd(int *a, int deg, int v) {
for (int i = deg; i; --i) Add(a[i], mul(a[i - 1], v));
}
} // namespace Poly
using namespace Poly;
namespace DreamLolita {
int n, K, tot, ans;
int head[N], siz[N], now[N], val[N], tmp[N];
int f[N], g[N], h[N], F[20][N];
struct Tway {
int v, nex;
} e[N];
void add(int u, int v) {
e[++tot] = (Tway){v, head[u]};
head[u] = tot;
e[++tot] = (Tway){u, head[v]};
head[v] = tot;
}
void solve(int l, int r, int d) {
if (l == r) {
F[d][1] = val[l];
F[d][0] = 1;
return;
}
int mid = (l + r) >> 1;
solve(l, mid, d);
solve(mid + 1, r, d + 1);
polymult(F[d], F[d + 1], F[d], mid - l + 2, r - mid + 1);
fill(F[d + 1], F[d + 1] + Poly::m, 0);
}
int calc(int *a, int len) {
int res = 0, lim = min(len, K);
for (int i = 0; i <= lim; ++i) Add(res, mul(a[i], P(K, i)));
return res;
}
bool cmp(int x, int y) { return siz[x] < siz[y]; }
void dfs1(int x, int fa) {
siz[x] = 1;
int son = 0;
for (int i = head[x]; i; i = e[i].nex) {
int v = e[i].v;
if (v == fa) continue;
dfs1(v, x);
Add(g[x], g[v]);
siz[x] += siz[v];
}
for (int i = head[x]; i; i = e[i].nex)
if (e[i].v != fa) val[++son] = siz[e[i].v];
;
if (!son) {
f[x] = g[x] = 1;
return;
}
solve(1, son, 0);
f[x] = calc(F[0], son);
Add(g[x], f[x]);
son = 0;
for (int i = head[x]; i; i = e[i].nex)
if (e[i].v != fa) val[++son] = e[i].v;
sort(val + 1, val + son + 1, cmp);
for (int i = 0; i <= son; ++i) tmp[i] = F[0][i];
for (int i = 1; i <= son; ++i) {
if (siz[val[i]] == siz[val[i - 1]])
h[val[i]] = h[val[i - 1]];
else {
for (int j = 0; j <= son; ++j) now[j] = tmp[j];
polydec(now, son + 1, siz[val[i]]);
polyadd(now, son, n - siz[x]);
h[val[i]] = calc(now, son);
}
}
fill(F[0], F[0] + Poly::m, 0);
for (int i = 0; i <= son; ++i) now[i] = tmp[i] = 0;
}
void dfs2(int x, int fa) {
for (int i = head[x]; i; i = e[i].nex) {
int v = e[i].v;
if (v == fa) continue;
Add(ans, mul(sub(h[v] - f[x]), g[v]));
dfs2(v, x);
}
}
void solution() {
initmath();
n = read();
K = read();
if (K == 1) {
printf("%lld\n", 1ll * n * (n - 1) / 2 % mod);
return;
}
for (int i = 1; i < n; ++i) add(read(), read());
dfs1(1, 0);
dfs2(1, 0);
int sum = 0;
for (int i = 1; i <= n; ++i) Add(sum, f[i]);
sum = mul(sum, sum);
for (int i = 1; i <= n; ++i) Sub(sum, mul(f[i], f[i]));
sum = mul(sum, inv2);
Add(ans, sum);
printf("%d\n", ans);
}
} // namespace DreamLolita
int main() {
DreamLolita::solution();
return 0;
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
You are given a tree of n vertices. You are to select k (not necessarily distinct) simple paths in such a way that it is possible to split all edges of the tree into three sets: edges not contained in any path, edges that are a part of exactly one of these paths, and edges that are parts of all selected paths, and the latter set should be non-empty.
Compute the number of ways to select k paths modulo 998244353.
The paths are enumerated, in other words, two ways are considered distinct if there are such i (1 ≤ i ≤ k) and an edge that the i-th path contains the edge in one way and does not contain it in the other.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 10^{5}) — the number of vertices in the tree and the desired number of paths.
The next n - 1 lines describe edges of the tree. Each line contains two integers a and b (1 ≤ a, b ≤ n, a ≠ b) — the endpoints of an edge. It is guaranteed that the given edges form a tree.
Output
Print the number of ways to select k enumerated not necessarily distinct simple paths in such a way that for each edge either it is not contained in any path, or it is contained in exactly one path, or it is contained in all k paths, and the intersection of all paths is non-empty.
As the answer can be large, print it modulo 998244353.
Examples
Input
3 2
1 2
2 3
Output
7
Input
5 1
4 1
2 3
4 5
2 1
Output
10
Input
29 29
1 2
1 3
1 4
1 5
5 6
5 7
5 8
8 9
8 10
8 11
11 12
11 13
11 14
14 15
14 16
14 17
17 18
17 19
17 20
20 21
20 22
20 23
23 24
23 25
23 26
26 27
26 28
26 29
Output
125580756
Note
In the first example the following ways are valid:
* ((1,2), (1,2)),
* ((1,2), (1,3)),
* ((1,3), (1,2)),
* ((1,3), (1,3)),
* ((1,3), (2,3)),
* ((2,3), (1,3)),
* ((2,3), (2,3)).
In the second example k=1, so all n ⋅ (n - 1) / 2 = 5 ⋅ 4 / 2 = 10 paths are valid.
In the third example, the answer is ≥ 998244353, so it was taken modulo 998244353, don't forget it!
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 262333, mod = 998244353, G = 3, inv2 = (mod + 1) >> 1;
int read() {
int ret = 0;
char c = getchar();
while (!isdigit(c)) c = getchar();
while (isdigit(c)) ret = ret * 10 + (c ^ 48), c = getchar();
return ret;
}
namespace Math {
int fac[N], ifac[N], inv[N];
int add(int x) { return x >= mod ? x - mod : x; }
int sub(int x) { return x < 0 ? x + mod : x; }
void Add(int &x, int y) { x = add(x + y); }
void Sub(int &x, int y) { x = sub(x - y); }
int mul(int x, int y) { return 1ll * x * y % mod; }
int qpow(int x, int y) {
int res = 1;
for (; y; y >>= 1, x = mul(x, x))
if (y & 1) res = mul(res, x);
return res;
}
int getinv(int x) { return qpow(x, mod - 2); }
void initmath() {
fac[0] = 1;
for (int i = 1; i < N; ++i) fac[i] = mul(fac[i - 1], i);
ifac[N - 1] = getinv(fac[N - 1]);
for (int i = N - 2; ~i; --i) ifac[i] = mul(ifac[i + 1], i + 1);
inv[0] = inv[1] = 1;
for (int i = 2; i < N; ++i) inv[i] = mul(mod - mod / i, inv[mod % i]);
}
int P(int x, int y) { return 1ll * fac[x] * ifac[x - y] % mod; }
} // namespace Math
using namespace Math;
namespace Poly {
int m, L, rev[N];
void ntt(int *a, int n, int op) {
for (int i = 0; i < n; ++i)
if (i < rev[i]) swap(a[i], a[rev[i]]);
for (int i = 1; i < n; i <<= 1) {
int wn = qpow(G, (mod - 1) / (i << 1));
if (!~op) wn = getinv(wn);
for (int j = 0; j < n; j += i << 1) {
int w = 1;
for (int k = 0; k < i; ++k, w = mul(w, wn)) {
int x = a[j + k], y = mul(w, a[i + j + k]);
a[j + k] = add(x + y);
a[i + j + k] = sub(x - y);
}
}
}
if (!~op)
for (int i = 0, iv = getinv(n); i < n; ++i) a[i] = mul(iv, a[i]);
}
void reget(int n) {
for (m = 1, L = 0; m < n; m <<= 1, ++L)
;
for (int i = 0; i < m; ++i)
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (L - 1));
}
void polymul(int *a, int *b, int *c) {
ntt(a, m, 1);
ntt(b, m, 1);
for (int i = 0; i < m; ++i) c[i] = mul(a[i], b[i]);
ntt(c, m, -1);
}
void polymult(int *a, int *b, int *c, int dega, int degb) {
static int A[N], B[N];
reget(dega + degb - 1);
copy(a, a + dega, A);
copy(b, b + degb, B);
polymul(A, B, c);
fill(c + dega + degb - 1, c + m, 0);
fill(A, A + m, 0);
fill(B, B + m, 0);
}
void polydec(int *a, int deg, int v) {
static int A[N];
int coe = getinv(v), iv;
for (int i = 0; i < deg; ++i) A[i] = a[i], a[i] = 0;
for (int i = deg - 1; i; --i) {
if (A[i]) {
a[i - 1] = iv = mul(A[i], coe);
Sub(A[i], mul(iv, coe));
Sub(A[i - 1], iv);
}
}
fill(A, A + deg, 0);
}
void polyadd(int *a, int deg, int v) {
for (int i = deg; i; --i) Add(a[i], mul(a[i - 1], v));
}
} // namespace Poly
using namespace Poly;
namespace DreamLolita {
int n, K, tot, ans;
int head[N], siz[N], now[N], val[N], tmp[N];
int f[N], g[N], h[N], F[20][N];
struct Tway {
int v, nex;
} e[N];
void add(int u, int v) {
e[++tot] = (Tway){v, head[u]};
head[u] = tot;
e[++tot] = (Tway){u, head[v]};
head[v] = tot;
}
void solve(int l, int r, int d) {
if (l == r) {
F[d][1] = val[l];
F[d][0] = 1;
return;
}
int mid = (l + r) >> 1;
solve(l, mid, d);
solve(mid + 1, r, d + 1);
polymult(F[d], F[d + 1], F[d], mid - l + 2, r - mid + 1);
fill(F[d + 1], F[d + 1] + Poly::m, 0);
}
int calc(int *a, int len) {
int res = 0, lim = min(len, K);
for (int i = 0; i <= lim; ++i) Add(res, mul(a[i], P(K, i)));
return res;
}
bool cmp(int x, int y) { return siz[x] < siz[y]; }
void dfs1(int x, int fa) {
siz[x] = 1;
int son = 0;
for (int i = head[x]; i; i = e[i].nex) {
int v = e[i].v;
if (v == fa) continue;
dfs1(v, x);
Add(g[x], g[v]);
siz[x] += siz[v];
}
for (int i = head[x]; i; i = e[i].nex)
if (e[i].v != fa) val[++son] = siz[e[i].v];
;
if (!son) {
f[x] = g[x] = 1;
return;
}
solve(1, son, 0);
f[x] = calc(F[0], son);
Add(g[x], f[x]);
son = 0;
for (int i = head[x]; i; i = e[i].nex)
if (e[i].v != fa) val[++son] = e[i].v;
sort(val + 1, val + son + 1, cmp);
for (int i = 0; i <= son; ++i) tmp[i] = F[0][i];
for (int i = 1; i <= son; ++i) {
if (siz[val[i]] == siz[val[i - 1]])
h[val[i]] = h[val[i - 1]];
else {
for (int j = 0; j <= son; ++j) now[j] = tmp[j];
polydec(now, son + 1, siz[val[i]]);
polyadd(now, son, n - siz[x]);
h[val[i]] = calc(now, son);
}
}
fill(F[0], F[0] + Poly::m, 0);
for (int i = 0; i <= son; ++i) now[i] = tmp[i] = 0;
}
void dfs2(int x, int fa) {
for (int i = head[x]; i; i = e[i].nex) {
int v = e[i].v;
if (v == fa) continue;
Add(ans, mul(sub(h[v] - f[x]), g[v]));
dfs2(v, x);
}
}
void solution() {
initmath();
n = read();
K = read();
if (K == 1) {
printf("%lld\n", 1ll * n * (n - 1) / 2 % mod);
return;
}
for (int i = 1; i < n; ++i) add(read(), read());
dfs1(1, 0);
dfs2(1, 0);
int sum = 0;
for (int i = 1; i <= n; ++i) Add(sum, f[i]);
sum = mul(sum, sum);
for (int i = 1; i <= n; ++i) Sub(sum, mul(f[i], f[i]));
sum = mul(sum, inv2);
Add(ans, sum);
printf("%d\n", ans);
}
} // namespace DreamLolita
int main() {
DreamLolita::solution();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const int MOD = 998244353;
const int g = 3;
int qpow(int x, int y = MOD - 2) {
int res = 1;
for (; y; y >>= 1, x = (long long)x * x % MOD)
if (y & 1) res = (long long)res * x % MOD;
return res;
}
int n, K, jc[N], njc[N];
void Prework() {
jc[0] = 1;
for (int i = 1; i <= K; ++i) jc[i] = (long long)jc[i - 1] * i % MOD;
njc[K] = qpow(jc[K]);
for (int i = K - 1; ~i; --i) njc[i] = (long long)njc[i + 1] * (i + 1) % MOD;
}
int Ar(int n, int m) { return (long long)jc[n] * njc[n - m] % MOD; }
int sz[N], sm[N];
vector<int> G[N];
int U(int x, int y) { return ((x += y) >= MOD) ? (x - MOD) : x; }
void SU(int& x, int y) { ((x += y) >= MOD) ? (x -= MOD) : 0; }
int ans;
vector<int> t;
int w[N] = {1};
void FFT(vector<int>& A, bool fl) {
int L = A.size();
for (int i = 1, j = L >> 1, k; i < L; ++i, j ^= k) {
if (i < j) swap(A[i], A[j]);
k = L >> 1;
while (j & k) {
j ^= k;
k >>= 1;
}
}
for (int i = 1; i < L; i <<= 1) {
int t = qpow(g, (MOD - 1) / (i << 1));
if (fl) t = qpow(t);
for (int j = 1; j < i; ++j) w[j] = (long long)w[j - 1] * t % MOD;
vector<int>::iterator p = A.begin(), q = p + i;
for (int j = 0; j < L; j += (i << 1), p += i, q += i) {
for (int k = 0; k < i; ++k, ++p, ++q) {
t = (long long)(*q) * w[k] % MOD;
*q = U(*p, MOD - t);
SU(*p, t);
}
}
}
if (fl) {
int t = qpow(L);
for (int i = 0; i < L; ++i) A[i] = (long long)A[i] * t % MOD;
}
}
void Print(const vector<int> p) {
for (int v : p) cout << v << " ";
cout << endl;
}
void Fuck() { cout << "!!!!!!!!!!!!!!!!!!" << endl; }
void deFuck() { cout << "------------------" << endl; }
vector<int> operator*(vector<int> A, vector<int> B) {
int len = A.size() + B.size() - 1, l;
for (l = 1; l < len; l <<= 1)
;
A.resize(l);
FFT(A, false);
B.resize(l);
FFT(B, false);
for (int i = 0; i < l; ++i) A[i] = (long long)A[i] * B[i] % MOD;
FFT(A, true);
A.resize(len);
return A;
}
vector<int> DivCalc(int l, int r) {
vector<int> res;
if (l < r) {
int mid = (l + r) >> 1;
vector<int> A = DivCalc(l, mid), B = DivCalc(mid + 1, r);
res = A * B;
} else if (l == r) {
res.emplace_back(1);
res.emplace_back(t[l]);
} else if (l > r)
res.emplace_back(1);
return res;
}
int Dark(vector<int> p, int x, int y) {
for (int i = 1; i <= (int)p.size() - 1; ++i)
SU(p[i], MOD - (long long)y * p[i - 1] % MOD);
for (int i = p.size() - 1; i > 0; --i)
SU(p[i], (long long)x * p[i - 1] % MOD);
int res = 0;
for (int i = 1; i < (int)p.size(); ++i)
SU(res, (long long)p[i] * Ar(K, i) % MOD);
return res;
}
int rec[N], vis[N], cc;
void Dfs(int u, int fa = 0) {
sz[u] = sm[u] = 1;
for (int v : G[u])
if (v != fa) {
Dfs(v, u);
SU(ans, (long long)sm[u] * sm[v] % MOD);
SU(sm[u], sm[v]);
sz[u] += sz[v];
}
t.clear();
for (int v : G[u])
if (v != fa) t.emplace_back(sz[v]);
static vector<int> p;
p = DivCalc(0, t.size() - 1);
if ((int)p.size() - 1 > K) p.resize(K + 1);
for (int i = 1; i < (int)p.size(); ++i)
SU(sm[u], (long long)Ar(K, i) * p[i] % MOD);
++cc;
for (int v : G[u])
if (v != fa) {
if (vis[sz[v]] != cc) {
rec[sz[v]] = Dark(p, n - sz[u], sz[v]);
vis[sz[v]] = cc;
}
SU(ans, (long long)rec[sz[v]] * sm[v] % MOD);
}
}
int main() {
scanf("%d%d", &n, &K);
if (K == 1) {
printf("%lld\n", (long long)n * (n - 1) / 2 % MOD);
return 0;
}
Prework();
for (int i = 1, u, v; i < n; ++i) {
scanf("%d%d", &u, &v);
G[u].emplace_back(v);
G[v].emplace_back(u);
}
Dfs(1);
printf("%d\n", ans);
return 0;
}
| ### Prompt
Develop a solution in cpp to the problem described below:
You are given a tree of n vertices. You are to select k (not necessarily distinct) simple paths in such a way that it is possible to split all edges of the tree into three sets: edges not contained in any path, edges that are a part of exactly one of these paths, and edges that are parts of all selected paths, and the latter set should be non-empty.
Compute the number of ways to select k paths modulo 998244353.
The paths are enumerated, in other words, two ways are considered distinct if there are such i (1 ≤ i ≤ k) and an edge that the i-th path contains the edge in one way and does not contain it in the other.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 10^{5}) — the number of vertices in the tree and the desired number of paths.
The next n - 1 lines describe edges of the tree. Each line contains two integers a and b (1 ≤ a, b ≤ n, a ≠ b) — the endpoints of an edge. It is guaranteed that the given edges form a tree.
Output
Print the number of ways to select k enumerated not necessarily distinct simple paths in such a way that for each edge either it is not contained in any path, or it is contained in exactly one path, or it is contained in all k paths, and the intersection of all paths is non-empty.
As the answer can be large, print it modulo 998244353.
Examples
Input
3 2
1 2
2 3
Output
7
Input
5 1
4 1
2 3
4 5
2 1
Output
10
Input
29 29
1 2
1 3
1 4
1 5
5 6
5 7
5 8
8 9
8 10
8 11
11 12
11 13
11 14
14 15
14 16
14 17
17 18
17 19
17 20
20 21
20 22
20 23
23 24
23 25
23 26
26 27
26 28
26 29
Output
125580756
Note
In the first example the following ways are valid:
* ((1,2), (1,2)),
* ((1,2), (1,3)),
* ((1,3), (1,2)),
* ((1,3), (1,3)),
* ((1,3), (2,3)),
* ((2,3), (1,3)),
* ((2,3), (2,3)).
In the second example k=1, so all n ⋅ (n - 1) / 2 = 5 ⋅ 4 / 2 = 10 paths are valid.
In the third example, the answer is ≥ 998244353, so it was taken modulo 998244353, don't forget it!
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const int MOD = 998244353;
const int g = 3;
int qpow(int x, int y = MOD - 2) {
int res = 1;
for (; y; y >>= 1, x = (long long)x * x % MOD)
if (y & 1) res = (long long)res * x % MOD;
return res;
}
int n, K, jc[N], njc[N];
void Prework() {
jc[0] = 1;
for (int i = 1; i <= K; ++i) jc[i] = (long long)jc[i - 1] * i % MOD;
njc[K] = qpow(jc[K]);
for (int i = K - 1; ~i; --i) njc[i] = (long long)njc[i + 1] * (i + 1) % MOD;
}
int Ar(int n, int m) { return (long long)jc[n] * njc[n - m] % MOD; }
int sz[N], sm[N];
vector<int> G[N];
int U(int x, int y) { return ((x += y) >= MOD) ? (x - MOD) : x; }
void SU(int& x, int y) { ((x += y) >= MOD) ? (x -= MOD) : 0; }
int ans;
vector<int> t;
int w[N] = {1};
void FFT(vector<int>& A, bool fl) {
int L = A.size();
for (int i = 1, j = L >> 1, k; i < L; ++i, j ^= k) {
if (i < j) swap(A[i], A[j]);
k = L >> 1;
while (j & k) {
j ^= k;
k >>= 1;
}
}
for (int i = 1; i < L; i <<= 1) {
int t = qpow(g, (MOD - 1) / (i << 1));
if (fl) t = qpow(t);
for (int j = 1; j < i; ++j) w[j] = (long long)w[j - 1] * t % MOD;
vector<int>::iterator p = A.begin(), q = p + i;
for (int j = 0; j < L; j += (i << 1), p += i, q += i) {
for (int k = 0; k < i; ++k, ++p, ++q) {
t = (long long)(*q) * w[k] % MOD;
*q = U(*p, MOD - t);
SU(*p, t);
}
}
}
if (fl) {
int t = qpow(L);
for (int i = 0; i < L; ++i) A[i] = (long long)A[i] * t % MOD;
}
}
void Print(const vector<int> p) {
for (int v : p) cout << v << " ";
cout << endl;
}
void Fuck() { cout << "!!!!!!!!!!!!!!!!!!" << endl; }
void deFuck() { cout << "------------------" << endl; }
vector<int> operator*(vector<int> A, vector<int> B) {
int len = A.size() + B.size() - 1, l;
for (l = 1; l < len; l <<= 1)
;
A.resize(l);
FFT(A, false);
B.resize(l);
FFT(B, false);
for (int i = 0; i < l; ++i) A[i] = (long long)A[i] * B[i] % MOD;
FFT(A, true);
A.resize(len);
return A;
}
vector<int> DivCalc(int l, int r) {
vector<int> res;
if (l < r) {
int mid = (l + r) >> 1;
vector<int> A = DivCalc(l, mid), B = DivCalc(mid + 1, r);
res = A * B;
} else if (l == r) {
res.emplace_back(1);
res.emplace_back(t[l]);
} else if (l > r)
res.emplace_back(1);
return res;
}
int Dark(vector<int> p, int x, int y) {
for (int i = 1; i <= (int)p.size() - 1; ++i)
SU(p[i], MOD - (long long)y * p[i - 1] % MOD);
for (int i = p.size() - 1; i > 0; --i)
SU(p[i], (long long)x * p[i - 1] % MOD);
int res = 0;
for (int i = 1; i < (int)p.size(); ++i)
SU(res, (long long)p[i] * Ar(K, i) % MOD);
return res;
}
int rec[N], vis[N], cc;
void Dfs(int u, int fa = 0) {
sz[u] = sm[u] = 1;
for (int v : G[u])
if (v != fa) {
Dfs(v, u);
SU(ans, (long long)sm[u] * sm[v] % MOD);
SU(sm[u], sm[v]);
sz[u] += sz[v];
}
t.clear();
for (int v : G[u])
if (v != fa) t.emplace_back(sz[v]);
static vector<int> p;
p = DivCalc(0, t.size() - 1);
if ((int)p.size() - 1 > K) p.resize(K + 1);
for (int i = 1; i < (int)p.size(); ++i)
SU(sm[u], (long long)Ar(K, i) * p[i] % MOD);
++cc;
for (int v : G[u])
if (v != fa) {
if (vis[sz[v]] != cc) {
rec[sz[v]] = Dark(p, n - sz[u], sz[v]);
vis[sz[v]] = cc;
}
SU(ans, (long long)rec[sz[v]] * sm[v] % MOD);
}
}
int main() {
scanf("%d%d", &n, &K);
if (K == 1) {
printf("%lld\n", (long long)n * (n - 1) / 2 % MOD);
return 0;
}
Prework();
for (int i = 1, u, v; i < n; ++i) {
scanf("%d%d", &u, &v);
G[u].emplace_back(v);
G[v].emplace_back(u);
}
Dfs(1);
printf("%d\n", ans);
return 0;
}
``` |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.