We are given a problem: Chef is in a 2D maze with N rows and M columns. He needs to find a magical path. Definition: A path is magical if it starts at any cell (a,b) and ends at cell (c,d) such that:
-
|a-c| + |b-d| = 1. So the start and end cells are adjacent (Manhattan distance 1). This essentially means the path is a Hamiltonian path (visits all cells exactly once) where the endpoints are adjacent.
-
All cells are traversed exactly once. So it's a Hamiltonian path that covers the entire grid graph (grid of size N x M).
-
Moves allowed: up, down, left, right.
Thus, we need to determine if an N x M grid graph has a Hamiltonian path whose endpoints are adjacent (distance 1). Equivalent to: Does there exist a Hamiltonian path in the grid graph where the start and end are neighbors? Or more specifically, a Hamiltonian path where the two endpoints are adjacent (i.e., there exists an edge between them). That means the Hamiltonian path plus that extra edge would form a Hamiltonian cycle? Actually, if you have a Hamiltonian path whose endpoints are adjacent, then adding that edge gives a Hamiltonian cycle. Conversely, if there exists a Hamiltonian cycle, then you can take any edge of the cycle and remove it to get a Hamiltonian path whose endpoints are that edge's endpoints, which are adjacent. So existence of a Hamiltonian path with adjacent endpoints is equivalent to existence of a Hamiltonian cycle in the grid graph. However, careful: For a path to have endpoints adjacent, it means the two endpoints are connected by an edge in the grid. So such a path exists if and only if the grid graph has a Hamiltonian cycle. But is that always? Let's examine: If we have a Hamiltonian cycle, we can remove any edge to get a Hamiltonian path whose endpoints are the two vertices of that edge, which are adjacent (since edge exists). So yes, Hamiltonian cycle implies existence of magical path. Conversely, if we have a Hamiltonian path with adjacent endpoints, we can add the edge between them to form a cycle that visits all vertices exactly once, which is a Hamiltonian cycle. However, we must ensure that adding that edge does not cause duplication of vertices or edges? The path visits each vertex exactly once, and endpoints are distinct and adjacent; the edge between them is not used in the path (since the endpoints are adjacent but not connected by a step in the path; the path ends at one endpoint, but the edge between them is not traversed; if it were, they'd be consecutive in the path, but they are endpoints, so not traversed). Adding that edge yields a closed walk that visits all vertices exactly once and returns to start, which is a Hamiltonian cycle, provided the graph has that edge. Since the grid graph includes all edges between adjacent cells, the edge between the endpoints exists by condition |a-c|+|b-d|=1. So yes, it's a Hamiltonian cycle. So the condition is exactly equivalent to existence of a Hamiltonian cycle in the N x M grid graph.
Thus the problem reduces to: For a grid of size N rows, M columns (with vertices N*M), when does the grid graph have a Hamiltonian cycle?
We recall known results: A grid graph (rectangular grid) has a Hamiltonian cycle if and only if both N and M are even? Actually, that's for grid graphs where all vertices are present? But there are nuances: For a rectangular grid graph (N x M) where each cell is a vertex and edges between orthogonal neighbors, Hamiltonian cycles exist if and only if at least one of N or M is even? Wait, let's recall:
- A chessboard coloring: The grid is bipartite. Number of black and white vertices may differ by at most 1 for Hamiltonian path existence; for a cycle, the graph must be bipartite with equal number of vertices in each part (since a cycle of even length alternates colors). So for a Hamiltonian cycle, the two bipartite parts must have equal size. In a grid with N rows and M columns, the bipartite parity: If we color like a chessboard, the number of black and white cells are equal if NM is even, and differ by one if NM is odd. More precisely: For a rectangular board, if NM is even, then there are equal numbers of black and white squares; if NM is odd, then one color has one more than the other. Therefore, a necessary condition for a Hamiltonian cycle is that NM is even. Because a Hamiltonian cycle must be of even length (since it's bipartite, any cycle must have even length; actually in bipartite graphs, all cycles are even. So the number of vertices must be even for a Hamiltonian cycle, because the cycle uses all vertices. So NM must be even. That's necessary.
But is it also sufficient? For grid graphs, there are known results: An m x n grid graph (with m,n >= 2) has a Hamiltonian cycle if and only if at least one of m or n is even. Actually, let's test: 2x2 grid: 4 vertices, has a Hamiltonian cycle? Yes, it's a square cycle. 2x3: 6 vertices, does it have a Hamiltonian cycle? Let's draw: 2 rows, 3 columns. The grid graph is basically a 2x3 lattice. Can we find a cycle covering all vertices? For example, go around perimeter? The perimeter covers 8 edges? But there are 6 vertices; perimeter: (1,1)->(1,2)->(1,3)->(2,3)->(2,2)->(2,1)->(1,1) is a cycle? Check adjacency: (2,1) to (1,1) is adjacent, yes. That's a cycle: (1,1)-(1,2)-(1,3)-(2,3)-(2,2)-(2,1)-(1,1). That covers all 6 vertices. So 2x3 works, and one dimension is even (2). 2x4 works. 3x3: N=3, M=3, product 9 odd, so cannot have Hamiltonian cycle because bipartite parity fails (5 of one color, 4 of the other). Indeed, no Hamiltonian cycle. 3x4: product 12 even, both odd? Actually 3 is odd, 4 is even. Does 3x4 have a Hamiltonian cycle? Known result: A grid graph with both dimensions odd has no Hamiltonian cycle (since odd*odd = odd => number of vertices odd, can't have a cycle covering all because cycle length must be even). But if one dimension is even, the product is even, so parity condition satisfied. Is that sufficient? I recall that for rectangular grid graphs (with no missing vertices), a Hamiltonian cycle exists if and only if at least one dimension is even, except maybe for some small cases? Let's test: 1 x M: a single row grid. For M>1, does it have a Hamiltonian cycle? A cycle requires each vertex degree at least 2. In 1xM, interior vertices have degree 2 (except ends degree 1). But even if M=2, vertices are two adjacent cells. Can we form a cycle? The only possible cycle would need to go from one to the other and back, but that would repeat vertices? Actually a simple cycle must not repeat vertices; a cycle of length 2 would need an edge connecting the two vertices, but that's just one edge; to form a cycle you need at least 2 distinct edges forming a closed loop. In a simple graph, a cycle of length 2 is not allowed (it would be a multiple edge). So the smallest cycle is length 3 in a simple graph? Actually, cycle definition: a closed walk with no repeated vertices or edges except start/end. A cycle of length 2 would require two distinct vertices connected by two distinct edges (parallel edges), which doesn't exist in a simple graph. So no cycle exists in a 1xM grid for any M because it's a path graph. For M=1, trivial single vertex, no cycle (needs at least 3 vertices for cycle? Actually a cycle on one vertex with a loop? Not allowed). So for N=1 or M=1, the graph is a path, no Hamiltonian cycle because degree 1 vertices exist. So we must have both N >= 2 and M >= 2. Also, the condition that at least one dimension is even is necessary for even number of vertices, but for N=2, M=2, both even? Actually 2 is even, so okay. For N=2, M=3, works. For N=2, M=odd? 2x5 works? 2x5 has 10 vertices even, should have Hamiltonian cycle? Possibly yes. Known result: A grid graph of size m x n (with m,n >= 2) has a Hamiltonian cycle if and only if m and n are both even? Wait I'm not entirely sure. Let's check 3x4: 3 rows, 4 columns. Is there a Hamiltonian cycle? I've seen puzzles of finding a Hamiltonian cycle in a 3x4 grid. I think it's possible. For example, go around the perimeter: The perimeter includes all vertices? The perimeter of a 3x4 grid: vertices on the boundary: top row (1,1)-(1,2)-(1,3)-(1,4), right column (2,4)-(3,4), bottom row (3,3)-(3,2)-(3,1), left column (2,1). That's all vertices except (2,2) and (2,3) which are interior. So perimeter alone doesn't cover all. But can we incorporate interior? Likely possible. I recall a known theorem: A rectangular grid graph (P_m × P_n) has a Hamiltonian cycle if and only if mn is even and both m and n are greater than 1? Actually, I think the condition is: A grid graph (Cartesian product of paths P_m and P_n) has a Hamiltonian cycle iff either m or n is even and m,n>1. Let's verify small cases:
- 2x2: m=2 even, n=2 even => cycle exists.
- 2x3: m=2 even => cycle exists.
- 2x4: yes.
- 3x3: both odd, product odd => no cycle.
- 3x4: m=3 odd, n=4 even, product even => I think cycle exists. Quick mental construction: Let's try to construct a Hamiltonian cycle on 3x4. Label rows 1,2,3; columns 1..4. Find a cycle that covers all. One approach: Use a pattern like a "snake" but need to close. Could do: start at (1,1) -> (2,1) -> (2,2) -> (1,2) -> (1,3) -> (2,3) -> (2,4) -> (3,4) -> (3,3) -> (3,2) -> (3,1) -> (2,1)? Wait (2,1) already visited. Let's try systematically. There are known results: The grid graph P_3 × P_n has a Hamiltonian cycle if and only if n is even? Let's check: P_3 × P_2: 3x2 grid (3 rows, 2 columns). Number of vertices 6 even. Does it have a Hamiltonian cycle? Consider vertices: (1,1)-(1,2)-(2,2)-(2,1)-(3,1)-(3,2) and then back to (1,1)? (3,2) is adjacent to (2,2) and (3,1) and maybe (??) Actually (3,2) adjacent to (3,1), (2,2), (3,3?) no, column 2 only. So to close we need an edge from (3,2) to (1,1) which is not adjacent. So need a cycle. Let's attempt: Possibly (1,1)-(1,2)-(2,2)-(2,1)-(3,1)-(3,2)-(2,2)?? that repeats. Another try: The 3x2 graph is essentially two columns of three vertices each, edges vertical and horizontal between corresponding rows? Actually edges: vertical edges in each column between consecutive rows; horizontal edges between same row adjacent columns. So shape: 3 rows, 2 columns, it's like a ladder with 3 rungs. A ladder graph (2xn) for n=3? Actually ladder graph is P_n × P_2 (2 rows). For 3 rows, it's P_3 × P_2. Does it have a Hamiltonian cycle? Let's check bipartiteness: 3x2=6 even. But is it possible? I recall that P_3 × P_2 is actually isomorphic to a 3-by-2 grid. I can try to find a cycle: Go (1,1) -> (2,1) -> (3,1) -> (3,2) -> (2,2) -> (1,2) -> back to (1,1). Check edges: (1,2) to (1,1) is adjacent horizontally? Yes, (1,2) adjacent to (1,1). So the cycle would be (1,1)-(2,1)-(3,1)-(3,2)-(2,2)-(1,2)-(1,1). That's valid: (1,1) to (2,1) yes, (2,1) to (3,1) yes, (3,1) to (3,2) yes, (3,2) to (2,2) yes, (2,2) to (1,2) yes, (1,2) to (1,1) yes. So that's a Hamiltonian cycle! So 3x2 works. So both dimensions: one even? 2 is even, so condition "one even" works. But note 3 is odd.
What about 3x4? Let's try to construct using known pattern: For even number of columns, there is a simple Hamiltonian cycle pattern for grids with odd rows? Actually there is known result: P_m × P_n has a Hamiltonian cycle if and only if m and n are both even? But we just found a counterexample: 3x2, where m=3 odd, n=2 even, and Hamiltonian cycle exists. So that condition is too strict. Let's test 3x6? Should work. So the necessary and sufficient condition is: both dimensions are at least 2, and the number of vertices (NM) is even. But is that always sufficient? Consider N=2, M=2: even product, yes cycle. N=2, M=3: even product, yes cycle. N=2, M=4: yes. N=3, M=3: product odd, so no. N=3, M=4: product even, we think yes. N=3, M=2: we did, yes. N=4, M=4: even product, yes. N=4, M=5: product 20 even, should be yes? Likely yes. N=5, M=5: odd product, no. N=5, M=6: product 30 even, should be yes. Are there any exceptions? Possibly when one dimension is 1? But we require both >=2 because degree 1 vertices prevent cycles. Indeed, if N=1 or M=1, it's a path graph with endpoints degree 1, cannot have a cycle (unless it's a 1x1 which has a single vertex but no cycle). So for N=1 or M=1, no Hamiltonian cycle. Also, what about N=2, M=1? Equivalent to 1x2, no cycle. So the condition: N >= 2 and M >= 2 and NM is even. But is there any case where N >=2, M >=2, product even, yet no Hamiltonian cycle? I recall a known result: The grid graph P_m × P_n is Hamiltonian if and only if mn is even and m,n > 1. I think that's a standard theorem. Let's verify with small borderline cases: N=2, M=2 works. N=2, M=3 works. N=2, M=odd works? Yes, we have 2xodd, product even (since 2 * odd = even). For 2x odd, we can always have a Hamiltonian cycle? For 2x3 we did, 2x5 should also be possible: essentially a ladder graph with 5 rungs. A ladder graph P_2 × P_n has a Hamiltonian cycle if and only if n is even? Wait, P_2 × P_n is a 2xn grid. Does it always have a Hamiltonian cycle? Let's test 2x3: we found a Hamiltonian cycle: (1,1)-(2,1)-(2,2)-(1,2)-(1,3)-(2,3)-(1,1)? Check edges: (2,3) to (1,1) not adjacent. Let's construct properly. For 2x3, we need a cycle covering all 6 vertices. Actually, I earlier constructed a Hamiltonian cycle for 3x2, not for 2x3. Let's test 2x3 grid (2 rows, 3 columns). Can we find a Hamiltonian cycle? Coordinates: rows 1,2; cols 1,2,3. Want a cycle that visits all. Let's attempt: (1,1)-(1,2)-(1,3)-(2,3)-(2,2)-(2,1)-(1,1). Check edges: (2,1) to (1,1) is adjacent, yes. So the cycle is (1,1)->(1,2) ok, (1,2)->(1,3) ok, (1,3)->(2,3) ok, (2,3)->(2,2) ok, (2,2)->(2,1) ok, (2,1)->(1,1) ok. So yes, it works. So 2x3 works. What about 2x4? Works: go around perimeter: (1,1)-(1,2)-(1,3)-(1,4)-(2,4)-(2,3)-(2,2)-(2,1)-(1,1). Yes. So 2xn works for any n>=2? Check 2x2 works. So yes, 2xn has Hamiltonian cycles for all n>=2. Actually, there's a nuance: For 2xn, the cycle is simply go around the perimeter, which covers all vertices if n>=2, because the perimeter includes all vertices (the grid is only two rows, so the perimeter includes all top row, right column (only (2,n)?) Actually the perimeter of a 2xn grid: top row all cells, bottom row all cells, but leftmost column (both (1,1) and (2,1)) and rightmost column (both (1,n) and (2,n)) are also covered. The perimeter path would be: start at (1,1), go right along top row to (1,n), go down to (2,n), go left along bottom row to (2,1), go up to (1,1). That's a cycle that covers all vertices exactly once? Let's check: It visits (1,1) twice? Actually it starts and ends at (1,1). But in a cycle we don't repeat vertices except start=end. The described walk: (1,1) -> (1,2) -> ... -> (1,n) -> (2,n) -> (2,n-1) -> ... -> (2,1) -> (1,1). That visits each vertex exactly once? It visits (1,1) at start and at end; but that's allowed as start=end. It does not visit any other vertex twice. So indeed it's a Hamiltonian cycle. So 2xn works.
Now, what about N=3, M=3? product odd, no. N=3, M=5: product 15 odd, no. N=3, M=6: product 18 even, should have cycle. I can try to construct for 3x6. I believe it's possible. I recall a known fact: A rectangular grid graph has a Hamiltonian cycle if and only if it is not of odd × odd (i.e., both dimensions odd) and it is not a 1×something or something×1. Also, there is a special case: 2×odd works, but what about 4×odd? Works because even×odd gives even product. So the condition seems to be: N > 1 and M > 1 and N*M is even.
But wait, there is also the case of N=2, M=2 works. So the only forbidden cases are: when N=1 or M=1 (any dimension 1) because that's a path graph, no cycle; when both N and M are odd (product odd) because parity; and perhaps also when both are even? That's fine. So the answer should be Yes if N>=2 and M>=2 and N*M is even. But is there any other exception like N=2, M=1? Already covered by M=1. N=2, M=0 not allowed.
But what about N=2, M=1 is invalid because M>=1 but condition M>=2 fails. So answer No.
But check the example: Input 2 2 -> Yes, consistent: both >=2 and product 4 even.
Now, we must confirm that for N>=2, M>=2, and product even, a Hamiltonian cycle always exists. I think this is a known result in graph theory: The grid graph G_{m,n} (Cartesian product of paths P_m and P_n) is Hamiltonian if and only if m and n are both greater than 1 and mn is even. Yes, I've seen that. For instance, the statement: "The Cartesian product of two paths P_m and P_n is Hamiltonian if and only if at least one of m,n is even and m,n > 1." That is equivalent because mn even iff at least one even. So condition: m,n > 1 and at least one even. This is the typical result.
But wait, there might be a special case: When one dimension is 2 and the other is something? That's covered because 2 is even. So all good.
Thus the answer for each scenario: "Yes" if N > 1 and M > 1 and (N % 2 == 0 or M % 2 == 0); else "No".
But let's double-check borderline: N=2, M=1: N>1, M=1 so fails M>1, so No. That makes sense: 2x1 grid is basically a vertical line of two cells; there is no cycle because you'd need to return to start but only one edge; no cycle. N=1, M=1: trivial, no cycle. N=1, M=2: no cycle. So correct.
But wait, the problem statement: "Chef is stuck in a two dimensional maze having N rows and M columns." The cells are presumably indexed from 1 to N, 1 to M. He needs to find any magical path. We deduced that magical path exists iff Hamiltonian cycle exists. But is it possible that a magical path exists even if Hamiltonian cycle does not? Let's re-express: The path must start at (a,b) and end at (c,d) such that |a-c|+|b-d|=1. That means the endpoints are adjacent. The path must visit all cells exactly once. So it's a Hamiltonian path with adjacent endpoints. That is exactly equivalent to existence of a Hamiltonian cycle as argued. However, consider the case where there is a Hamiltonian path whose endpoints are adjacent but adding that edge does not yield a simple cycle because the graph might have that edge but maybe it creates a cycle that is not simple? Actually if the path is simple (visits each vertex once) and the endpoints are distinct and adjacent, then adding the edge gives a cycle that visits each vertex exactly once and returns to start; that is a Hamiltonian cycle. So indeed it's equivalent.
Thus condition stands.
But is there any scenario where a Hamiltonian path with adjacent endpoints exists even if a Hamiltonian cycle does not? That would be contradictory: because adding that edge gives a Hamiltonian cycle. Unless the endpoints are the same vertex? But they are distinct because distance 1 implies they are different. So it's fine.
Thus answer is Yes if grid has a Hamiltonian cycle.
Now check constraints: T up to 1e5, N,M up to 1e18. So we just need O(1) per test.
So we output "Yes" if N >= 2 and M >= 2 and (N % 2 == 0 or M % 2 == 0). Equivalent to: (N > 1 && M > 1 && (NM % 2 == 0)). Since NM might overflow 64-bit? N and M up to 1e18, product up to 1e36, which doesn't fit in 64-bit. But we can check parity without multiplication: N%2==0 or M%2==0. So use that.
Edge cases: N=0? Not in constraints (>=1). So okay.
Thus solution: For each test case, read N, M. If N >= 2 and M >= 2 and (N % 2 == 0 or M % 2 == 0) -> print "Yes" else "No".
But wait, what about N=1, M=2? We said No. But could there be a magical path? For a 1x2 grid, there are two cells. Starting at one cell and ending at the other, the Manhattan distance is 1 (adjacent). And we can traverse all cells exactly once: from (1,1) to (1,2) visits both cells exactly once. That seems to satisfy the conditions! Let's check: N=1, M=2. Cells: (1,1) and (1,2). Path: start at (1,1) -> (1,2). Endpoints: (1,1) and (1,2). |1-1|+|1-2| = 0+1 = 1. All cells traversed exactly once. Moves allowed: we moved right. So this is a valid magical path! So for 1x2, answer should be Yes. But our earlier reasoning about Hamiltonian cycles would say No because there is no Hamiltonian cycle (grid is 1x2) but we argued magical path equivalent to Hamiltonian cycle? Wait, for 1x2, the Hamiltonian path with adjacent endpoints exists. But adding the edge between endpoints would produce a cycle? In a 1x2 grid, the two vertices are connected by an edge. The path is just that edge. Adding the edge again would produce a cycle of length 2? In a simple graph, you cannot have a cycle of length 2 because it would require using the same edge twice, which is not allowed (would repeat edge). However, if you consider adding the edge to the path to form a cycle, you would have: path (v1, v2). Add edge (v2, v1) to close. That would give a closed walk v1-v2-v1, which uses the same edge twice (once in the path, once added). In a simple graph, that would be a walk that repeats the edge. But a Hamiltonian cycle is a simple cycle that does not repeat vertices or edges. The resulting closed walk would have repeated edge, so it's not a simple cycle. So the equivalence between Hamiltonian path with adjacent endpoints and Hamiltonian cycle holds only if there is an extra edge distinct from the ones used in the path that can be added to form a cycle without repeating edges. But in a simple graph, the edge between the endpoints may already be present, but if the path does not use that edge, then adding it yields a simple cycle (since the edge wasn't used). But in the 1x2 case, the path uses the only edge between the two vertices. So the path already uses that edge. The endpoints are the two vertices, and they are adjacent, but the edge between them is already traversed in the path. The condition for magical path does not forbid that the endpoints are adjacent via the same edge that is the last move? Actually the condition says: |a-c|+|b-d| = 1. It does not say that the edge between start and end is not used in the path. In a path that visits all vertices exactly once, if the endpoints are adjacent, it's possible that the last move goes from the penultimate vertex to the end, but that edge is part of the path. The condition only requires that the start and end cells themselves are adjacent, regardless of whether that edge is used or not. In the 1x2 example, start (1,1) and end (1,2) are adjacent, and the path consists of that single edge. So it's valid.
Our earlier equivalence to Hamiltonian cycle assumed that the path does not use the edge between the endpoints, because if it did, then adding it would duplicate an edge. But maybe we don't need to add; we just need existence of a Hamiltonian path with adjacent endpoints. In many grid graphs, you can construct such a path without using the edge between the endpoints. In the 1x2 case, you can't avoid using it because there is only one edge. So the equivalence to Hamiltonian cycle fails for this degenerate case. Also consider 1x1? For N=1, M=1, there's only one cell. Start and end same? The condition says start (a,b) and end (c,d) such that |a-c|+|b-d| = 1. If start = end, then |a-a|+|b-b| = 0, not 1. So need distinct cells. So 1x1 cannot have a magical path because only one cell and cannot have start and end distinct adjacent. So 1x1 is No.
What about 1x3? Grid 1x3: three cells in a line. We need to traverse all cells exactly once, start and end adjacent. Can we do that? Let's try: Path must cover all three cells. In a path graph of three vertices, the only Hamiltonian paths are from one end to the other end: (1,1)-(1,2)-(1,3) or reverse. The endpoints are (1,1) and (1,3). Their Manhattan distance = 2 (since columns differ by 2). So not adjacent. Could we start at middle and end at an end? That would be (1,2)-(1,1)-(1,2)? No, can't repeat. The only Hamiltonian paths in a path graph are the two that go from one end to the other, because to cover all vertices exactly once, you must start at one endpoint and end at the other. Because if you start at an interior vertex, you'll have to go to one neighbor then eventually you'll be forced to leave the other neighbor but then you'll have to come back? Let's check: For a path graph with vertices A-B-C, a Hamiltonian path is a permutation that respects adjacency? Actually a Hamiltonian path in a graph is a path that visits each vertex exactly once; it doesn't have to follow edges? Wait, but it's a path in the graph, meaning consecutive vertices must be adjacent. So in a path graph (linear chain), a Hamiltonian path must follow the edges in some order, but you can start at B? Suppose you start at B. You can go to A, then you cannot go to C because A and C are not adjacent. So you'd only visit A and B. So not all. So indeed, the only Hamiltonian paths are the ones that start at an endpoint and end at the other endpoint. So endpoints are not adjacent (distance 2). So 1x3 has no magical path. So 1x3 -> No.
What about 2x1? Same as 1x2, but transposed: 2 rows, 1 column. Then the two cells are adjacent vertically. Path can go from top to bottom, covering both cells, endpoints adjacent, distance 1. So 2x1 -> Yes.
So we have a special case where one dimension is 1 and the other is 2 (or vice versa) yields Yes. Also 1x2 and 2x1. What about 1x2? Yes. 1x1? No. 1x4? 1 row, 4 columns. Can we find a path that covers all 4 cells exactly once, with start and end adjacent? In a 1x4 path graph, endpoints of a Hamiltonian path that covers all vertices must be the two ends (positions 1 and 4) because any Hamiltonian path in a path graph must start and end at the two endpoints. The distance between 1 and 4 is 3, not 1. So not adjacent. Could there be a Hamiltonian path that doesn't use all edges? But in a simple path graph, the only simple paths covering all vertices must be the whole path from one end to the other; you cannot have a different ordering because edges only connect consecutive cells. So start and end must be the two endpoints. So 1x4 -> No. So for 1xN, magical path exists only when N=2? Because only then the two endpoints are adjacent (distance 1). Similarly, for M=1, only when N=2.
Now consider N=2, M=1 we said Yes. So these are special.
But wait, what about N=2, M=2? Already covered by general condition (both >=2, product even) -> Yes. That's fine.
Now what about N=2, M=3 we said Yes via Hamiltonian cycle. But is there a magical path? In 2x3, we have a Hamiltonian cycle. Remove any edge to get a Hamiltonian path whose endpoints are that edge's endpoints (adjacent). So that yields a magical path. So yes.
Now, consider N=2, M=2, we have a Hamiltonian cycle; remove an edge to get path; endpoints adjacent -> yes.
Now, what about N=2, M=1? It's special, not covered by general condition (M=1). But we already accounted.
Now, consider N=3, M=1? 3 rows, 1 column. That's a path of length 3. Hamiltonian path must start and end at endpoints: distance 2 (rows 1 and 3 difference 2). So not adjacent -> No. So only 1x2 and 2x1 work.
Thus overall condition for magical path: Existence of Hamiltonian path with adjacent endpoints. This is known as a "Hamiltonian path with endpoints adjacent". For grid graphs, we need to characterize.
Let's search memory: There is known result: An m x n grid graph has a Hamiltonian path between two given vertices if certain parity conditions hold. But we just need existence of any such path. I recall that the grid graph is Hamiltonian (has Hamiltonian cycle) iff m and n are both even? No that's wrong as we saw. Actually, many sources state: The grid graph P_m × P_n is Hamiltonian if and only if mn is even and m,n > 1. That is for Hamiltonian cycles. I'm fairly certain that's a standard theorem. For example, see "Hamiltonian cycles in grid graphs" by Itai, Papadimitriou, and Szwarcfiter (1982) where they prove: The grid graph G(m,n) has a Hamiltonian cycle if and only if (1) mn is even, and (2) m>1 and n>1. So yes, that is the theorem. So for m,n >=2, Hamiltonian cycle exists exactly when product even. So that implies magical path exists for m,n>=2 and product even.
But what about m,n>=2 and product odd (both odd)? Then no Hamiltonian cycle, but could there still be a Hamiltonian path with adjacent endpoints? If no Hamiltonian cycle exists, could there still be a Hamiltonian path whose endpoints are adjacent? Possibly if there is a Hamiltonian path that uses the edge between endpoints as part of the path? Actually if there is a Hamiltonian path with adjacent endpoints, then either the edge between them is used in the path (i.e., they are consecutive) or not used. If not used, then adding it yields a Hamiltonian cycle, contradicting the absence. So if a Hamiltonian cycle does not exist, then any Hamiltonian path with adjacent endpoints must use that edge. But does that still imply existence of a Hamiltonian cycle? Not necessarily; because if the edge is used, you cannot add it again to get a simple cycle (edge would be repeated). However, you might be able to re-route: maybe there exists another Hamiltonian path that doesn't use that edge, or maybe you can transform? But if there exists a Hamiltonian path with adjacent endpoints that uses that edge, does that guarantee existence of a Hamiltonian cycle? Not obviously. Could there be a grid where no Hamiltonian cycle exists (both odd) but there is a Hamiltonian path whose endpoints are adjacent? Let's test small odd×odd grids, like 3x3. Does 3x3 grid have a Hamiltonian path with adjacent endpoints? We can attempt to find one. 3x3 has 9 cells. We need a path that visits all cells exactly once, starting and ending at adjacent cells. Is that possible? Let's try mental search. Known fact: 3x3 grid has Hamiltonian paths, but typically endpoints are opposite colors? Because grid bipartite, any path alternates colors, so start and end must be opposite colors if length even (9 vertices => path length 8 edges, so number of vertices = 9, which is odd, so start and end must be same color? Actually for a bipartite graph, a path of even length (odd number of vertices) starts and ends in opposite partitions? Let's recall: In bipartite graph, vertices colored black and white. Edges go between opposite colors. A path of length L (number of edges) will have L+1 vertices. If L is even, then number of vertices is odd. Starting color, after odd number of edges, you end at opposite color. Wait: Starting color, after one edge (odd) you are opposite; after two edges (even) you are back to same color. So after L edges, the parity of L determines color: if L even, start and end same color; if L odd, start and end opposite. For a Hamiltonian path covering all vertices, L = NM - 1. So if NM is odd, then L is even, so start and end must be same color. For N*M even, L odd, start and end opposite colors.
Now, adjacent cells are always opposite colors (since grid is bipartite, adjacent cells have opposite colors). So if start and end are adjacent, they must be opposite colors. Therefore, necessary condition: start and end must be opposite colors. For a path with odd number of vertices (N*M odd), start and end must be same color (since L even). So impossible. Thus for odd×odd grids, no Hamiltonian path with adjacent endpoints exists because they would need to be opposite colors, but any Hamiltonian path covering an odd number of vertices must start and end on the same color. So that's a contradiction. So indeed odd×odd cannot have magical path. Good.
Now consider even number of vertices (NM even). Then start and end must be opposite colors. Since adjacent cells are opposite colors, this is compatible. So necessary condition: NM even. Also we need both dimensions >1? Not necessarily; consider 1x2: N*M=2 even, and indeed magical path exists. For 1x2, we have a path that covers both cells, start and end are adjacent and opposite colors? 1x2 grid: (1,1) black, (1,2) white? Typically chessboard: if (1,1) is black, then (1,2) is white. So start and end opposite colors; path length 1 edge (odd), so start and end opposite colors, consistent. So 1x2 works.
But 1x4: NM=4 even, but we argued no magical path. Why? Because the grid is a path graph, and in a path graph, any Hamiltonian path that covers all vertices must start and end at the two endpoints. The endpoints of the 1x4 path are (1,1) and (1,4), which are same color? Let's check: In 1x4, coloring: (1,1) black, (1,2) white, (1,3) black, (1,4) white? Actually alternating: positions: column 1 black, column 2 white, column 3 black, column 4 white. So endpoints (1,1) black, (1,4) white? Wait, column 4 is white if parity: (1,1) black, (1,2) white, (1,3) black, (1,4) white. So endpoints are black and white? Let's compute: (1,1) color = (1+1) mod 2 = 0? Usually (i+j) even = black? But we can define: (i+j) even = one color, odd = other. For (1,1) even => black; (1,4) 1+4=5 odd => white. So they are opposite colors. But the endpoints of the only Hamiltonian path are these two endpoints, which are opposite colors. That's fine. But are they adjacent? No, they are distance 3. So not magical. Could there be a different Hamiltonian path? In a path graph, is there a Hamiltonian path that starts at (1,2) and ends at (1,4)? That would need to cover all vertices: If start at (1,2), then we need to visit all vertices exactly once using edges. Starting at (1,2), we can go to (1,1) or (1,3). If we go to (1,1), then from (1,1) we can only go back to (1,2) (already visited) so dead end. If we go to (1,3) from (1,2), then we have visited (1,2) and (1,3). From (1,3) we can go to (1,2) visited, or (1,4). Go to (1,4), then visited (1,2),(1,3),(1,4). But (1,1) remains unvisited and unreachable because (1,2) is visited and it's the only neighbor of (1,1). So cannot. So only Hamiltonian paths start at one end and end at the other. So no magical path. So for 1x4, even though NM even, no magical path. So condition N*M even is necessary but not sufficient.
Now consider 2x1: works. 2x2 works. 2x3 works. 2x4 works. 3x2 works. 3x4 works (should). 3x6 works. So the pattern seems: Magical path exists if and only if (N >= 2 and M >= 2 and (N*M even)) OR (N == 1 and M == 2) OR (N == 2 and M == 1). But wait, what about N=1, M=2 we said Yes. N=2, M=1 Yes. Are there any other cases with a dimension 1? N=1, M=1 No. N=1, M=2 Yes. N=1, M=3 No. N=1, M=4 No. So only 1x2 and 2x1.
But could 1x2 be considered as a special case of N>=2, M>=2? No.
Now what about N=2, M=2? Already covered. So general condition for N>=2 and M>=2: magical path exists iff N*M even. Is that always true? We must be sure. For N=2, M=2 even works. For N=2, M=3 even works. For N=2, M=4 works. For N=3, M=2 works. For N=3, M=4 works. For N=3, M=6 works. For N=4, M=3 works. For N=5, M=4 works? 5x4 has 20 even. Should work. I'd like to confirm for 5x4: grid graph of 5 rows and 4 columns. According to the theorem, a Hamiltonian cycle exists if both dimensions >1 and product even. So yes. So magical path exists. So condition seems correct.
But let's double-check a known counterexample: What about 2x2? That's fine. 2x3 is fine. 3x3 is odd product, no. 4x4 fine. 5x5 odd product, no. So the theorem appears robust. However, we should consider if there is any exception like N=2, M=2 is fine. N=2, M=1 is special.
Now, is there any scenario where N>=2 and M>=2 and product even, but the grid is disconnected? No, grid is always connected.
Thus, the answer is Yes if:
- (N == 1 && M == 2) OR (N == 2 && M == 1) OR (N >= 2 && M >= 2 && (N % 2 == 0 || M % 2 == 0)).
But note that the condition (N >= 2 && M >= 2 && (N%2==0 || M%2==0)) is equivalent to (N >= 2 && M >= 2 && (NM % 2 == 0)). So we can combine: overall condition: (NM % 2 == 0) AND NOT (N == 1 && M != 2) maybe? Actually need to check all cases:
List all (N,M) with N>=1, M>=1:
-
If N=1:
- M=1 => product 1 odd => No.
- M=2 => product 2 even => Yes.
- M>=3: product even if M even? M=3 odd => product 3 odd => No; M=4 even => product 4 even. But we argued 1x4 No. So not all even products with N=1 work. So for N=1, only M=2 works. Similarly, for M=1, only N=2 works.
-
If N=2, M=1 => covered by M=1, N=2? But we treat symmetrically. So we can say: if min(N,M) = 1, then magical path exists only if max(N,M) = 2.
-
If both N>=2 and M>=2, then magical path exists iff product even.
Now, check edge case: N=2, M=2 fits both: product even, both >=2, so Yes. Good.
Now, what about N=2, M=1: min=1, max=2, so Yes. N=1, M=2: min=1, max=2, Yes.
Now, what about N=2, M=2? min=2 not 1, so condition product even -> Yes.
Now, what about N=1, M=2? product even, but the rule we just wrote for both>=2 doesn't apply because N=1, so we need separate handling.
Thus final condition: if (N == 1 && M == 2) or (N == 2 && M == 1) or (N >= 2 && M >= 2 && (N*M % 2 == 0)). But careful: N=2, M=1 will be caught by (N==2 && M==1) but also N>=2 && M>=2 fails because M=1. So okay.
We could also simplify: The magical path exists if and only if (N == 1 && M == 2) or (N == 2 && M == 1) or (N >= 2 && M >= 2 && (N%2==0 || M%2==0)). But note that N%2==0 || M%2==0 is equivalent to product even when both >=2? Actually product even is equivalent to at least one even (since both >=2 they are integers). So yes.
Now, is there any case where both N>=2, M>=2, product even but magical path does not exist? I want to be absolutely sure. Could there be a grid where Hamiltonian cycle does not exist but magical path still exists without being derived from a cycle? That would require a Hamiltonian path with adjacent endpoints where the edge between them is used in the path (so cannot form a cycle). For product even, we have Hamiltonian cycles, so that's not an issue. But if product even and at least one dimension >1, the theorem says Hamiltonian cycles exist. So we can always get magical path from cycle. So it's sufficient.
But what about product even and both dimensions >=2? Theorem says Hamiltonian cycle exists. Let's verify for some borderline: N=2, M=2: yes. N=2, M=4: yes. N=3, M=2: yes. N=3, M=4: we need to confirm thoroughly. I recall that the theorem might require that not both dimensions are odd, i.e., at least one even. That's exactly product even. So I'm confident.
But wait, there is also the case where N=2, M=2? both even, okay.
Now, what about N=2, M=0? Not possible.
Thus answer stands.
Now, we should also consider possibility N=0? No.
Now, double-check with the example: 2 2 -> Yes.
Now, maybe there are other special 1xM cases with M=2 only? What about 1x2 is yes. Could 1x0? no.
Now, also consider if both N and M are 1? No.
Now, what about N=2, M=1? we have yes. The problem example didn't cover that but we can test mental: 2 rows, 1 column. Starting at (1,1) down to (2,1) visits both cells, endpoints adjacent: distance 1 -> magical path. So yes.
Now, check with constraints: T up to 1e5, N,M up to 1e18. So we need fast IO, O(1) per test.
Thus we implement as described.
But before finalizing, we need to be absolutely sure about the theorem's scope: The grid graph considered is the Cartesian product of two paths. The existence of Hamiltonian cycles is indeed: For m,n > 1, G(m,n) is Hamiltonian if and only if mn is even. This is a known result. I want to recall any potential counterexample: e.g., 3x4, I think it's Hamiltonian. Let's try to construct explicitly to confirm. 3x4 grid (3 rows, 4 columns). I'll attempt to draw a Hamiltonian cycle:
Rows: 1,2,3; Cols: 1,2,3,4.
One known pattern: For odd number of rows and even number of columns, there is a standard construction: snake through rows and then wrap around. For 3x4, we can design:
Start at (1,1) -> (2,1) -> (2,2) -> (1,2) -> (1,3) -> (2,3) -> (2,4) -> (3,4) -> (3,3) -> (3,2) -> (3,1) -> (2,1)? Already visited (2,1). Hmm.
Let's try a more systematic approach: Use a cycle that goes around the perimeter and then weaves through interior. Another approach: There is a known pattern: For an m x n grid with m odd and n even, there is a Hamiltonian cycle. I can recall a standard pattern:
For m=3, n=4, you can do:
(1,1)-(2,1)-(3,1)-(3,2)-(2,2)-(1,2)-(1,3)-(2,3)-(3,3)-(3,4)-(2,4)-(1,4)-(1,1)?? Check edges: (1,4) to (1,1) not adjacent. So not.
Maybe: (1,1)-(1,2)-(2,2)-(2,1)-(3,1)-(3,2)-(3,3)-(2,3)-(2,4)-(1,4)-(1,3)-(??). Let's systematically search. However, I'm aware that many puzzles exist for 3x4 Hamiltonian cycle, so it's possible. I can also rely on known literature.
But to be thorough, let's attempt to find a Hamiltonian cycle for 3x4 using known algorithm: For even n, we can cover by 2xN blocks. For 3 rows, we can treat as 2 rows + 1 row. Another pattern: For 3x4, one known cycle is:
(1,1)-(2,1)-(3,1)-(3,2)-(2,2)-(1,2)-(1,3)-(2,3)-(3,3)-(3,4)-(2,4)-(1,4)-(1,1)? No.
Maybe we need to incorporate an extra detour: (1,1)-(2,1)-(3,1)-(3,2)-(2,2)-(1,2)-(1,3)-(2,3)-(2,4)-(3,4)-(3,3)-(1,3)? no.
Let's search memory: I recall an explicit Hamiltonian cycle for a 3x4 grid used in some problem solutions. Possibly:
Coordinates listed in order: (1,1), (1,2), (2,2), (2,1), (3,1), (3,2), (3,3), (2,3), (2,4), (1,4), (1,3), (??). After (1,3) we need to go to (1,1) but not adjacent. Could go to (2,3) already visited. So need a different ordering.
Maybe start at (1,1) go down to (2,1), then right to (2,2), up to (1,2), right to (1,3), down to (2,3), right to (2,4), down to (3,4), left to (3,3), left to (3,2), left to (3,1), then up to (2,1) already visited. So stuck.
Alternative: After (3,2), go up to (2,2) already visited.
Try: (1,1)-(1,2)-(2,2)-(2,1)-(3,1)-(3,2)-(3,3)-(2,3)-(1,3)-(1,4)-(2,4)-(3,4)-(3,1)? no.
Maybe we need to start elsewhere. Let's search internet memory: For 3x4, Hamiltonian cycle exists. I can quickly design a cycle using the fact that it's bipartite and even number of vertices, and connectivity is high.
Better: Use a known theorem: The grid graph is Hamiltonian if and only if it is not a 1xn (n>1) and not both dimensions odd. That includes 3x4. So I'll trust that.
But we must be absolutely sure because the problem is from CodeChef maybe? Usually they have problems like "Magical Path" or something. I've seen a problem "Chef and Magical Path" or something like that. I recall a CodeChef problem: "Magical Path" or "Magical Doors"? Not sure.
Let's think: The problem statement: Chef is stuck in a maze, needs to find a magical path, conditions, output Yes/No. Input: T, then N M. Example: 2 2 -> Yes. This looks like a typical parity problem. I've seen similar problem on CodeChef: "MAGICHF" or something? But not sure.
Anyway, to be safe, we could also consider if there is any other special case like N=2, M=2 we already covered. Could there be a case N=2, M=1? Already covered. Could there be N=2, M=0? no.
Thus, answer condition as described.
But wait: What about N=2, M=1 is covered by (N==2 && M==1) or (M==2 && N==1). So we need to include that.
Now, what about N=1, M=2? Same.
Now, consider N=1, M=2 works. Could N=1, M=2 be expressed also as product even? Yes, product even, but we need to exclude other 1x even? 1x4 product even but no. So we need to specifically handle the 1x2 and 2x1 cases.
Now, could there be a case N=1, M=2k (k>1) where there is a Hamiltonian path with adjacent endpoints that is not the simple endpoint path? Let's double-check: For 1x4, we argued the only Hamiltonian paths are from one end to the other. Could there be a Hamiltonian path that starts at (1,2) and ends at (1,3)? That would cover all vertices? Let's test: Start at (1,2). Move to (1,1) then to (1,2)? can't. Move to (1,3) then to (1,4) then? Then (1,1) remains unreachable. So no. So indeed only two Hamiltonian paths (end-to-end). So no magical path.
Thus, for 1xM with M>2, no.
Now, what about 2x1? Works. 2x1 is essentially same as 1x2 transposed.
Now, what about N=1, M=2 is the only width-1 case that works.
Now, consider N=3, M=1: No. So done.
Now, what about N=2, M=2? works. Good.
Now, we must consider the possibility of N=0? Not.
Now, the output: "Yes" or "No".
Now, let's double-check with some small cases manually to ensure condition matches.
We'll enumerate small N and M:
- (1,1): N*M odd, min=1, not (1,2) or (2,1) => No.
- (1,2): min=1, max=2 => Yes.
- (1,3): max=3 => No.
- (1,4): max=4 => No.
- (2,1): Yes.
- (2,2): both >=2, product even => Yes.
- (2,3): both >=2, product even => Yes.
- (2,4): Yes.
- (3,1): No.
- (3,2): both >=2, product even => Yes.
- (3,3): both odd => product odd => No.
- (3,4): product even => Yes.
- (4,1): No.
- (4,2): Yes.
- (4,3): Yes.
- (4,4): Yes.
- (5,1): No.
- (5,2): Yes.
- (5,3): product 15 odd => No.
- (5,4): product 20 even => Yes.
- (5,5): odd => No.
These seem plausible.
Now, is there any case where both N and M are even but one is 2? Works. Good.
Now, what about N=2, M=2 is even. Good.
Thus answer condition: if (N == 1 && M == 2) or (N == 2 && M == 1) or (N >= 2 && M >= 2 && (N%2 == 0 or M%2 == 0)) then Yes else No.
But we can simplify: Equivalent to: if (N == 1 && M == 2) or (N == 2 && M == 1) or (min(N, M) >= 2 && (N % 2 == 0 || M % 2 == 0)). Because if min(N,M) >=2, both >=2. So condition: (min(N,M) >= 2 && (N%2 == 0 || M%2 == 0)) or (N==1 && M==2) or (N==2 && M==1). But note that (N==1 && M==2) or (N==2 && M==1) are actually also covered by (N%2==0 || M%2==0) with min(N,M)=1? Not exactly because min(N,M) =1 not >=2. So we need explicit.
Now, we can also think: The magical path exists if and only if (N*M is even) and not (N==1 && M>2) and not (M==1 && N>2). But that's more complicated.
Another approach: Derive condition based on graph theory: For a grid graph, a Hamiltonian path with adjacent endpoints exists iff either (i) N=1, M=2 or N=2, M=1; or (ii) min(N,M) >= 2 and N*M is even. This is neat.
Now, we should verify the condition for N=2, M=1: min=1, not >=2, but special case, product even, and we say Yes. For N=1, M=2: same. For N=2, M=2: min=2 >=2, product even => Yes.
Now, any other case where product even but min(N,M) =1 and max(N,M) !=2? That would be (1, even >2) or (even >2, 1). Those we say No. So we need to exclude them.
Now, is there any case with min(N,M) >=2, product even, but magical path does not exist? Let's try to find a potential counterexample: N=2, M=2 works. N=4, M=2 works. N=6, M=2 works. N=2, M=6 works. N=3, M=6 works. N=3, M=2 works. N=3, M=4 works. N=4, M=4 works. N=5, M=6 works. I'm comfortable.
However, I recall that there is a nuance: The grid graph is considered as vertices in a rectangular lattice with edges between orthogonal neighbors. But sometimes, the grid graph is defined as having vertices for each cell, and edges between adjacent cells. That's what we used. So it's exactly the Cartesian product of two paths.
Now, there is also the concept of "Hamiltonian path with endpoints adjacent" which is also known as "Hamiltonian cycle missing one edge" if that edge is not used. But if the edge is used, it's not a missing edge; it's just a Hamiltonian path that starts and ends at adjacent vertices but they are consecutive in the path. However, if a Hamiltonian cycle exists, we can always get such a path by removing an edge that is not the one connecting start and end? Actually, we need the path's endpoints to be adjacent. If we remove any edge from a Hamiltonian cycle, we get a Hamiltonian path whose endpoints are the vertices of that edge, which are adjacent. So yes.
Thus existence of Hamiltonian cycle implies existence of magical path.
Now, could there be a magical path when no Hamiltonian cycle exists? As argued, for odd×odd, impossible due to parity. For 1x2 and 2x1, we have magical path without Hamiltonian cycle. Are there any other such? For 2x1, we have a Hamiltonian path that uses the only edge, endpoints adjacent. That's a magical path but no Hamiltonian cycle. So these are the only cases with min(N,M)=1 that work. Could there be a case with min(N,M)>=2 and product even but no Hamiltonian cycle? According to theorem, no. So magical path exists.
Thus answer conditions are correct.
Now, let's also consider N=2, M=2 we have cycle. So all good.
Now, final answer: For each test case, output "Yes" if condition holds else "No".
Implementation details: read T, loop, read N, M as long long (since up to 1e18, use 64-bit). Then:
bool ok = false;
if (N == 1 && M == 2) ok = true;
else if (N == 2 && M == 1) ok = true;
else if (N >= 2 && M >= 2 && (N % 2 == 0 || M % 2 == 0)) ok = true;
print ok ? "Yes" : "No"
We can also combine the first two into: if (N == 1 && M == 2) or (N == 2 && M == 1). Or check: if (N * M == 2) maybe? Since N,M positive integers, product=2 only possibilities: (1,2) and (2,1). So we can write: if (N * M == 2) ok = true; but careful: product=2 and also both >=1. But if N=1, M=2 product=2; N=2, M=1 product=2; also N=2, M=1 product=2; but also N=2, M=1 product=2. So check (N * M == 2) works. However, product might overflow if we multiply two 1e18 numbers? But we only multiply when N and M are small? Actually N and M up to 1e18, product can be up to 1e36, which doesn't fit in 64-bit unsigned? 2^63 approx 9.22e18, so product may overflow. So we cannot directly multiply unless using big integers. So better avoid multiplication. So check with conditions (N==1 && M==2) or (N==2 && M==1).
But we could also check: if (N==1 && M==2) or (N==2 && M==1). That's safe.
Alternatively, we could check: if (min(N,M)==1 && max(N,M)==2) then Yes.
But then for general: if (min(N,M) >= 2 && (N%2==0 || M%2==0)) Yes else No.
That's straightforward.
Now, check if any case with min(N,M) >=2 and product even but maybe N=2, M=2 already covered.
Now, finalize.
But wait, there's one more nuance: The condition for a Hamiltonian cycle is that both dimensions are >1 and product even. However, is there any exception when one dimension is 2 and the other is 2? No, it's fine. When one dimension is 2 and the other is odd? That's product even, works. So okay.
Thus answer.
Now, before finalizing, I'll attempt to prove the theorem quickly: For grid graphs with both dimensions >1, Hamiltonian cycle exists iff product even. Known proof: If both odd, no because bipartite with unequal parts. If at least one even, construct a Hamiltonian cycle by induction or by explicit pattern: snake through rows and then connect ends. For example, if number of columns is even, you can snake row by row and then connect first and last cells via the border. For odd rows and even columns, you can still snake but need to adjust. Actually, classic construction: For m x n with m even, you can snake each row left to right and right to left, and then connect the ends vertically to form a cycle. For m odd and n even, you can snake but need to handle the last row specially. There is a known explicit construction: if n is even, you can produce a Hamiltonian cycle for any m>=2. So yes.
Thus safe.
Now, we produce solution.
Potential pitfalls: T up to 1e5, need fast I/O. Use scanf/printf or cin with sync.
Now, let's write solution.
But before final answer, let's double-check with some random tests:
Seems fine.
Now, edge case: N=10^18, M=10^18-1 (both large). If N even? If N is even, then Yes, else M might be even? N=odd, M=odd? Then product odd => No. So fine.
Now, we should also consider N=2, M=1 is Yes. For T large, it's fine.
Now, confirm with the problem's constraints: 1 ≤ T ≤ 10^5, 1 ≤ N, M ≤ 10^18. So we must use 64-bit integers.
Now, final code:
#include <bits/stdc++.h>
using namespace std;
int main() {
int T;
scanf("%d", &T);
while (T--) {
long long N, M;
scanf("%lld %lld", &N, &M);
bool ok = false;
if (N == 1 && M == 2) ok = true;
else if (N == 2 && M == 1) ok = true;
else if (N >= 2 && M >= 2 && (N % 2 == 0 || M % 2 == 0)) ok = true;
puts(ok ? "Yes" : "No");
}
return 0;
}
But we can simplify the first two: if (min(N,M)==1 && max(N,M)==2) ok = true; This is equivalent and maybe more elegant.
Now, final answer.
But before final, I want to test a bit more: Consider N=2, M=2: min=2, not 1, so goes to else if: N>=2 M>=2 true, N%2==0 (2 even) => true => Yes.
Consider N=2, M=1: min=1, max=2 => first condition true => Yes.
Consider N=1, M=1: min=1, max=1 !=2 => not true; else if conditions: N>=2 fails => false => No.
Consider N=1, M=4: min=1, max=4 !=2 => false; else if: N>=2 false => No.
Thus correct.
Now, we should ensure that we haven't missed a case like N=2, M=2 obviously covered.
Thus answer is ready.
But hold on: Could there be a case where N=1, M=2 but we output Yes; but maybe the problem expects No? Let's read problem statement: "A path is defined as magical if it starts from any of the cell (a,b) of the maze and ends at the cell (c,d) such that |a - c| + |b - d| = 1. All the cells in the maze are traversed exactly once. It is allowed to move only in the four directions - up, down, left and right from the current cell." For 1x2 maze, we can start at (1,1) and end at (1,2) after moving right, traversing both cells exactly once. So indeed magical path exists. So Yes.
If the problem's example had only 2 2 -> Yes, they might have not included 1 2 test but it should be Yes.
Now, I'll search memory: There is a known CodeChef problem "MAGICP" or something. Actually, I recall a problem named "Magical Path" where answer was "Yes" if N*M is even and not (N==1 or M==1 except 2). Possibly same.
But to be thorough, let's check if there is any other corner: N=2, M=1 we said Yes. But does the condition |a-c|+|b-d|=1 hold? For 2x1, start (1,1) down to (2,1) distance 1, works. So yes.
Thus answer correct.
Now, finalize solution.
I'll produce a clear explanation in the final answer: Show reasoning, equivalence to Hamiltonian cycle, conditions.
But as per instructions: only need code? The user asked: "Write code to solve the problem." But as ChatGPT, we should provide explanation and code. However, the final output should be the code? The user might expect just the code. But in this environment, we need to output the final answer with explanation? The system instruction: "You are ChatGPT... Write code to solve the problem." Typically we give explanation and code. But I'll provide both.
Thus final answer: Provide reasoning and then code.
Now, to be thorough, let's test some more cases with small script mental:
(1,2): Yes.
(2,1): Yes.
(2,2): Yes.
(2,3): Yes.
(2,4): Yes.
(3,2): Yes.
(3,4): Yes.
(3,6): Yes.
(3,8): Yes.
(4,5): Yes (even product).
(4,6): Yes.
(5,6): Yes.
(5,7): product 35 odd => No.
(6,7): even product? 6 even => Yes.
(6,8): Yes.
(1,1): No.
(1,3): No.
(1,4): No.
(1,5): No.
(2,0) no.
All good.
Now, ensure we handle T up to 1e5, so fast I/O.
Now, code in C++.
Potential micro-optimizations: Use getchar or fast scanf.
Now, final answer.
But also note: Some languages like Python might have big ints but we are using C++.
Now, final code.
Now, I'll produce the final answer with explanation and code. ```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int T;
scanf("%d", &T);
while (T--) {
long long N, M;
scanf("%lld %lld", &N, &M);
bool ok = false;
// special 1x2 and 2x1 cases
if ((N == 1 && M == 2) || (N == 2 && M == 1))
ok = true;
// both dimensions at least 2 and at least one even
else if (N >= 2 && M >= 2 && (N % 2 == 0 || M % 2 == 0))
ok = true;
puts(ok ? "Yes" : "No");
}
return 0;
}