{"raw_statement":[{"iden":"statement","content":"Andi has $N$ gears (numbered from 1 to $N$) on a board. Each gear has an arrow initially pointing upward. Some pair of gears may touch each other. If we construct a graph where each gear is a node, and we connect each pair of touching gears with an edge, then the structure of this graph is a tree. For example, the following is a possible gear configuration example with $N = 4$ gears and gear $2$ touches all other gears.\n\nStandard gear rotation rule applies: Suppose a gear $u$ touches another gear $v$, and gear $u$ is rotated $alpha$ degrees clockwise, then gear $v$ will be rotated $alpha$ degrees counter-clockwise, and vice-versa.\n\nAndi wants to perform three kinds of operations:\n\nLet $delta_u$ be the arrow's degree (clockwise, modulo $360$) of gear $u$ after $Q$ operations are done. Andi wants to know the sum of $delta_u$ for all $u$. Furthermore, since rotating gears requires a lot of work, Andi also wants to know how much energy he needs for every Type 3 operation (rotating a gear). The amount of energy Andi needs to perform the Type 3 operation is defined as $angle.l$number of rotating gears$angle.r$ $times$ $angle.l$rotation done in degrees$angle.r$\n\nInput begins with a line containing an integer $N$ ($1 <= N <= 100000$) representing the number of gears. The next $N -1$ lines, each contains two integers: $u$ $v$ ($1 <= u, v <= N$; $u != v$) representing that gear $u$ and gear $v$ touches each other. It is guaranteed that the structure of the connected gears forms a tree. The next line contains an integer $Q$ ($1 <= Q <= 100000$) representing the number of operations. The next $Q$ lines, each representing an operation to be done sequentially. Each operation is given in one of the following formats: \n\nFor each Type 3 operation in the same order as input, output in a line the amount of energy needed to rotate the gears. Finally, output in a line the sum of $delta_u$ for all $u$.\n\n_Explanation for the sample input/output_\n\nThe structure of the gears for this sample is illustrated by the figure in the problem description. The following table illustrates the state of each gear after each operation.\n\nTherefore, the sum of $delta_u$ for all $u$ is $210 + 145 + 225 + 225 = 805$.\n\n"},{"iden":"input","content":"Input begins with a line containing an integer $N$ ($1 <= N <= 100000$) representing the number of gears. The next $N -1$ lines, each contains two integers: $u$ $v$ ($1 <= u, v <= N$; $u != v$) representing that gear $u$ and gear $v$ touches each other. It is guaranteed that the structure of the connected gears forms a tree. The next line contains an integer $Q$ ($1 <= Q <= 100000$) representing the number of operations. The next $Q$ lines, each representing an operation to be done sequentially. Each operation is given in one of the following formats:   Two integers: $1$ $x$ ($1 <= x <= N$). This means that this operation takes out gear $x$ from its position on the board. It is guaranteed that gear $x$ is currently on the board.  Two integers: $2$ $x$ ($1 <= x <= N$). This means that this operation places gear $x$ back to its original position on the board. It is guaranteed that gear $x$ is currently not on the board.  Three integers: $3$ $x$ $alpha$ ($1 <= x <= N$; $0 <= alpha <= 359$). This means that this operation rotates gear $x$ $alpha$ degrees clockwise. It is guaranteed that gear $x$ is currently on the board. "},{"iden":"output","content":"For each Type 3 operation in the same order as input, output in a line the amount of energy needed to rotate the gears. Finally, output in a line the sum of $delta_u$ for all $u$."},{"iden":"note","content":"_Explanation for the sample input/output_The structure of the gears for this sample is illustrated by the figure in the problem description. The following table illustrates the state of each gear after each operation.  Therefore, the sum of $delta_u$ for all $u$ is $210 + 145 + 225 + 225 = 805$."}],"translated_statement":null,"sample_group":[],"show_order":[],"formal_statement":"**Definitions**  \nLet $ T \\in \\mathbb{Z} $ be the number of test cases.  \nFor each test case $ k \\in \\{1, \\dots, T\\} $:  \n- Let $ n_k \\in \\mathbb{Z} $ denote the length of the binary strings.  \n- Let $ s_k^{(1)}, s_k^{(2)} $ be the two given binary strings of length $ n_k $.  \n- Let $ c_k^{(1)} $ be the number of '1's in $ s_k^{(1)} $, and $ c_k^{(2)} $ be the number of '1's in $ s_k^{(2)} $.  \n- Let $ z_k^{(1)} = n_k - c_k^{(1)} $, $ z_k^{(2)} = n_k - c_k^{(2)} $ be the number of '0's in each string.\n\n**Constraints**  \n1. $ 1 \\leq T \\leq 512 $  \n2. For each $ k \\in \\{1, \\dots, T\\} $:  \n   - $ 1 \\leq n_k \\leq 100 $  \n   - $ 0 \\leq c_k^{(1)}, c_k^{(2)} \\leq n_k $\n\n**Objective**  \nMaximize the Hamming distance between two rearranged binary strings of length $ n_k $, given $ c_k^{(1)} $ ones and $ z_k^{(1)} $ zeros in the first string, and $ c_k^{(2)} $ ones and $ z_k^{(2)} $ zeros in the second string.  \n\nThe maximum Hamming distance is:  \n$$\n\\max_{\\text{rearrangements}} \\text{Hamming}(s', s'') = \\min(c_k^{(1)} + z_k^{(2)},\\; z_k^{(1)} + c_k^{(2)},\\; n_k)\n$$  \nEquivalently:  \n$$\n\\min(c_k^{(1)} + (n_k - c_k^{(2)}),\\; (n_k - c_k^{(1)}) + c_k^{(2)},\\; n_k) = \\min(n_k + c_k^{(1)} - c_k^{(2)},\\; n_k + c_k^{(2)} - c_k^{(1)},\\; n_k)\n$$  \nSimplified:  \n$$\n\\min(2n_k - |c_k^{(1)} - c_k^{(2)}|,\\; n_k)\n$$  \nBut since $ 2n_k - |c_k^{(1)} - c_k^{(2)}| \\geq n_k $ always holds (because $ |c_k^{(1)} - c_k^{(2)}| \\leq n_k $), the maximum is simply:  \n$$\n\\boxed{n_k}\n$$  \nif $ c_k^{(1)} \\neq 0 $ and $ c_k^{(2)} \\neq n_k $, and vice versa — but actually, the true maximum is:\n\n$$\n\\min(c_k^{(1)}, n_k - c_k^{(2)}) + \\min(n_k - c_k^{(1)}, c_k^{(2)})\n$$\n\nWhich simplifies to:  \n$$\n\\min(c_k^{(1)}, n_k - c_k^{(2)}) + \\min(n_k - c_k^{(1)}, c_k^{(2)})\n$$\n\nBut this equals:  \n$$\n\\min(c_k^{(1)} + c_k^{(2)},\\; n_k - c_k^{(1)} + n_k - c_k^{(2)},\\; c_k^{(1)} + n_k - c_k^{(2)},\\; n_k - c_k^{(1)} + c_k^{(2)})\n$$\n\nActually, the **maximum possible Hamming distance** after rearrangement is:  \n$$\n\\min(c_k^{(1)} + c_k^{(2)},\\; 2n_k - c_k^{(1)} - c_k^{(2)},\\; n_k)\n$$\n\nWait — simpler insight: To maximize disagreement, align as many 0s of string 1 with 1s of string 2, and 1s of string 1 with 0s of string 2.\n\nSo maximum Hamming distance = number of positions where we can mismatch =  \n$$\n\\min(c_k^{(1)}, n_k - c_k^{(2)}) + \\min(n_k - c_k^{(1)}, c_k^{(2)})\n$$\n\nThis is the correct expression.\n\nLet $ a = c_k^{(1)} $, $ b = c_k^{(2)} $\n\nThen maximum Hamming distance =  \n$$\n\\min(a, n_k - b) + \\min(n_k - a, b)\n$$\n\nBut note:  \n$$\n\\min(a, n_k - b) + \\min(n_k - a, b) = \\min(a + b,\\; 2n_k - a - b,\\; n_k)\n$$\n\nActually, no — better to leave as:\n\n$$\n\\boxed{ \\min(a, n_k - b) + \\min(n_k - a, b) }\n$$\n\nBut this can be simplified to:  \n$$\n\\min(a + b,\\; 2n_k - a - b,\\; n_k)\n$$\n\nWait — let’s test:  \nIf $ a = 2, b = 2, n_k = 4 $:  \nmin(2,2) + min(2,2) = 2 + 2 = 4  \na + b = 4, 2n - a - b = 4, n = 4 → min(4,4,4)=4 ✓  \nIf $ a=1, b=1, n=3 $: min(1,2)+min(2,1)=1+1=2  \na+b=2, 2n-a-b=4, n=3 → min(2,4,3)=2 ✓  \nIf $ a=3, b=0, n=3 $: min(3,3)+min(0,0)=3+0=3  \na+b=3, 2n-a-b=3, n=3 → min=3 ✓  \nIf $ a=0, b=0 $: min(0,n)+min(n,0)=0 → correct.\n\nSo:  \n$$\n\\min(a + b,\\; 2n_k - a - b,\\; n_k) = \\min(a + b,\\; n_k)\n$$  \nsince $ 2n_k - a - b \\geq n_k $ iff $ a + b \\leq n_k $, and vice versa.\n\nActually:  \nThe maximum Hamming distance is always:  \n$$\n\\min(a + b,\\; 2n_k - a - b)\n$$  \nbut capped at $ n_k $, so:\n\n$$\n\\boxed{ \\min(a + b,\\; 2n_k - a - b,\\; n_k) }\n$$\n\nBut since $ a + b + (2n_k - a - b) = 2n_k $, the minimum of those two is always ≤ $ n_k $, so:\n\n$$\n\\boxed{ \\min(a + b,\\; 2n_k - a - b) }\n$$\n\nis sufficient.\n\nBut wait — if $ a + b > n_k $, then $ 2n_k - a - b < n_k $, so min is $ 2n_k - a - b $.  \nIf $ a + b \\leq n_k $, then min is $ a + b $.  \nSo maximum Hamming distance = $ \\min(a + b,\\; 2n_k - a - b) $\n\nYes.\n\nSo final answer per test case:  \nLet $ a = \\text{number of '1's in first string} $,  \n$ b = \\text{number of '1's in second string} $.  \nThen:  \n$$\n\\boxed{ \\min(a + b,\\; 2n - a - b) }\n$$\n\n**Objective**  \nFor each test case $ k \\in \\{1, \\dots, T\\} $, compute:  \n$$\n\\min(c_k^{(1)} + c_k^{(2)},\\; 2n_k - c_k^{(1)} - c_k^{(2)})\n$$","simple_statement":"Given two binary strings of length n, you can rearrange each string arbitrarily. What is the maximum Hamming distance (number of differing positions) you can achieve between them?","has_page_source":false}