Codeforces Round 922 (Div. 2 ABCDEF题)

A. Brick Wall

Problem Statement

A brick is a strip of size 1 × k 1 \times k 1×k, placed horizontally or vertically, where k k k can be an arbitrary number that is at least 2 2 2 ( k ≥ 2 k \ge 2 k2).

A brick wall of size n × m n \times m n×m is such a way to place several bricks inside a rectangle n × m n \times m n×m, that all bricks lie either horizontally or vertically in the cells, do not cross the border of the rectangle, and that each cell of the n × m n \times m n×m rectangle belongs to exactly one brick. Here n n n is the height of the rectangle n × m n \times m n×m and m m m is the width. Note that there can be bricks with different values of k in the same brick wall.

The wall stability is the difference between the number of horizontal bricks and the number of vertical bricks. Note that if you used 0 0 0 horizontal bricks and 2 2 2 vertical ones, then the stability will be − 2 -2 2, not 2 2 2.

What is the maximal possible stability of a wall of size n × m n \times m n×m?

It is guaranteed that under restrictions in the statement at least one n × m n \times m n×m wall exists.

Input

The first line of the input contains one integer t t t ( 1 ≤ t ≤ 10 000 1 \le t \le 10\,000 1t10000), the number of test cases.

The only line of each test case contains two integers n n n and m m m ( 2 ≤ n , m ≤ 1 0 4 2 \le n,\,m \le 10^4 2n,m104).

Output

For each test case, print one integer, the maximum stability of a wall of size n × m n \times m n×m.

Example

Example

input
5
2 2
7 8
16 9
3 5
10000 10000
output
2
28
64
6
50000000

Note

In the 1st test case, the maximum stability of 2 2 2 is obtained by placing two horizontal bricks 1 × 2 1 \times 2 1×2 one on top of the other.

In the 2nd test case, one can get the maximum stability of 28 28 28 by placing 4 4 4 horizontal bricks 1 × 2 1 \times 2 1×2 in each of the 7 7 7 rows.

Solution

A


Code

#include <bits/stdc++.h>
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;void solve()
{int N, M;cin >> N >> M;cout << N * (M / 2) << endl;
}signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int Data;cin >> Data;while (Data --)solve();return 0;
}

B. Minimize Inversions

Problem Statement

You are given two permutations a a a and b b b of length n n n. A permutation is an array of n n n elements from 1 1 1 to n n n where all elements are distinct. For example, an array [ 2 , 1 , 3 2,1,3 2,1,3] is a permutation, but [ 0 , 1 0,1 0,1] and [ 1 , 3 , 1 1,3,1 1,3,1] aren’t.

You can (as many times as you want) choose two indices i i i and j j j, then swap a i a_i ai with a j a_j aj and b i b_i bi with b j b_j bj simultaneously.

You hate inversions, so you want to minimize the total number of inversions in both permutations.

An inversion in a permutation p p p is a pair of indices ( i , j ) (i, j) (i,j) such that KaTeX parse error: Expected 'EOF', got '&' at position 3: i &̲lt; j and KaTeX parse error: Expected 'EOF', got '&' at position 5: p_i &̲gt; p_j. For example, if p = [ 3 , 1 , 4 , 2 , 5 ] p=[3,1,4,2,5] p=[3,1,4,2,5] then there are 3 3 3 inversions in it (the pairs of indices are ( 1 , 2 ) (1,2) (1,2), ( 1 , 4 ) (1,4) (1,4) and ( 3 , 4 ) (3,4) (3,4)).

Input

The first line contains an integer t t t ( 1 ≤ t ≤ 20 000 1 \leq t \leq 20\,000 1t20000) — the number of test cases.

Each test case consists of three lines. The first line contains an integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \leq n \leq 2\cdot10^5 1n2105) — the length of the permutations a a a and b b b. The second line contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( 1 ≤ a i ≤ n 1 \leq a_i \leq n 1ain) — permutation a a a. The third line contains b b b in a similar format.

It is guaranteed that the sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2\cdot10^5 2105.

Output

