A bitstring is a string that contains only the characters 0 and 1.
Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length $$$2n$$$. A valid novel for the contest is a bitstring of length at most $$$3n$$$ that contains at least two of the three given strings as subsequences.
Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.
A string $$$a$$$ is a subsequence of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero) characters.
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.
The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).
Each of the following three lines contains a bitstring of length $$$2n$$$. It is guaranteed that these three strings are pairwise distinct.
It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$10^5$$$.
For each test case, print a single line containing a bitstring of length at most $$$3n$$$ that has at least two of the given bitstrings as subsequences.
It can be proven that under the constraints of the problem, such a bitstring always exists.
If there are multiple possible answers, you may output any of them.
2 1 00 11 01 3 011001 111010 010001
010 011001010
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.
In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010.
Seiji Maki doesn't only like to observe relationships being unfolded, he also likes to observe sequences of numbers, especially permutations. Today, he has his eyes on almost sorted permutations.
A permutation $$$a_1, a_2, \dots, a_n$$$ of $$$1, 2, \dots, n$$$ is said to be almost sorted if the condition $$$a_{i + 1} \ge a_i - 1$$$ holds for all $$$i$$$ between $$$1$$$ and $$$n - 1$$$ inclusive.
Maki is considering the list of all almost sorted permutations of $$$1, 2, \dots, n$$$, given in lexicographical order, and he wants to find the $$$k$$$-th permutation in this list. Can you help him to find such permutation?
Permutation $$$p$$$ is lexicographically smaller than a permutation $$$q$$$ if and only if the following holds:
The first line contains a single integer $$$t$$$ ($$$1\le t\le 1000$$$) — the number of test cases.
Each test case consists of a single line containing two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10^5$$$, $$$1 \le k \le 10^{18}$$$).
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
For each test case, print a single line containing the $$$k$$$-th almost sorted permutation of length $$$n$$$ in lexicographical order, or $$$-1$$$ if it doesn't exist.
5 1 1 1 2 3 3 6 5 3 4
1 -1 2 1 3 1 2 4 3 5 6 3 2 1
For the first and second test, the list of almost sorted permutations with $$$n = 1$$$ is $$$\{[1]\}$$$.
For the third and fifth test, the list of almost sorted permutations with $$$n = 3$$$ is $$$\{[1, 2, 3], [1, 3, 2], [2, 1, 3], [3, 2, 1]\}$$$.
As a teacher, Riko Hakozaki often needs to help her students with problems from various subjects. Today, she is asked a programming task which goes as follows.
You are given an undirected complete graph with $$$n$$$ nodes, where some edges are pre-assigned with a positive weight while the rest aren't. You need to assign all unassigned edges with non-negative weights so that in the resulting fully-assigned complete graph the XOR sum of all weights would be equal to $$$0$$$.
Define the ugliness of a fully-assigned complete graph the weight of its minimum spanning tree, where the weight of a spanning tree equals the sum of weights of its edges. You need to assign the weights so that the ugliness of the resulting graph is as small as possible.
As a reminder, an undirected complete graph with $$$n$$$ nodes contains all edges $$$(u, v)$$$ with $$$1 \le u \lt v \le n$$$; such a graph has $$$\frac{n(n-1)}{2}$$$ edges.
She is not sure how to solve this problem, so she asks you to solve it for her.
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n \le 2 \cdot 10^5$$$, $$$0 \le m \le \min(2 \cdot 10^5, \frac{n(n-1)}{2} - 1)$$$) — the number of nodes and the number of pre-assigned edges. The inputs are given so that there is at least one unassigned edge.
The $$$i$$$-th of the following $$$m$$$ lines contains three integers $$$u_i$$$, $$$v_i$$$, and $$$w_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u \ne v$$$, $$$1 \le w_i \lt 2^{30}$$$), representing the edge from $$$u_i$$$ to $$$v_i$$$ has been pre-assigned with the weight $$$w_i$$$. No edge appears in the input more than once.
Print on one line one integer — the minimum ugliness among all weight assignments with XOR sum equal to $$$0$$$.
4 4 2 1 14 1 4 14 3 2 15 4 3 8
15
6 6 3 6 4 2 4 1 4 5 7 3 4 10 3 5 1 5 2 15
0
5 6 2 3 11 5 3 7 1 4 10 2 4 14 4 3 8 2 5 6
6
The following image showcases the first test case. The black weights are pre-assigned from the statement, the red weights are assigned by us, and the minimum spanning tree is denoted by the blue edges.
Based on a peculiar incident at basketball practice, Akari came up with the following competitive programming problem!
You are given $$$n$$$ points on the plane, no three of which are collinear. The $$$i$$$-th point initially has a label $$$a_i$$$, in such a way that the labels $$$a_1, a_2, \dots, a_n$$$ form a permutation of $$$1, 2, \dots, n$$$.
You are allowed to modify the labels through the following operation:
A sequence of operations is valid if after applying all of the operations in the sequence in order, the $$$k$$$-th point ends up having the label $$$k$$$ for all $$$k$$$ between $$$1$$$ and $$$n$$$ inclusive, and the drawn segments don't intersect each other internally. Formally, if two of the segments intersect, then they must do so at a common endpoint of both segments.
In particular, all drawn segments must be distinct.
Find any valid sequence of operations, or say that none exist.
The first line contains an integer $$$n$$$ ($$$3 \le n \le 2000$$$) — the number of points.
The $$$i$$$-th of the following $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$a_i$$$ ($$$-10^6 \le x_i, y_i \le 10^6$$$, $$$1 \le a_i \le n$$$), representing that the $$$i$$$-th point has coordinates $$$(x_i, y_i)$$$ and initially has label $$$a_i$$$.
It is guaranteed that all points are distinct, no three points are collinear, and the labels $$$a_1, a_2, \dots, a_n$$$ form a permutation of $$$1, 2, \dots, n$$$.
If it is impossible to perform a valid sequence of operations, print $$$-1$$$.
Otherwise, print an integer $$$k$$$ ($$$0 \le k \le \frac{n(n - 1)}{2}$$$) — the number of operations to perform, followed by $$$k$$$ lines, each containing two integers $$$i$$$ and $$$j$$$ ($$$1 \le i, j \le n$$$, $$$i\neq j$$$) — the indices of the points chosen for the operation.
Note that you are not required to minimize or maximize the value of $$$k$$$.
If there are multiple possible answers, you may print any of them.
5 -1 -2 2 3 0 5 1 3 4 4 -3 3 5 2 1
5 1 2 5 3 4 5 1 5 1 3
3 5 4 1 0 0 2 -3 -2 3
0
The following animation showcases the first sample test case. The black numbers represent the indices of the points, while the boxed orange numbers represent their labels.
In the second test case, all labels are already in their correct positions, so no operations are necessary.
Yuu Koito and Touko Nanami are newlyweds! On the wedding day, Yuu gifted Touko a directed tree with $$$n$$$ nodes and rooted at $$$1$$$, and a labeling $$$a$$$ which is some DFS order of the tree. Every edge in this tree is directed away from the root.
After calling dfs(1) the following algorithm returns $$$a$$$ as a DFS order of a tree rooted at $$$1$$$ :
order := 0
a := array of length n
function dfs(u):
order := order + 1
a[u] := order
for all v such that there is a directed edge (u -> v):
dfs(v)
Note that there may be different DFS orders for a given tree.
Touko likes the present so much she decided to play with it! On each day following the wedding day, Touko performs this procedure once:
Days have passed since their wedding, and Touko has somehow forgotten which date the wedding was and what was the original labeling $$$a$$$! Fearing that Yuu might get angry, Touko decided to ask you to derive these two pieces of information using the current labeling.
Being her good friend, you need to find the number of days that have passed since the wedding, and the original labeling of the tree. However, there is a chance that Touko might have messed up her procedures, which result in the current labeling being impossible to obtain from some original labeling; in that case, please inform Touko as well.
The first line of the input contains an integer $$$n$$$ ($$$2 \le n \le 3 \cdot 10^5$$$) — the number of nodes on the tree.
The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 \le a_i \le n$$$, all $$$a_i$$$ are distinct) — the current labeling of the tree.
Each of the next $$$n - 1$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u, v \le n$$$, $$$u \neq v$$$), describing an directed edge from $$$u_i$$$ to $$$v_i$$$. The edges form a directed tree rooted at $$$1$$$.
If the current labeling is impossible to arrive at from any DFS order, print NO.
Else, on the first line, print YES. On the second line, print a single integer denoting the number of days since the wedding. On the third line, print $$$n$$$ numbers space-separated denoting the original labeling of the tree.
If there are multiple correct outputs, print any. This means: you are allowed to output any pair (DFS order, number of days), such that we get the current configuration from the DFS order you provided in exactly the number of days you provided.
7 4 5 2 1 7 6 3 1 5 7 6 1 2 2 7 3 4 1 3
YES 5 1 4 2 3 7 6 5
7 7 6 5 3 1 4 2 4 3 2 5 3 7 1 4 7 2 2 6
NO
The following animation showcases the first sample test case. The white label inside the node represents the index of the node $$$i$$$, while the boxed orange label represents the value $$$a_i$$$.
Touko's favorite sequence of numbers is a permutation $$$a_1, a_2, \dots, a_n$$$ of $$$1, 2, \dots, n$$$, and she wants some collection of permutations that are similar to her favorite permutation.
She has a collection of $$$q$$$ intervals of the form $$$[l_i, r_i]$$$ with $$$1 \le l_i \le r_i \le n$$$. To create permutations that are similar to her favorite permutation, she coined the following definition:
Yuu wants to figure out all $$$k$$$-similar permutations for Touko, but it turns out this is a very hard task; instead, Yuu will encode the set of all $$$k$$$-similar permutations with directed acylic graphs (DAG). Yuu also coined the following definitions for herself:
Since Yuu is free today, she wants to figure out the minimum number of edges among all $$$k$$$-encodings for each $$$k$$$ from $$$1$$$ to $$$q$$$.
The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 \le n \le 25\,000$$$, $$$1 \le q \le 100\,000$$$).
The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ which form a permutation of $$$1, 2, \dots, n$$$.
The $$$i$$$-th of the following $$$q$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$. ($$$1 \le l_i \le r_i \le n$$$).
Print $$$q$$$ lines. The $$$k$$$-th of them should contain a single integer — The minimum number of edges among all $$$k$$$-encodings.
4 3 2 4 1 3 1 3 2 4 1 4
2 4 3
8 4 3 7 4 8 1 5 2 6 3 6 1 6 3 8 1 8
3 5 9 7
10 10 10 5 1 2 7 3 9 4 6 8 2 2 4 5 6 8 4 10 4 4 2 7 2 2 7 8 3 7 2 10
0 1 3 6 6 9 9 9 9 8
For the first test case: