apaz/codeforces-genesys huggingface dataset:
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.

Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.

Put your final solution within a single code block:
```cpp
<your code here>
```

Execution time limit: 2.0 seconds
Memory limit: 256.0 MB

# Problem
You are given a string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin characters. Find the length of the shortest string $$$k$$$ such that several (possibly one) copies of $$$k$$$ can be concatenated together to form a string with the same length as $$$s$$$ and, at most, one different character.

More formally, find the length of the shortest string $$$k$$$ such that $$$c = \underbrace{k + \cdots + k}_{x\rm\ \text{times}}$$$ for some positive integer $$$x$$$, strings $$$s$$$ and $$$c$$$ has the same length and $$$c_i \neq s_i$$$ for at most one $$$i$$$ (i.e. there exist $$$0$$$ or $$$1$$$ such positions).

## Input Format
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^3$$$) — the number of test cases.

The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 2\cdot10^5$$$) — the length of string $$$s$$$.

The second line of each test case contains the string $$$s$$$, consisting of lowercase Latin characters.

The sum of $$$n$$$ over all test cases does not exceed $$$2\cdot10^5$$$.

## Output Format
For each test case, print the length of the shortest string $$$k$$$ satisfying the constraints in the statement.

## Examples
```input
5
4
abaa
4
abba
13
slavicgslavic
8
hshahaha
20
stormflamestornflame
```
```output
1
4
13
2
10
```

## Note
In the first test case, you can select $$$k = \texttt{a}$$$ and $$$k+k+k+k = \texttt{aaaa}$$$, which only differs from $$$s$$$ in the second position.

In the second test case, you cannot select $$$k$$$ of length one or two. We can have $$$k = \texttt{abba}$$$, which is equal to $$$s$$$.

Now solve the problem and return the code.
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.

Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.

Put your final solution within a single code block:
```cpp
<your code here>
```

Execution time limit: 2.0 seconds
Memory limit: 256.0 MB

# Problem
Find the minimum height of a rooted tree$$$^{\dagger}$$$ with $$$a+b+c$$$ vertices that satisfies the following conditions:

- $$$a$$$ vertices have exactly $$$2$$$ children,
- $$$b$$$ vertices have exactly $$$1$$$ child, and
- $$$c$$$ vertices have exactly $$$0$$$ children.

The tree above is rooted at the top vertex, and each vertex is labeled with the number of children it has. Here $$$a=2$$$, $$$b=1$$$, $$$c=3$$$, and the height is $$$2$$$.

$$$^{\dagger}$$$ A rooted tree is a connected graph without cycles, with a special vertex called the root. In a rooted tree, among any two vertices connected by an edge, one vertex is a parent (the one closer to the root), and the other one is a child.

The distance between two vertices in a tree is the number of edges in the shortest path between them. The height of a rooted tree is the maximum distance from a vertex to the root.

## Input Format
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases.

The only line of each test case contains three integers $$$a$$$, $$$b$$$, and $$$c$$$ ($$$0 \leq a, b, c \leq 10^5$$$; $$$1 \leq a + b + c$$$).

The sum of $$$a + b + c$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$.

## Output Format
For each test case, if no such tree exists, output $$$-1$$$. Otherwise, output one integer — the minimum height of a tree satisfying the conditions in the statement.

## Examples
```input
10
2 1 3
0 0 1
0 1 1
1 0 2
1 1 3
3 1 4
8 17 9
24 36 48
1 0 0
0 3 1
```
```output
2
0
1
1
-1
3
6
-1
-1
3
```

## Note
The first test case is pictured in the statement. It can be proven that you can't get a height smaller than $$$2$$$.

In the second test case, you can form a tree with a single vertex and no edges. It has height $$$0$$$, which is clearly optimal.

In the third test case, you can form a tree with two vertices joined by a single edge. It has height $$$1$$$, which is clearly optimal.

Now solve the problem and return the code.
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.

Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.

Put your final solution within a single code block:
```cpp
<your code here>
```

Execution time limit: 3.0 seconds
Memory limit: 256.0 MB

# Problem
Vladislav has a playlist consisting of $$$n$$$ songs, numbered from $$$1$$$ to $$$n$$$. Song $$$i$$$ has genre $$$g_i$$$ and writer $$$w_i$$$. He wants to make a playlist in such a way that every pair of adjacent songs either have the same writer or are from the same genre (or both). He calls such a playlist exciting. Both $$$g_i$$$ and $$$w_i$$$ are strings of length no more than $$$10^4$$$.

It might not always be possible to make an exciting playlist using all the songs, so the shuffling process occurs in two steps. First, some amount (possibly zero) of the songs are removed, and then the remaining songs in the playlist are rearranged to make it exciting.

Since Vladislav doesn't like when songs get removed from his playlist, he wants the making playlist to perform as few removals as possible. Help him find the minimum number of removals that need to be performed in order to be able to rearrange the rest of the songs to make the playlist exciting.

## Input Format
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 16$$$) — the number of songs in the original playlist.

Then $$$n$$$ lines follow, the $$$i$$$-th of which contains two strings of lowercase letters $$$g_i$$$ and $$$w_i$$$ ($$$1 \leq |g_i|, |w_i| \leq 10^4$$$) — the genre and the writer of the $$$i$$$-th song. Where $$$|g_i|$$$ and $$$|w_i|$$$ are lengths of the strings.

The sum of $$$2^n$$$ over all test cases does not exceed $$$2^{16}$$$.

The sum of $$$|g_i| + |w_i|$$$ over all test cases does not exceed $$$4 \cdot 10^5$$$.

## Output Format
For each test case, output a single integer — the minimum number of removals necessary so that the resulting playlist can be made exciting.

## Examples
```input
4
1
pop taylorswift
4
electronic themotans
electronic carlasdreams
pop themotans
pop irinarimes
7
rap eminem
rap drdre
rap kanyewest
pop taylorswift
indierock arcticmonkeys
indierock arcticmonkeys
punkrock theoffspring
4
a b
c d
e f
g h
```
```output
0
0
4
3
```

## Note
In the first test case, the playlist is already exciting.

In the second test case, if you have the songs in the order $$$4, 3, 1, 2$$$, it is exciting, so you don't need to remove any songs.

In the third test case, you can remove songs $$$4, 5, 6, 7$$$. Then the playlist with songs in the order $$$1, 2, 3$$$ is exciting.

Now solve the problem and return the code.
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.

Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.

Put your final solution within a single code block:
```cpp
<your code here>
```

Execution time limit: 1.0 seconds
Memory limit: 256.0 MB

# Problem
Mocha likes arrays, so before her departure, 378QAQ gave her an array $$$a$$$ consisting of $$$n$$$ positive integers as a gift.

Mocha thinks that $$$a$$$ is beautiful if there exist two numbers $$$i$$$ and $$$j$$$ ($$$1\leq i,j\leq n$$$, $$$i\neq j$$$) such that for all $$$k$$$ ($$$1 \leq k \leq n$$$), $$$a_k$$$ is divisible$$$^\dagger$$$ by either $$$a_i$$$ or $$$a_j$$$.

Determine whether $$$a$$$ is beautiful.

$$$^\dagger$$$ $$$x$$$ is divisible by $$$y$$$ if there exists an integer $$$z$$$ such that $$$x = y \cdot z$$$.

## Input Format
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1\leq t\leq 500$$$). The description of the test cases follows.

The first line of each test case contains a single integer $$$n$$$ ($$$3\leq n\leq 10^5$$$) — the length of the array $$$a$$$.

The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1\leq a_i \leq 10^9$$$) — the elements of the array $$$a$$$.

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

## Output Format
For each test case, output "Yes" if array $$$a$$$ is beautiful, and output "No" otherwise.

You can output "Yes" and "No" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response).