For each test case, output two permutations a ′ a' a and b ′ b' b (in the same format as in the input) — the permutations after all operations. The total number of inversions in a ′ a' a and b ′ b' b should be the minimum possible among all pairs of permutations that can be obtained using operations from the statement.

If there are multiple solutions, print any of them.

Example

Example

input
3
5
1 2 3 4 5
5 4 3 2 1
3
3 1 2
3 1 2
6
2 5 6 1 3 4
1 5 3 6 2 4
output
3 2 5 1 4
3 4 1 5 2
1 2 3
1 2 3
2 3 4 6 5 1
1 2 4 3 5 6

Note

In the first test case, the minimum possible number of inversions is 10 10 10.

In the second test case, we can sort both permutations at the same time. For this, the following operations can be done:

  • Swap the elements in the positions 1 1 1 and 3 3 3 in both permutations. After the operation, a = a = a= [ 2 , 1 , 3 2,1,3 2,1,3], b = b = b= [ 2 , 1 , 3 2,1,3 2,1,3].
  • Swap the elements in the positions 1 1 1 and 2 2 2. After the operations, a a a and b b b are sorted.

In the third test case, the minimum possible number of inversions is 7 7 7.

Solution

B


Code

#include <bits/stdc++.h>
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;const int SIZE = 2e5 + 10;int N;
PII A[SIZE];int Cmp(PII a, PII b)
{if (a.first + a.second == b.first + b.second)return abs(a.first - a.second) < abs(b.first - b.second);return a.first + a.second < b.first + b.second;
}void solve()
{cin >> N;for (int i = 1; i <= N; i ++)cin >> A[i].first;for (int i = 1; i <= N; i ++)cin >> A[i].second;sort(A + 1, A + 1 + N, Cmp);for (int i = 1; i <= N; i ++)cout << A[i].first << " ";cout << endl;for (int i = 1; i <= N; i ++)cout << A[i].second << " ";cout << endl;
}signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int Data;cin >> Data;while (Data --)solve();return 0;
}

C. XOR-distance

Problem Statement

You are given integers a a a, b b b, r r r. Find the smallest value of ∣ ( a ⊕ x ) − ( b ⊕ x ) ∣ |({a \oplus x}) - ({b \oplus x})| (ax)(bx) among all 0 ≤ x ≤ r 0 \leq x \leq r 0xr.

⊕ \oplus is the operation of bitwise XOR, and ∣ y ∣ |y| y is absolute value of y y y.

Input

The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104) — the number of test cases.

Each test case contains integers a a a, b b b, r r r ( 0 ≤ a , b , r ≤ 1 0 18 0 \le a, b, r \le 10^{18} 0a,b,r1018).

Output

For each test case, output a single number — the smallest possible value.

Example

input
10
4 6 0
0 3 2
9 6 10
92 256 23
165 839 201
1 14 5
2 7 2
96549 34359 13851
853686404475946 283666553522252166 127929199446003072
735268590557942972 916721749674600979 895150420120690183
output
2
1
1
164
542
5
3
37102
27934920819538516
104449824168870225

Note

In the first test, when r = 0 r = 0 r=0, then x x x is definitely equal to 0 0 0, so the answer is ∣ 4 ⊕ 0 − 6 ⊕ 0 ∣ = ∣ 4 − 6 ∣ = 2 |{4 \oplus 0} - {6 \oplus 0}| = |4 - 6| = 2 4060=∣46∣=2.

In the second test:

  • When x = 0 x = 0 x=0, ∣ 0 ⊕ 0 − 3 ⊕ 0 ∣ = ∣ 0 − 3 ∣ = 3 |{0 \oplus 0} - {3 \oplus 0}| = |0 - 3| = 3 0030=∣03∣=3.
  • When x = 1 x = 1 x=1, ∣ 0 ⊕ 1 − 3 ⊕ 1 ∣ = ∣ 1 − 2 ∣ = 1 |{0 \oplus 1} - {3 \oplus 1}| = |1 - 2| = 1 0131=∣12∣=1.
  • When x = 2 x = 2 x=2, ∣ 0 ⊕ 2 − 3 ⊕ 2 ∣ = ∣ 2 − 1 ∣ = 1 |{0 \oplus 2} - {3 \oplus 2}| = |2 - 1| = 1 0232=∣21∣=1.

