You are given two strings $$$X$$$ and $$$Y$$$ of length $$$n$$$ each, consisting of characters A and B. The following two kinds of operations can be applied on the string $$$X$$$ any (including $$$0$$$) number of times:
For example, you can convert ABAA to ABBA, and then ABBA to AABA.
First, find whether it is possible to convert $$$X$$$ into $$$Y$$$ using any (including 0) number of allowed operations. If it is so possible, find the minimum number of operations required for the conversion.
Then, you need to process updates of the following types:
After each update, find if it is possible to convert $$$X$$$ into $$$Y$$$. Note that, you only have to check if it is possible to convert $$$X$$$ into $$$Y$$$; you don't have to actually convert it. Also, you don't have to print the minimum number of operations required after updating, in case the conversion is possible.
The first line contains $$$t (1 \le t \le 100)$$$, the number of test cases. Then, the testcases follow:
For each testcase:
21 0AB4 2ABAAAABAY 3X 2
NO YES 2 NO YES
In the first testcase, $$$X=\texttt{A}$$$ and $$$Y=\texttt{B}$$$. Since the conversion is not possible, we print "NO" on the first line. Then, there are no updates to process ($$$Q = 0$$$), so we move to the next testcase
In the second testcase:
| Number of updates | $$$X$$$ | $$$Y$$$ | Possible to convert? | Minimum Number of moves required |
| 0 | ABAA | AABA | YES | 2 |
| 1 | ABAA | AAAA | NO | - |
| 2 | AAAA | AAAA | YES | 0 (not to be printed) |
ABAA $$$\rightarrow$$$ ABBA $$$\rightarrow$$$ AABA
You are given a grid of size $$$N \times N$$$. Rows are numbered from $$$0$$$ to $$$N-1$$$ from top to bottom and columns are numbered from $$$0$$$ to $$$N-1$$$ from left to right. $$$(i, j)$$$ represents the cell on row $$$i$$$ and column $$$j$$$. $$$(0, 0)$$$ is therefore the cell at the top most left position.
Each cell in the grid is either empty or contains a block. There are $$$B$$$ ($$$1 \le B \le 20$$$) blocks in the grid. The blocks are placed in such a way that no two blocks share a side or a corner. Formally, no two cells $$$(a, b)$$$ and $$$(c, d)$$$ have blocks such that $$$|a-b| \leq 1 \And |c-d| \leq 1$$$. It is also guaranteed that there is no block on the cell $$$(0, 0)$$$. Your task is to find the positions of these blocks given the dimension of the grid.
You can do a query $$$420$$$ times to place mirrors on the grid and send a light ray starting from $$$(0, 0)$$$ in some direction. Note that mirrors placed for a particular query are removed before the next query. Mirrors can be placed on any cell, including $$$(0, 0)$$$ and cells containing blocks.
The query returns the number of mirrors the ray of light gets reflected from before hitting a block or going outside the grid. If a mirror is placed on the same cell as a block, light hits the block first and the mirror isn't counted in the return value. If a light ray hits the non reflecting side of a mirror it passes through to the next cell and nothing gets added. If the light starts going in a cycle $$$-1$$$ is returned.
A mirror is represented by a tuple of size $$$3$$$. The number of mirrors in a query can be no more than $$$4 \cdot N$$$ and no two mirrors can be placed in the same cell. The first $$$2$$$ integers in the tuple represent the row and column at which the mirror is placed respectively. The $$$3$$$rd integer represents the type of mirror. The mirrors are tilted such that light reflects perpendicularly on them and can be of $$$4$$$ types:
You can choose the direction light travels in when starting from the cell $$$(0,0)$$$. These can be Down or Right represented by $$$0$$$ and $$$1$$$ in code respectively.
Please view the interaction format and sample explanation for more clarity.
The input consists of a single integer $$$N(2 \le N \le 1000)$$$, representing the dimensions of the grid.
Your program can ask queries using the format specified. It should first print $$$M$$$, the number of mirrors placed and $$$D$$$, the direction of the light. Then $$$M$$$ lines should follow, containing three integers each — the row $$$r_i$$$, column $$$c_i$$$ and type $$$t_i$$$ of mirror placed.
? M D
$$$r_1$$$ $$$c_1$$$ $$$t_1$$$
$$$r_2$$$ $$$c_2$$$ $$$t_2$$$
$$$\ldots$$$
$$$r_M$$$ $$$c_M$$$ $$$t_M$$$
When you want to give an answer you should first print the number of blocks $$$B$$$ determined, followed by the position of each block.
! B
$$$r_1$$$ $$$c_1$$$
$$$r_2$$$ $$$c_2$$$
$$$\ldots$$$
$$$r_B$$$ $$$c_B$$$
Note: In case of a wrong answer or invalid query, $$$-2$$$ will be returned by the judge. In such cases you must exit the program immediately.
4 1 0
? 3 1 0 1 1 0 2 3 1 2 0 ? 3 0 0 1 1 0 2 3 1 2 0 ! 2 1 2 3 0
The path of light for the first query can be seen in the image provided below. For the second query, light doesn't hit any mirrors and directly goes to the block at $$$(3, 0)$$$. 
Consider an undirected tree. A subset of nodes of this tree is called good if there exists a node $$$r$$$ such that if the tree is rooted at $$$r$$$, no node in the subset is an ancestor of another node in the subset.
You are given a connected undirected graph of $$$N$$$ nodes numbered from $$$1$$$ to $$$N$$$ and $$$M$$$ edges numbered from $$$1$$$ to $$$M$$$. Each edge $$$j$$$ connects the nodes $$$U_j$$$ and $$$V_j$$$. The graph has no self-loops or duplicate edges. A subset of nodes of this graph is called ideal if there is some spanning tree of the graph for which this subset of nodes is good.
You are given a subset of nodes of this graph which has size at least 2. Find whether or not it is ideal. If it is ideal, find a valid spanning tree and root as specified above.
The first line contains two integers, $$$N$$$ and $$$M$$$, respectively the number of nodes and number of edges.
The next $$$M$$$ lines describe the edges. The $$$j$$$-th line contains two numbers $$$U_j$$$ and $$$V_j$$$, the endpoints of the $$$j$$$-th edge.
The next line contains a single integer $$$S$$$, the size of the given subset of nodes.
The next line contains $$$S$$$ integers, $$$T_1, ... T_S$$$, the given subset of nodes.
Constraints
If the given subset is not ideal, you should print a single line which is NO.
If the given subset is ideal, on the first line, you should print YES.
On the second line, you should print your chosen root.
Then, on the next $$$N-1$$$ lines you should describe your chosen spanning tree. Each line should contain the two endpoints of a single chosen edge. The two endpoints may be in any order. The edges themselves may be in any order.
6 71 22 31 45 65 16 34 334 2 5
YES 1 1 2 1 4 1 5 2 3 5 6
6 71 22 31 45 65 16 34 333 6 5
NO
Samples
In Sample 1, the given subset is ideal. If the spanning tree in the sample output is chosen and rooted at 1, no node in the subset is an ancestor of another node in the subset.
In Sample 2, the given subset is not ideal. It is impossible to choose a valid spanning tree and root.
There are $$$N$$$ bricks numbered from $$$1$$$ to $$$N$$$ and $$$M$$$ colors numbered from $$$1$$$ to $$$M$$$. Each brick $$$i$$$ has a size $$$A_i$$$ and color $$$B_i$$$. It is guaranteed that there is at least one brick of each color.
A tower made of some set of bricks is called $$$k$$$-beautiful if the absolute difference in sizes between every two adjacent bricks is at most $$$k$$$.
Your task is to answer $$$Q$$$ queries. The $$$j$$$-th query is: can at least one $$$K_j$$$-beautiful tower be made using all the bricks of the colors $$$X_j$$$ and $$$Y_j$$$ and no bricks of any other colors? It is guaranteed that $$$X_j$$$ and $$$Y_j$$$ are distinct.
The first line contains two integers, $$$N$$$ and $$$M$$$.
The next $$$N$$$ lines describe the bricks. The $$$i$$$-th of these lines contains two integers $$$A_i$$$ and $$$B_i$$$.
The next line contains a single integer, $$$Q$$$.
The next $$$Q$$$ lines describe the queries. The $$$j$$$-th of these lines contain three integers, $$$K_j$$$, $$$X_j$$$ and $$$Y_j$$$.
Constraints
For each query, you should print either YES or NO on a separate line.
8 45 110 22 220 333 46 117 242 1513 3 47 2 325 1 320 1 220 2 4
YES NO YES NO YES
5 25 15 110 215 120 215 1 2
YES
10 435 492 397 319 120 184 411 126 260 329 3108 2 410 2 434 2 343 2 347 1 224 2 330 3 445 1 346 1 414 2 3
NO NO YES YES YES NO YES YES NO NO
You are given a tree with $$$n$$$ vertices, numbered $$$1, 2, \ldots, n$$$. For two vertices $$$x$$$ and $$$y$$$, let $$$d(x, y)$$$ denote the number of edges on the unique simple path between $$$x$$$ and $$$y$$$ in the tree.
For distinct nodes $$$a$$$ and $$$b$$$, we say that node $$$a$$$ dominates node $$$b$$$ if the number of vertices $$$x$$$ such that $$$d(a, x) \lt d(b, x)$$$ is strictly more than the number of vertices $$$y$$$ such that $$$d(a, y) \gt d(b, y)$$$.
You want to order the $$$n$$$ vertices such that for any $$$a, b$$$, if $$$a$$$ dominates $$$b$$$, then $$$a$$$ appears before $$$b$$$ in the order. Find such an ordering, or claim that it doesn't exist.
The first line contains a single integer $$$t(1 \le t \le 3 \cdot 10^5)$$$, the number of test cases. Then the testcases follow:
The sum of $$$n$$$ over all testcases does not exceed $$$3 \cdot 10^5$$$.
For each test case:
261 22 32 43 53 621 2
YES 2 3 1 4 5 6 YES 1 2
In the first testcase, the ordered domination pairs are the following:
The ordering $$$3, 2, 4, 5, 1, 6$$$ is also valid, among others.
You are given an initial configuration of $$$N$$$ cells (from left to right), where each cell can have one of the 4 things: empty, tombstone, left arrow or right arrow.
Two players play a game on it, starting from Player A followed by Player B turn-by-turn.
Player A's objective is to maximize the number of arrows (which were not turned to tombstones) when the game ends and Player B's objective is to minimize the number of arrows when the game ends.
Find the number of arrows at the end of the game if both players play optimally.
For each test case, output the number of arrows at the end of game under optimal play.
4 4XXXXLTXRRTXLLRTR
2 3 2 3
4 4XXXXLTXRRTXLLRTR
2 3 2 3
There is a directed road network with $$$N$$$ nodes and $$$M$$$ weighted edges, where you are at node $$$1$$$. Your car has battery capacity $$$C$$$, and also starts with $$$C$$$ battery.
Each edge in the graph either increases or decreases your battery when you traverse it. When you have $$$x$$$ battery, you can traverse an edge with weight $$$w$$$ if and only if $$$x \geq w$$$. After traversing such an edge, you will have $$$\min(C, x - w)$$$ battery. Some edges may even be negative, thus increasing your battery.
You want to know the following: which nodes can you reach from your starting node? For each node you can reach, what is the maximum amount of battery you can have when you reach it?
The first line contains $$$T(1 \le T \le 50)$$$, the number of testcases. Then the test cases follow, each with the following format:
For each testcase, print $$$N$$$ space-separated integers on a new line. The $$$i^{\text{th}}$$$ of these integers should denote the maximum possible battery you can have when you reach node $$$i$$$, or $$$-1$$$ if it is impossible.
26 6 111 2 31 3 22 3 53 4 44 5 74 6 16 6 111 3 21 2 32 3 53 4 -94 5 124 6 10
11 8 9 5 -1 4 11 8 9 11 -1 1
There is a hidden matrix $$$M$$$ with $$$n$$$ rows numbered $$$1, 2, \ldots n$$$ and $$$m$$$ columns numbered $$$1, 2, \ldots m$$$. Let $$$(i, j)$$$ denote the cell with row number $$$i$$$ and column number $$$j$$$, and let $$$M[i][j]$$$ denote the matrix value in the cell $$$(i, j)$$$. The matrix is guaranteed to satisfy the following properties:
In other words, every row and every column of the matrix is non-increasing.
To know the value of the cell $$$(i, j)$$$ of the matrix, you can make a query of the form (without quotes):
"? i j"
where $$$1 \leq i \leq n$$$ and $$$1 \leq j \leq m$$$. In response, you get the value of $$$M[i][j]$$$.
You are allowed to make at most $$$50$$$ queries, and your task is to approximate the sum of the matrix to within a factor of $$$2$$$. In particular, if the actual sum is $$$X$$$, you need to output an integer $$$Y$$$ such that $$$\lceil X/2 \rceil \leq Y \leq 2\cdot X$$$.
Note that the grader is adaptive, which means that the matrix is not fixed before the interaction begins. However, the judge will never contradict the replies given in the past. This means that, it is guaranteed at each point of time that there exists a valid matrix with the aforementioned properties, whose values are consistent with the responses of the judge to the past queries.
First, read an integer $$$t (1 \leq t \leq 200)$$$, denoting the number of test-cases. Each test case should be considered independently. For each test case,
Please note that, there is no additional constraint on sum of $$$n$$$, sum of $$$m$$$, or sum of $$$n \cdot m$$$ over the testcases.
2 2 3 10 3 4 1 1 1
? 1 1 ? 2 2 ? 1 3 ! 42 ? 1 1 ! 1
In the first test case, the matrix is:
| 10 | 5 | 4 |
| 8 | 3 | 1 |
The total sum is $$$10+5+4+8+3+1 = 31$$$, and the output sum is $$$42$$$, which is a valid $$$2-$$$approximation, since $$$16 \leq 42 \leq 62$$$.
In the second test-case, the matrix is a single cell with value $$$1$$$, and hence the output $$$1$$$ is considered correct.
You have a matrix $$$A$$$ with $$$N$$$ rows and $$$M$$$ columns. The rows are numbered $$$1, 2, \ldots N$$$, and the columns are numbered $$$1, 2, \ldots M$$$. For each $$$1 \le i \le N$$$ and $$$1 \le j \le M$$$, let $$$(i, j)$$$ denote the cell with row number $$$i$$$ and column number $$$j$$$.
Initially, all of its $$$N \times M$$$ cells are colored white. You want to do the following operation exactly $$$K$$$ times:
You have a target $$$N \times M$$$ matrix $$$C$$$ consisting of $$$0$$$ and $$$1$$$. For each cell $$$(i, j)$$$, if $$$C_{i,j} = 1$$$, you want the cell to be colored black in the matrix $$$A$$$ and otherwise you want it to remain white.
Find if there exist $$$K$$$ operations that color the matrix as required. Note that, it is allowed to do the same operation multiple times.
The first line contains $$$N, M (1 \le N, M \le 1000)$$$ and $$$K (1 \le K \le 4)$$$, the number of rows, the number of columns, and the required number of operations, respectively.
For each $$$1 \le i \le N$$$, the $$$i^{\text{th}}$$$ of the next $$$N$$$ lines contains a binary string of length $$$M$$$, whose $$$j^{\text{th}}$$$ character is $$$1$$$ if $$$C_{i,j}=1$$$ and $$$0$$$ otherwise.
Print "Yes" (without quotes) if it is possible to convert the matrix into the required state after K operations and "No" (without quotes) otherwise.
Note that the output is case insensitive. So, you can also print "yEs" instead of "Yes".
5 6 2000000001100001111000111000000
Yes
5 6 1000000001100001111000111000000
No
In the first sample:
In the first operation, choose the submatrix $$$[2, 3] \times [3, 4]$$$

In the second operation, choose the submatrix $$$[3, 4] \times [4, 6]$$$

In the second sample, one can not obtain the required coloring in $$$1$$$ operation.
For a rooted tree $$$T$$$, define the depth of a node to be the number of edges on the path from the root to it. Also, define $$$f(T)$$$ as the sum of depths of all the nodes in the tree.
You are given a rooted tree $$$T$$$ with $$$N$$$ nodes numbered $$$1, 2, \ldots, N$$$, rooted at node $$$1$$$. For $$$i \ge 2$$$, the parent of node $$$i$$$ is $$$p_i$$$. It is given that $$$p_i \lt i$$$ for all $$$i \ge 2$$$.
You want to remove all nodes from the tree one by one until only the root remains. In a single step, you can remove any leaf node (a non-root node which has exactly one adjacent node). Note that a node which is not a leaf node initially can become a leaf node later and become eligible for removal.
In this process, let $$$T_0$$$(the original tree), $$$T_1, T_2, \ldots T_{n-1}$$$(the tree with only the root remaining) be the sequence of trees formed. In other words, $$$T_i$$$ is the tree formed after $$$i$$$ nodes have been removed. What is the maximum possible value of $$$\displaystyle \sum_{i=0}^{n-1} f(T_i)$$$?
The first line contains $$$Q(1 \le Q \le 40)$$$, the number of test-cases. Then the testcases follow, each containing two lines:
The sum of $$$N$$$ over all test-cases does not exceed $$$40$$$.
For each testcase, print the maximum possible value of $$$\displaystyle \sum_{i=0}^{n-1} f(T_i) $$$ on a new line.
241 1 151 2 2 1
6 15
In the first testcase, the depth of node $$$1$$$ is $$$0$$$, and that of nodes $$$2, 3, 4$$$ are $$$1$$$. If we remove the nodes in the order $$$2, 3, 4$$$, the objective value is $$$(0 + 1 + 1 + 1) + (0 + 1 + 1) + (0 + 1) + 0 = 6$$$. This is the maximum possible value, and so is the answer.
In the second testcase, the depths of nodes $$$1, 2, 3, 4, 5$$$ are $$$0, 1, 2, 2, 1$$$ respectively. We can remove nodes in the order $$$5, 3, 4, 2$$$ to get the objective value as $$$6 + 5 + 3 + 1 + 0 = 15$$$. The sequence $$$T_0, T_1, \ldots T_4$$$ is :