## Examples
```input
4
3
7 3 8
5
7 1 9 3 5
5
4 12 2 6 3
5
7 49 9 3 1000000000
```
```output
No
Yes
Yes
No
```

## Note
In the first test case, any two numbers in the array are coprime, so the answer is "No".

In the second test case, we can pick $$$i=2$$$ and $$$j=1$$$. Since every number in the array is divisible by $$$a_i = 1$$$, the answer is "Yes".

In the third test case, we can pick $$$i=3$$$ and $$$j=5$$$. $$$2$$$ and $$$4$$$ is divisible by $$$a_i = 2$$$ while $$$3$$$, $$$6$$$ and $$$12$$$ is divisible by $$$a_j = 3$$$, so the answer is "Yes".

Now solve the problem and return the code.
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.

Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.

Put your final solution within a single code block:
```cpp
<your code here>
```

Execution time limit: 2.0 seconds
Memory limit: 256.0 MB

# Problem
378QAQ has a tree with $$$n$$$ vertices. Initially, all vertices are white.

There are two chess pieces called $$$P_A$$$ and $$$P_B$$$ on the tree. $$$P_A$$$ and $$$P_B$$$ are initially located on vertices $$$a$$$ and $$$b$$$ respectively. In one step, 378QAQ will do the following in order:

1. Move $$$P_A$$$ to a neighboring vertex. If the target vertex is white, this vertex will be painted red.
2. Move $$$P_B$$$ to a neighboring vertex. If the target vertex is colored in red, this vertex will be painted blue.

Initially, the vertex $$$a$$$ is painted red. If $$$a=b$$$, the vertex $$$a$$$ is painted blue instead. Note that both the chess pieces must be moved in each step. Two pieces can be on the same vertex at any given time.

378QAQ wants to know the minimum number of steps to paint all vertices blue.

## Input Format
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1\leq t\leq 10^4$$$). The description of the test cases follows.

The first line of each test case contains one integer $$$n$$$ ($$$1\leq n\leq 2\cdot 10^5$$$).

The second line of each test case contains two integers $$$a$$$ and $$$b$$$ ($$$1\leq a,b\leq n$$$).

Then $$$n - 1$$$ lines follow, each line contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \le x_i,y_i \le n$$$), indicating an edge between vertices $$$x_i$$$ and $$$y_i$$$. It is guaranteed that these edges form a tree.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.

## Output Format
For each test case, output the minimum number of steps to paint all vertices blue.

## Examples
```input
3
2
1 2
1 2
5
1 2
1 2
1 3
1 4
1 5
8
5 4
7 1
1 5
1 8
8 3
7 2
8 6
3 4
```
```output
2
8
13
```

## Note
In the first test case, 378QAQ can paint all vertices blue in the following order:

- Initially, $$$P_A$$$ is located on the vertex $$$1$$$, and $$$P_B$$$ is located on the vertex $$$2$$$. The vertex $$$1$$$ is painted red and the vertex $$$2$$$ is white.
- 378QAQ moves $$$P_A$$$ to the vertex $$$2$$$ and paints it red. Then 378QAQ moves $$$P_B$$$ to the vertex $$$1$$$ and paints it blue.
- 378QAQ moves $$$P_A$$$ to the vertex $$$1$$$. Then 378QAQ moves $$$P_B$$$ to the vertex $$$2$$$ and paints it blue.

Now solve the problem and return the code.
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.

Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.

Put your final solution within a single code block:
```cpp
<your code here>
```

Execution time limit: 2.0 seconds
Memory limit: 256.0 MB

# Problem
You are given a tree of $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$. Initially, all vertices are colored white or black.

You are asked to perform $$$q$$$ queries:

- "u" — toggle the color of vertex $$$u$$$ (if it was white, change it to black and vice versa).

After each query, you should answer whether all the black vertices form a chain. That is, there exist two black vertices such that the simple path between them passes through all the black vertices and only the black vertices. Specifically, if there is only one black vertex, they form a chain. If there are no black vertices, they do not form a chain.