Therefore, the answer is 1 1 1.

In the third test, the minimum is achieved when x = 1 x = 1 x=1.

Solution

C


Code

#include <bits/stdc++.h>
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;void solve()
{int A, B, R;string R1, R2;cin >> A >> B >> R;if (A == B){cout << 0 << endl;return;}if (A < B) swap(A, B);for (int i = 0; i < 64; i ++)R1 += ((A >> i & 1) ^ 48);for (int i = 0; i < 64; i ++)R2 += ((B >> i & 1) ^ 48);int Vis[65] = {0};int Highest;for (int i = 63; i >= 0; i --)if ((A >> i & 1) != (B >> i & 1)){Highest = i;break;}for (int i = 0; i < Highest; i ++)if ((A >> i & 1) && !(B >> i & 1))Vis[i] = 1;int Limit = 1;for (int i = 63; i >= 0; i --){if ((R >> i & 1) && !Vis[i]) Limit = 0;if (Vis[i]){if (Limit){if (R >> i & 1) swap(R1[i], R2[i]);else continue;}elseswap(R1[i], R2[i]);}}int T1 = 0, T2 = 0;for (int i = 0; i < 64; i ++){if (R1[i] == '1')T1 += (1ll << i);if (R2[i] == '1')T2 += (1ll << i);}cout << T1 - T2 << endl;
}signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int Data;cin >> Data;while (Data --)solve();return 0;
}

D. Blocking Elements

Problem Statement

You are given an array of numbers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an. Your task is to block some elements of the array in order to minimize its cost. Suppose you block the elements with indices 1 ≤ b 1 < b 2 < … < b m ≤ n 1 \leq b_1 < b_2 < \ldots < b_m \leq n 1b1<b2<<bmn. Then the cost of the array is calculated as the maximum of:

  • the sum of the blocked elements, i.e., a b 1 + a b 2 + … + a b m a_{b_1} + a_{b_2} + \ldots + a_{b_m} ab1+ab2++abm.
  • the maximum sum of the segments into which the array is divided when the blocked elements are removed. That is, the maximum sum of the following ( m + 1 m + 1 m+1) subarrays: [ 1 , b 1 − 1 1, b_1 − 1 1,b11], [ b 1 + 1 , b 2 − 1 b_1 + 1, b_2 − 1 b1+1,b21], [ … \ldots ], [ b m − 1 + 1 , b m − 1 b_{m−1} + 1, b_m - 1 bm1+1,bm1], [ b m + 1 , n b_m + 1, n bm+1,n] (the sum of numbers in a subarray of the form [ x , x − 1 x,x − 1 x,x1] is considered to be 0 0 0).

For example, if n = 6 n = 6 n=6, the original array is [ 1 , 4 , 5 , 3 , 3 , 2 1, 4, 5, 3, 3, 2 1,4,5,3,3,2], and you block the elements at positions 2 2 2 and 5 5 5, then the cost of the array will be the maximum of the sum of the blocked elements ( 4 + 3 = 7 4 + 3 = 7 4+3=7) and the sums of the subarrays ( 1 1 1, 5 + 3 = 8 5 + 3 = 8 5+3=8, 2 2 2), which is max ⁡ ( 7 , 1 , 8 , 2 ) = 8 \max(7,1,8,2) = 8 max(7,1,8,2)=8.

You need to output the minimum cost of the array after blocking.

Input

The first line of the input contains a single integer t t t ( 1 ≤ t ≤ 30 000 1 \leq t \leq 30\,000 1t30000) — the number of queries.

Each test case consists of two lines. The first line contains an integer n n n ( 1 ≤ n ≤ 1 0 5 1 \leq n \leq 10^5 1n105) — the length of the array a a a. The second line contains n n n elements a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( 1 ≤ a i ≤ 1 0 9 1 \leq a_i \leq 10^9 1ai109) — the array a a a.

It is guaranteed that the sum of n n n over all test cases does not exceed 1 0 5 10^5 105.

Output

For each test case, output a single number — the minimum cost of blocking the array.

Example

Example

input
3
6
1 4 5 3 3 2
5
1 2 3 4 5
6
4 1 6 3 10 7
output
7
5
11

Note

The first test case matches with the array from the statement. To obtain a cost of 7 7 7, you need to block the elements at positions 2 2 2 and 4 4 4. In this case, the cost of the array is calculated as the maximum of:

  • the sum of the blocked elements, which is a 2 + a 4 = 7 a_2 + a_4 = 7 a2+a4=7.
  • the maximum sum of the segments into which the array is divided when the blocked elements are removed, i.e., the maximum of a 1 a_1 a1, a 3 a_3 a3, a 5 + a 6 = max ⁡ ( 1 , 5 , 5 ) = 5 a_5 + a_6 = \max(1,5,5) = 5 a5+a6=max(1,5,5)=5.

So the cost is max ⁡ ( 7 , 5 ) = 7 \max(7,5) = 7 max(7,5)=7.

In the second test case, you can block the elements at positions 1 1 1 and 4 4 4.

In the third test case, to obtain the answer 11 11 11, you can block the elements at positions 2 2 2 and 5 5 5. There are other ways to get this answer, for example, blocking positions 4 4 4 and 6 6 6.

Solution

D

Code

#include <bits/stdc++.h>
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;const int SIZE = 1e5 + 10;int N;
int A[SIZE], S[SIZE];
int F[SIZE];struct Node
{int l, r;int v;
}tree[SIZE * 4];inline void pushup(int u)
{tree[u].v = min(tree[u << 1].v, tree[u << 1 | 1].v);
}inline void build(int u, int l, int r)
{tree[u] = {l, r, (int)1e18};if (l == r) return;int mid = l + r >> 1;build(u << 1, l, mid);build(u << 1 | 1, mid + 1, r);
}inline void modify(int u, int x, int c)
{if (tree[u].l == x && tree[u].r == x) tree[u].v = c;else {int mid = tree[u].l + tree[u].r >> 1;if (x <= mid) modify(u << 1, x, c);else modify(u << 1 | 1, x, c);pushup(u);}return;
}inline int query(int u, int l, int r)
{if (tree[u].l >= l && tree[u].r <= r) return tree[u].v;int mid = tree[u].l + tree[u].r >> 1, v = 1e18;if (l <= mid) v = query(u << 1, l, r);if (r > mid) v = min(v, query(u << 1 | 1, l, r));return v;
}bool Check(int X)
{build(1, 0, N);modify(1, 0, 0);for (int i = 1; i <= N; i ++){int P = lower_bound(S, S + 1 + i - 1, S[i - 1] - X) - S;modify(1, i, query(1, P, i - 1) + A[i]);// cout << query(1, P, i - 1) << " " << P << " " << A[i] << endl;}for (int i = 1; i <= N; i ++)if (max(query(1, i, i), S[N] - S[i]) <= X)return 1;return 0;
}void solve()
{cin >> N;int Sum = 0;for (int i = 1; i <= N; i ++)cin >> A[i], Sum += A[i], S[i] = S[i - 1] + A[i];int l = 0, r = Sum;while (l < r){int mid = l + r >> 1;if (Check(mid)) r = mid;else l = mid + 1;}cout << r << endl;
}signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int Data;cin >> Data;while (Data --)solve();return 0;
}

E. ace5 and Task Order

Problem Statement

This is an interactive problem!

In the new round, there were n n n tasks with difficulties from 1 1 1 to n n n. The coordinator, who decided to have the first round with tasks in unsorted order of difficulty, rearranged the tasks, resulting in a permutation of difficulties from 1 1 1 to n n n. After that, the coordinator challenged ace5 to guess the permutation in the following way.

Initially, the coordinator chooses a number x x x from 1 1 1 to n n n.

ace5 can make queries of the form: ? i ?\ i ? i. The answer will be:

  • > > >, if a i > x a_i > x ai>x, after which x x x increases by 1 1 1.
  • < < <, if a i < x a_i < x ai<x, after which x x x decreases by 1 1 1.
  • = = =, if a i = x a_i = x ai=x, after which x x x remains unchanged.

The task for ace5 is to guess the permutation in no more than 40 n 40n 40n queries. Since ace5 is too busy writing the announcement, he has entrusted this task to you.