## Input Format
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1\leq t\leq 10^4$$$). The description of the test cases follows.

The first line of each test case contains two integers $$$n$$$ and $$$q$$$ ($$$1\leq n,q\leq 2\cdot 10^5$$$).

The second line of each test case contains $$$n$$$ integers $$$c_1,c_2,\ldots,c_n$$$ ($$$c_i \in \{ \mathtt{0}, \mathtt{1} \}$$$) — the initial color of the vertices. $$$c_i$$$ denotes the color of vertex $$$i$$$ where $$$\mathtt{0}$$$ denotes the color white, and $$$\mathtt{1}$$$ denotes the color black.

Then $$$n - 1$$$ lines follow, each line contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \le x_i,y_i \le n$$$), indicating an edge between vertices $$$x_i$$$ and $$$y_i$$$. It is guaranteed that these edges form a tree.

The following $$$q$$$ lines each contain an integer $$$u_i$$$ ($$$1 \le u_i \le n$$$), indicating the color of vertex $$$u_i$$$ needs to be toggled.

It is guaranteed that the sum of $$$n$$$ and $$$q$$$ over all test cases respectively does not exceed $$$2\cdot 10^5$$$.

## Output Format
For each query, output "Yes" if the black vertices form a chain, and output "No" otherwise.

You can output "Yes" and "No" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response).

## Examples
```input
2
2 1
1 0
1 2
1
5 4
1 0 0 0 0
1 2
1 3
1 5
3 4
4
3
2
5
```
```output
No
No
Yes
Yes
No
```
-----
```input
4
5 3
1 1 1 1 1
3 5
2 5
3 4
1 5
1
1
1
4 4
0 0 0 0
1 2
2 3
1 4
1
2
3
2
1 1
1
1
1 1
0
1
```
```output
Yes
No
Yes
Yes
Yes
Yes
No
No
Yes
```

## Note
In the second test case, the color of the vertices are as follows:

The initial tree:

The first query toggles the color of vertex $$$4$$$:

The second query toggles the color of vertex $$$3$$$:

The third query toggles the color of vertex $$$2$$$:

The fourth query toggles the color of vertex $$$5$$$:

Now solve the problem and return the code.
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.

Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.

Put your final solution within a single code block:
```cpp
<your code here>
```

Execution time limit: 2.0 seconds
Memory limit: 512.0 MB

# Problem
Define the binary encoding of a finite set of natural numbers $$$T \subseteq \{0,1,2,\ldots\}$$$ as $$$f(T) = \sum\limits_{i \in T} 2^i$$$. For example, $$$f(\{0,2\}) = 2^0 + 2^2 = 5$$$ and $$$f(\{\}) = 0$$$. Notice that $$$f$$$ is a bijection from all such sets to all non-negative integers. As such, $$$f^{-1}$$$ is also defined.

You are given an integer $$$n$$$ along with $$$2^n-1$$$ sets $$$V_1,V_2,\ldots,V_{2^n-1}$$$.

Find all sets $$$S$$$ that satisfy the following constraint:

- $$$S \subseteq \{0,1,\ldots,n-1\}$$$. Note that $$$S$$$ can be empty.
- For all non-empty subsets $$$T \subseteq \{0,1,\ldots,n-1\}$$$, $$$|S \cap T| \in V_{f(T)}$$$.

Due to the large input and output, both input and output will be given in terms of binary encodings of the sets.

## Input Format
The first line of input contains a single integer $$$n$$$ ($$$1 \leq n \leq 20$$$).

The second line of input contains $$$2^n-1$$$ integers $$$v_1,v_2,\ldots,v_{2^n-1}$$$ ($$$0 \leq v_i < 2^{n+1}$$$) — the sets $$$V_i$$$ given in their binary encoding where $$$V_i = f^{-1}(v_i)$$$.

## Output Format
The first line of output should contain an integer $$$k$$$ indicating the number of possible $$$S$$$.