Input

The first line contains a single integer t t t ( 1 ≤ t ≤ 1000 1 \leq t \leq 1000 1t1000) — the number of test cases.

Interaction

The interaction between your program and the jury’s program begins with reading a positive integer n n n ( 1 ≤ n ≤ 2000 1 \leq n \leq 2000 1n2000) — the length of the hidden permutation.

To make a query, output a line in the format “? i”, where 1 ≤ i ≤ n 1 \leq i \leq n 1in.

As an answer, you will receive:

  • “>”, if a i a_i ai > x x x, after which x x x will increase by 1 1 1.
  • “<”, if a i a_i ai < x x x, after which x x x will decrease by 1 1 1.
  • “=”, if a i a_i ai = x x x, after which x x x will remain unchanged.

You can make no more than 40 n 40n 40n queries. To output the answer, you need to print “! a_1 a_2 … a_n”, where 1 ≤ a i ≤ n 1 \leq a_i \leq n 1ain, and all of them are distinct. Outputting the answer does not count as a query.

If your program makes more than 40 n 40n 40n queries for one test case, or makes an invalid query, then the response to the query will be -1. After receiving such a response, your program should immediately terminate to receive the verdict Wrong Answer. Otherwise, it may receive any other verdict.

After outputting a query, do not forget to print a newline and flush the output buffer. Otherwise, you will receive the verdict Presentation Error. To flush the buffer, use:

  • fflush(stdout) or cout.flush() in C++;
  • System.out.flush() in Java;
  • flush(output) in Pascal;
  • stdout.flush() in Python;
  • see the documentation for other languages.

It is guaranteed that the sum of n n n over all test cases does not exceed 2000 2000 2000.

The interactor in this problem is not adaptive.

Hacks:

To make a hack, use the following format:

The first line contains a single integer t t t — the number of test cases.

The description of each test case should consist of two lines. The first line contains the numbers n n n and x x x ( 1 ≤ x ≤ n ≤ 2000 1 \leq x \leq n \leq 2000 1xn2000) — the length of the hidden permutation and the initial value of the number x x x. The second line contains n n n distinct numbers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( 1 ≤ a i ≤ n 1 \leq a_i \leq n 1ain) — the permutation that the jury should choose in this test case.

Sum of n n n over all test cases should not exceed 2000 2000 2000.

Example

input
2
5
>
=
<
=
<
<
2
>
output
? 4
? 2
? 1
? 5
? 1
? 3
! 2 4 1 5 3
? 1
! 2 1

Solution

E


Code

方法一:

#include <bits/stdc++.h>
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;const int SIZE = 2e3 + 10;int N;
int Result[SIZE];char Ask(int X)
{cout << "? " << X << endl;char T;cin >> T;return T;
}void Quick_Sort(vector<int> &A, vector<int> &Order)
{if (!A.size()) return;int mid = rand() % (int)A.size();while (Ask(A[mid]) != '=');std::vector<int> L, R;for (int i = 0; i < A.size(); i ++){if (i == mid) continue;else if (Ask(A[i]) == '<')L.push_back(A[i]);elseR.push_back(A[i]);Ask(A[mid]);}vector<int> L_Order, R_Order;Quick_Sort(L, L_Order), Quick_Sort(R, R_Order);for (auto c : L_Order)Order.push_back(c);Order.push_back(A[mid]);for (auto c : R_Order)Order.push_back(c);
}void solve()
{cin >> N;std::vector<int> A, Order;for (int i = 1; i <= N; i ++)A.push_back(i);Quick_Sort(A, Order);for (int i = 1; i <= N; i ++)Result[Order[i - 1]] = i;cout << "! ";for (int i = 1; i <= N; i ++)cout << Result[i] << " ";cout << endl;
}signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);srand(time(0));int Data;cin >> Data;while (Data --)solve();return 0;
}

方法二:

#include <bits/stdc++.h>
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;const int SIZE = 2e3 + 10;int N;
int L[SIZE], R[SIZE], Result[SIZE];char Ask(int Id)
{cout << "? " << Id << endl;char T;cin >> T;return T;
}void solve()
{cin >> N;int X = 0, Cnt = 0;for (int i = 1; i <= N; i ++)L[i] = -N - 1, R[i] = N + 1;while (Cnt < N){int Max = -1e18, Id;for (int j = 1; j <= N; j ++){if (L[j] == R[j]) continue;if (min(X - L[j], R[j] - X) > Max)Max = min(X - L[j], R[j] - X), Id = j;}char T = Ask(Id);if (T == '>'){X ++;L[Id] = max(L[Id], X);}else if (T == '<'){X --;R[Id] = min(R[Id], X);}elseL[Id] = R[Id] = X;if (L[Id] == R[Id]){Cnt ++;Result[Id] = X;}}int Min = 1e18;for (int i = 1; i <= N; i ++)Min = min(Min, Result[i]);cout << "! ";for (int i = 1; i <= N; i ++)cout << Result[i] - Min + 1 << " ";cout << endl;
}signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int Data;cin >> Data;while (Data --)solve();return 0;
}

F. Caterpillar on a Tree

Problem Statement

The caterpillar decided to visit every node of the tree. Initially, it is sitting at the root.

The tree is represented as a rooted tree with the root at the node 1 1 1. Each crawl to a neighboring node takes 1 1 1 minute for the caterpillar.

And there is a trampoline under the tree. If the caterpillar detaches from the tree and falls onto the trampoline, it will end up at the root of the tree in 0 0 0 seconds. But the trampoline is old and can withstand no more than k k k caterpillar’s falls.

What is the minimum time the caterpillar can take to visit all the nodes of the tree?

More formally, we need to find the minimum time required to visit all the nodes of the tree, if the caterpillar starts at the root (node 1 1 1) and moves using two methods.

  1. Crawl along an edge to one of the neighboring nodes: takes 1 1 1 minute.
  2. Teleport to the root: takes no time, no new nodes become visited.

The second method (teleportation) can be used at most k k k times. The caterpillar can finish the journey at any node.

Input

The first line of the input contains two integers: n n n ( 2 ≤ n ≤ 2 ⋅ 1 0 5 2 \le n \le 2\cdot 10^5 2n2105) — the number of nodes in the tree, and k k k ( 0 ≤ k ≤ 1 0 9 0 \le k \le 10^9 0k109) — the maximum number of teleports to the root.

The second line contains n − 1 n-1 n1 integers p 2 p_2 p2, p 3 p_3 p3, …, p n p_n pn ( 1 ≤ p i ≤ n 1 \le p_i \le n 1pin) — the ancestors in the tree for nodes 2 , 3 , … , n 2, 3, \ldots, n 2,3,,n; node 1 1 1 is the root.

Output

Print a single number in a single line — the minimum number of minutes required to visit all the nodes of the tree.

Example

input
8 1
1 1 2 2 4 3 3
output
9
input
4 0
4 4 1
output
4

Note

The figure shows one of the ways to traverse the tree from the first test in 9 minutes.

Solution

F

Code

#include <bits/stdc++.h>
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;const int SIZE = 2e5 + 10;int N, K;
std::vector<int> G[SIZE];
int Longest[SIZE], Son[SIZE];
std::vector<int> Euler;
int Vis[SIZE], Depth[SIZE];void DFS(int u, int fa)
{for (auto c : G[u]){Depth[c] = Depth[u] + 1;DFS(c, u);if (Longest[c] + 1 > Longest[u])Longest[u] = Longest[c] + 1, Son[u] = c;}
}void DFS2(int u, int fa)
{Euler.push_back(u);for (auto c : G[u]){if (c == Son[u]) continue;DFS2(c, u);Euler.push_back(u);}if (Son[u])DFS2(Son[u], u), Euler.push_back(u);
}signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);cin >> N >> K;int Fa;for (int i = 2; i <= N; i ++)cin >> Fa, G[Fa].push_back(i);DFS(1, -1);DFS2(1, -1);vector<int> Pos;for (int i = 0; i < Euler.size(); i ++)if (!Vis[Euler[i]])Vis[Euler[i]] = 1, Pos.push_back(i);vector<int> Dis;int Result = Euler.size() - 1 - Longest[1];for (int i = 1; i < Pos.size(); i ++)Dis.push_back(Pos[i] - Pos[i - 1] - Depth[Euler[Pos[i]]]);sort(Dis.begin(), Dis.end(), greater<int>());for (int i = 0; i < min(K, (int)Dis.size()); i ++)Result -= max(Dis[i], 0ll);cout << Result << endl;return 0;
}