In the following $$$k$$$ lines, you should output $$$f(S)$$$ for all possible $$$S$$$ in increasing order.

## Examples
```input
3
15 15 15 15 15 15 12
```
```output
4
3
5
6
7
```
-----
```input
5
63 63 63 63 6 63 63 63 63 63 63 5 63 63 63 63 63 63 8 63 63 63 63 2 63 63 63 63 63 63 63
```
```output
1
19
```

## Note
In the first test case, one possible $$$S$$$ is $$$f^{-1}(3) = \{0,1\}$$$. All the non-empty subsets $$$T \subseteq \{0,1,2\}$$$ and the corresponding $$$|S \cap T|$$$, $$$f(T)$$$ and $$$V_f(T)$$$ are as follows:

$$$T$$$$$$|S\cap T|$$$$$$f(T)$$$$$$V_{f(T)}$$$$$$\{0\}$$$$$$1$$$$$$1$$$$$$\{0,1,2,3\}$$$$$$\{1\}$$$$$$1$$$$$$2$$$$$$\{0,1,2,3\}$$$$$$\{2\}$$$$$$0$$$$$$4$$$$$$\{0,1,2,3\}$$$$$$\{0,1\}$$$$$$2$$$$$$3$$$$$$\{0,1,2,3\}$$$$$$\{0,2\}$$$$$$1$$$$$$5$$$$$$\{0,1,2,3\}$$$$$$\{1,2\}$$$$$$1$$$$$$6$$$$$$\{0,1,2,3\}$$$$$$\{0,1,2\}$$$$$$2$$$$$$7$$$$$$\{2,3\}$$$

Now solve the problem and return the code.
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.

Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.

Put your final solution within a single code block:
```cpp
<your code here>
```

Execution time limit: 12.0 seconds
Memory limit: 512.0 MB

# Problem
One day, Zimpha casually came up with a problem. As a member of "Zimpha fan club", you decided to solve that problem.

You are given two strings $$$s$$$ and $$$t$$$ of length $$$n$$$ and $$$m$$$, respectively. Both strings only consist of lowercase English letters, - and *.

You need to replace all occurrences of * and -, observing the following rules:

- For each -, you must replace it with any lowercase English letter.
- For each *, you must replace it with a string of any (possibly, zero) length which only consists of lowercase English letters.

Note that you can replace two different instances of - with different characters. You can also replace each two different instances of * with different strings.

Suppose $$$s$$$ and $$$t$$$ have been transformed into $$$s'$$$ and $$$t'$$$. Now you're wondering if there's a replacement that makes $$$s'=t'$$$.

## Input Format
The first line of input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 2 \cdot 10^6$$$) — the length of the strings $$$s$$$ and $$$t$$$, respectively.

The second line contains the string $$$s$$$ of length $$$n$$$. It is guaranteed that $$$s$$$ only consists of lowercase English letters, - and *.

The third line contains the string $$$t$$$ of length $$$m$$$. It is guaranteed that $$$t$$$ only consists of lowercase English letters, - and *.

## Output Format
For each test case, output "Yes" if there is a replacement that makes $$$s'=t'$$$, and output "No" otherwise.

You can output "Yes" and "No" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response).

## Examples
```input
10 10
justmonika
j-stsayori
```
```output
No
```
-----
```input
7 8
ttk-wxx
*tt-l-xx
```
```output
Yes
```
-----
```input
13 11
asoulwangziji
-soulg*z-y-
```
```output
No
```
-----
```input
7 3
abc*cba
a*c
```
```output
No
```
-----
```input
20 18
bulijiojio-dibuliduo
*li*ji-*ox*i*-du*-
```
```output
Yes
```

## Note
In the second test case, we can transform both strings into ttklwxx. In $$$s$$$, - will be replaced with l. In $$$t$$$, * will be replaced by the empty string with the first and second - will be replaced with k and w respectively.

In the fifth test case, we can transform both strings into bulijiojioxdibuliduo.

Now solve the problem and return the code.