最后祝大家早日在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/659423.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Pandas进阶--map映射,分组聚合和透视pivot_table详解

文章目录 1.Pandas的map映射&#xff08;1&#xff09;映射&#xff08;2&#xff09;map充当运算工具 2.数据分组和透视&#xff08;1&#xff09;分组统计 - groupby功能 是pandas最重要的功能&#xff08;2&#xff09;聚合agg 3.透视表pivot_table&#xff08;1&#xff09…

小程序上的h5页面在web上面访问 页面基于vue

前提&#xff1a;BASE_URL: https://ded.toll.keeke.ai/ded-api/ 页面路径是&#xff1a;pages/webview/webview 参数是&#xff1a;id111 列表页跳转到详情页的方法是 toDetail(e) {wx.navigateTo({url: ../webview/webview?id e.currentTarget.dataset.oid})}, vue的映射…

【日常问题】Failed to enable unit: Unit file docker.service does not exist.

Failed to enable unit: Unit file docker.service does not exist. 1. 问题原因 笔者问题的产生是因为在ubuntu20.04下采用snapd安装的docker&#xff0c;因此 systemctl restart docker.servicesystemd并不能找到守护进程docker.service 同时使用docker命令时还会产生若干…

React Router 完美教程(上)

概述 什么叫路由呢&#xff0c;说白了就是如何处理页面的跳转。在传统的网站中&#xff0c;我们都是向服务器请求页面及相应的css和js代码。自从前后端分离的相思提出后&#xff0c;一堆基于js虚拟Dom的框架应运而生。React就是其中优秀的代表作之一。这种方式极大的优化了开发…

Conventional SortSearch

Merge Sort——from bottom to top(iteration) void merge_sort( ElementType list[], ElementType sorted[], int N, int length ){int size1;for(;size<N;size*2){for(int i0;i<N;i2*size){int l1i; int l2sizei;int locl1;while(l1<isize&&l1<N&…

etcd自动化安装配置教程

文章目录 前言一、简介1. 简介2. 特点3. 端口介绍 二、etcd安装教程&#xff08;单机版&#xff09;1. 复制脚本2. 增加执行权限3. 执行脚本4. 查看启动状态5. 卸载etcd 三、etcd安装教程&#xff08;集群版&#xff09;1. 复制脚本2. 增加执行权限3. 分发脚本4. 执行脚本5. 启…

中科大计网学习记录笔记(一):Internet | 网络边缘

计算机网络 前言&#xff1a; 学习视频&#xff1a;中科大郑烇、杨坚全套《计算机网络&#xff08;自顶向下方法 第7版&#xff0c;James F.Kurose&#xff0c;Keith W.Ross&#xff09;》课程 该视频是B站非常著名的计网学习视频&#xff0c;但相信很多朋友和我一样在听完前面…

【JavaSE篇】——内部类

目录 &#x1f393;内部类 &#x1f388;内部类的分类 &#x1f6a9;实例内部类 一.如何实例内部类对象 二.实例内部类中为什么不能有静态成员变量 &#xff08;用final解决&#xff09; 三.在实例内部类对象时&#xff0c;如何访问外部类当中相同的成员变量&#xff1f;…

SpringMVC-基本概念

一、引子 我们在上篇文章Spring集成Web中抛出了一个问题&#xff1a;为什么我们一直在自用Java Web阶段使用的Servlet来承接客户端浏览器的请求呢&#xff0c;我们熟知甚至是已经在日常开发中经常使用的Controller又与之有什么关系呢&#xff1f;我们将在本篇文章解答读者的这…

检测CUDA 是否能访问GPU时回应速度慢【笔记】

SUPWEMICRO 418G-Q20X12 维护记录&#xff1a; 两台设备均已安装CUDA与Pytorch&#xff0c;在检测CUDA 是否能访问GPU&#xff0c;执行torch.cuda.is_available()命令时&#xff0c;一台设备速度秒回应True&#xff0c;但另外一台设备回应速度慢&#xff08;1分钟左右&#xff…

node,node-sass,sass-loader之间的版本关系

前言 安装配置node-sass 以及 sass-loader想必是很多前端的噩梦–一不小心又不成功还得装个半天。 下面说一下这个问题。 当然&#xff0c;你肯定遇到过&#xff1a; Node Sass version 9.0.0 is incompatible with ^4.0.0-这样的问题&#xff0c;这个也是因为三者关系对不上…

【PyQt】02-基本UI

文章目录 前言一、首先了解什么是GUI&#xff1f;二、初学程序1.界面展示代码运行结果 2.控件2.1按钮展示代码运行结果 2.2 纯文本和输入框代码运行结果 3、重新设置大小 -resize4、移动窗口-move()5、设置界面在电脑中央5.1 代码运行结果 6、设置窗口图标代码运行结果 7、布局…

Django模型(二)

一、更新数据库表结构 不管是新增模型,还是修改已有模型后,只需要执行行命令即可: 1.1、创建迁移 在项目根目录的cmd中运行: $ python manage.py makemigrations model_app备注 model_app是子应用的名称,如果不指定,那么就是对所有 INSTALLED_APPS 中的应用都进行预备…

开发数据产品+AI产品通关上岸课程

该课程全面解析数据产品和人工智能产品的开发与设计。学员将学习产品规划、数据分析以及AI技术应用&#xff0c;通过案例实践掌握产品开发流程&#xff0c;致力于帮助他们成功进入数据和人工智能产品领域。 课程大小&#xff1a;9.8G 课程下载&#xff1a;https://download.cs…

Java多线程共享变量控制volatile

1. volatile实现可见性&#xff08;jdk 1.5后&#xff09; 1. 可见性 如果一个线程对共享变量值的修改&#xff0c;能够及时的被其他线程看到&#xff0c;叫做共享变量的可见性。如果一个变量同时在多个线程的工作内存中存在副本&#xff0c;那么这个变量就叫共享变量 volati…

如何从零开始开发一个PS5浏览器 | How to develop a PS5 browser

环境&#xff1a;Windows PS5一台 问题&#xff1a;PS5折腾需要使用PKG浏览器访问特定网址&#xff0c;如何自定义网址呢&#xff1f; 解决办法&#xff1a;使用开发套件PS Multi Tools开发一个空应用&#xff0c;利于deeplinkUri 参数访问网页 背景&#xff1a;PS5折腾后&…

dockerpipwork相关测试过程

pipework可以减轻docker实施过程中的工作量&#xff0c;在网上也找了几篇类似的文章&#xff0c;按照相应配置&#xff0c;结果并不相同 如下测试过程记录下&#xff1a; docker run -it --rm --name c1 busybox docker run -it --rm --name c2 busyboxpipework br1 c1 192…

Altium Designer的学习

PCB设计流程 1.新建空白工程&#xff1a; 创建一个新的工程 新建四个文件&#xff0c;并且保存&#xff1a; 每次打开文件时&#xff0c;打开以.PrjPcb结尾的文件 2.元件符号的创建&#xff1a; 在绘制图形的时候设置成10mil,为了在原理图中显得不那么大。 在绘制引脚的时候设…

拦截器,AOP,自定义注解的使用

自定义注解AOP&#xff0c;实现 进入方法打印参数日志 /*** 定义进入方法前打印日志注解* author zy*/ Target(ElementType.METHOD) Retention(RetentionPolicy.RUNTIME) Documented public interface PrintLog {} /*** 定义一个切面&#xff0c;在使用PrintLog注解的方法进…

Hadoop3.x基础(2)- HDFS

来源&#xff1a;B站尚硅谷 目录 HDFS概述HDFS产出背景及定义HDFS优缺点HDFS组成架构HDFS文件块大小&#xff08;面试重点&#xff09; HDFS的Shell操作&#xff08;开发重点&#xff09;基本语法命令大全常用命令实操准备工作上传下载HDFS直接操作 HDFS的API操作HDFS的API案例…