We are given a special jigsaw puzzle: pieces have three tabs and one blank (like a typical puzzle piece shape? Possibly a square piece with tabs on three sides and a blank on one side? The image likely shows a square piece with three protrusions (tabs) and one indentation (blank). Each piece can be rotated. They are identical pieces. We need to arrange them into an n x m grid. Condition: For any two adjacent pieces (sharing an edge), a tab of one must fit into a blank of the other. That is, along each shared edge, one side must have a tab and the other side a blank. So essentially, along each internal edge between two pieces, we need one piece to have a tab on that side and the other a blank on that side. At the border edges (outer boundary) there is no neighbor, so no condition.
Each piece has exactly three tabs and one blank, meaning it has three sides with tabs, one side with a blank. Since pieces are square? Typically jigsaw puzzle pieces are interlocking with tabs and blanks; they are often shaped but for a grid they'd have four sides. So each piece has 4 sides. Among them, exactly 3 are tabs, 1 is blank. So each piece can be oriented by rotation, meaning we can choose which side is blank (and the opposite side? Actually rotation can place the blank on any of the four sides; the relative positions of tabs are fixed because exactly three sides are tabs, one blank; after rotation, the blank can be on north, east, south, or west). So we have identical pieces, but we can rotate each independently.
We need to assign orientations to each cell such that for each adjacent pair, exactly one of the two sides (the sides that face each other) is a tab and the other is a blank. That is, if we consider a graph of cells, each edge between neighbors must be satisfied: the two pieces on that edge must have opposite types (one tab, one blank) on that side.
We need to decide if such an assignment exists.
We can think of a grid, each cell has a state: orientation, which determines which side is blank. Let’s denote the sides: N, E, S, W. We have a piece type: one blank side, three tab sides. So for a given orientation, the blank is on one side, and the other three are tabs. For a cell, the type of side (tab or blank) is determined by whether that side is the blank side.
Condition: For each internal edge (horizontal between (i,j) and (i,j+1)), we need the east side of left cell and west side of right cell to be opposite: exactly one tab and one blank. Similarly for vertical edges between (i,j) and (i+1,j): south side of top cell and north side of bottom cell must be opposite.
Goal: Existence of assignment.
We can treat as a constraint satisfaction problem on grid. Perhaps it reduces to parity conditions or something.
Observation: Since each piece has exactly three tabs and one blank, the total number of tabs in the whole puzzle is 3nm, and blanks nm (since each piece contributes one blank). But that might not directly help.
Let's think about constraints on degrees: For each edge, we need a tab-blank pair. So each edge consumes one tab from one piece and one blank from the other. But a piece has multiple sides: a piece with blank on one side will have that side blank, other sides tab. So each piece contributes one blank side (the side that is blank) and three tab sides.
Now, consider the sum over all edges (internal) of some indicator. Actually think of graph of cells with adjacency edges. There are (n-1)m vertical edges + n(m-1) horizontal edges = total internal edges = n(m-1)+(n-1)m = 2nm - n - m.
Each internal edge must have one tab and one blank. So total number of tab sides used in internal edges is equal to total number of blank sides used in internal edges = number of internal edges. But each piece may also have some sides on the boundary; those sides are not used (no neighbor). There is no condition on boundary sides.
So for each piece, the number of its sides that are used in internal edges depends on its position: interior cells (with 4 neighbors) have all four sides used; edge cells (on border but not corner) have 3 sides used; corner cells have 2 sides used. The condition is that for each piece, the sides that are used must be such that the edges are satisfied. However, the assignment of orientation determines which side is blank; we need to ensure that for each edge the two sides are opposite.
This is reminiscent of a two-coloring of edges? Or perhaps we can model as: For each cell, assign a variable indicating orientation, but maybe we can reduce to a simpler condition: that the total number of blanks in the interior edges must equal total number of tabs in interior edges, which is automatically equal if each edge gives one of each, but that's not a constraint on pieces because pieces have fixed numbers of tabs and blanks. Actually, total number of blank sides that are exposed to internal edges (i.e., blanks that are on sides that have neighbors) must equal total number of internal edges (since each edge requires a blank). Similarly total number of tab sides exposed to internal edges must equal total number of internal edges. But each piece contributes its one blank on one side; whether that blank side is used (i.e., faces a neighbor) depends on orientation and position. Similarly, each piece contributes three tabs on the other sides; but some of those tab sides may face the boundary and not be used. So we have to assign orientations such that the total number of blank sides that are internal equals total number of tab sides that are internal, and both equal number of internal edges. But the total number of blank sides that are internal cannot exceed the total number of blanks available (which is nm, since each piece has exactly one blank side). However, if a blank side faces the boundary, it's not used, so the used blanks are subset. Similarly, used tabs are subset of total tabs (3nm). So we need:
Let B_used = number of pieces whose blank side is adjacent to another piece (i.e., not on boundary). Then we need B_used = E, where E = number of internal edges. Also, let T_used = number of pieces whose tab sides are adjacent to another piece. Since each piece may have multiple tab sides used, we need total T_used = sum over pieces of number of tab sides that face a neighbor = E as well.
Now, B_used is at most nm, and T_used is at most 3nm. For large grids, E is about 2nm, so B_used ≈ 2nm, but B_used cannot exceed nm because there are only nm blank sides total. So we must have E ≤ nm. Similarly, E ≤ 3nm (trivial). So a necessary condition is: number of internal edges ≤ total number of blanks available (i.e., nm). Since each internal edge requires a blank to be present on one side, and each blank can be used at most once (each blank side is part of exactly one edge if it faces a neighbor). So we need:
E = n(m-1) + (n-1)m = 2nm - n - m ≤ nm
=> 2nm - n - m ≤ nm
=> nm - n - m ≤ 0
=> nm ≤ n + m
=> nm - n - m ≤ 0
=> (n-1)(m-1) ≤ 1
Because nm - n - m +1 = (n-1)(m-1) so nm - n - m = (n-1)(m-1) -1. Let's derive:
nm - n - m ≤ 0 => nm - n - m + 1 ≤ 1 => (n-1)(m-1) ≤ 1.
Yes, exactly: (n-1)(m-1) ≤ 1.
Thus necessary condition: (n-1)(m-1) ≤ 1. That is, either n=1 or m=1, or n=m=2 (since (1,any) gives 0 ≤1, (any,1) gives 0, (2,2) gives (1)(1)=1 ≤1). Also (n,m) = (2,1) and (1,2) already covered by n=1 or m=1. Also (n,m)=(1,1): (0)*(0)=0 ≤1, that's trivial? n=1,m=1 grid is single piece, no internal edges, condition automatically satisfied? But we have piece with three tabs and one blank; no adjacency constraints, so any orientation works. So (1,1) is possible.
Thus from this counting argument, necessary condition is that either one of dimensions is 1, or both dimensions are exactly 2. However, the sample input: 1 3 -> YES (n=1, so OK). 100000 100000 -> NO (since (10^5-1)^2 >>1). 2 2 -> YES. So matches.
But is this condition also sufficient? We need to determine if for these cases we can always assign orientations to satisfy adjacency constraints. For n=1 or m=1 (i.e., single row or column), we need to arrange pieces in a line. For each adjacent pair, we need one tab and one blank on the shared side. Since each piece has exactly one blank and three tabs, we can orient them appropriately along the line? Let's examine: For a single row of m pieces, there are m-1 internal edges (horizontal). Each piece has left and right sides. For interior pieces (except ends), both sides have neighbors. For end pieces, only one side has neighbor. We need to assign orientations (which side is blank) such that for each adjacent pair, exactly one of the two facing sides is blank and the other is tab.
Consider a line: We can treat it as a sequence. Let’s denote for each piece, we choose the side that is blank. For a piece, if we set blank on left, then its left side is blank, right side is tab (since only one blank, the other three are tabs). Similarly, if blank on right, then right side blank, left side tab. If blank on top or bottom (in a single row, top and bottom are boundaries, not used), those are okay but then both left and right would be tabs? Actually, if blank is on top (or bottom), then left and right sides are both tabs (since those are not the blank side). So piece would have both sides tabs. Similarly, if blank is on left, left=blank, right=tab; blank on right, left=tab, right=blank; blank on top/bottom: left=tab, right=tab.
Thus each piece can have configurations: (left,right) can be (blank,tab), (tab,blank), or (tab,tab). (Note: if piece is oriented with blank on left, then right is tab automatically; similarly blank on right yields left tab; blank on top/bottom yields both tab). Also note blank on top/bottom are essentially the same for our purposes.
Now, we have a line of m pieces. For each internal edge between i and i+1, we need that the right side of piece i and left side of piece i+1 are opposite: one blank, one tab. So we need that for each i, the pair (right_i, left_{i+1}) is (blank,tab) or (tab,blank). That means they cannot be both blank (impossible because each piece has at most one blank, and if both right_i and left_{i+1} were blank, then both pieces would have blank on that side, but could that happen? For piece i, right blank if its blank is on right; piece i+1, left blank if its blank is on left. So it's possible both are blank? But then the edge would have blank-blank, which is not allowed (must be tab-blank). So we must avoid that. Also both tabs is not allowed because we need one blank. So allowed pairs: (blank,tab) or (tab,blank). So essentially, we need that for each edge, the two sides are different. So it's like we need to assign to each piece a "type" that determines its left and right states.
But we also have to respect that each piece has exactly one blank overall; if we assign blank on left or right, then top/bottom become tabs, no issue. If we assign blank on top/bottom, then both left and right are tabs. That piece would then have tabs on both sides. So that piece would contribute (tab,tab). Then for edges adjacent to that piece, we would need the neighbor to have opposite: i.e., if piece i has right=tab, then piece i+1 must have left=blank. But if piece i has both sides tab, then both its left and right are tab. Then for edge between i-1 and i, left_i=tab, so piece i-1 must have right=blank. And for edge between i and i+1, right_i=tab, so piece i+1 must have left=blank. So a piece with both tabs forces its neighbors to have blanks on the sides facing it. That is okay as long as we can assign accordingly.
Also note that end pieces: for the leftmost piece, its left side has no neighbor; so we don't care if it's blank or tab. Similarly rightmost piece's right side is free. So only the internal edges matter.
Now, can we always assign orientations for any m in a single row? Let's try small cases.
m=1: trivially yes.
m=2: two pieces with one internal edge. We need one blank and one tab on that edge. Options: we could orient piece1 with blank on right (so right blank), piece2 with blank on left (left blank) would give blank-blank -> not allowed. So we need opposite. So we could set piece1 with blank on right (right blank) and piece2 with blank on top (so left=tab) gives blank-tab? Actually piece2 with blank on top => left=tab, right=tab. So then right of piece1=blank, left of piece2=tab -> allowed (blank-tab). Also piece1 could have blank on top (both tabs) and piece2 blank on left (left blank) gives tab-blank, allowed. Or piece1 blank left (left blank, right tab) and piece2 blank on left? That would give right of piece1=tab, left of piece2=blank? Actually piece2 blank on left => left blank, right tab. So right_i=tab, left_{i+1}=blank -> allowed. So many possibilities. So m=2 works.
m=3: three pieces, edges between 1-2 and 2-3. Need to assign. Let's see if always possible. Try to construct: we can use a pattern: piece1: blank on right (so right blank, left irrelevant), piece2: blank on top (both tabs), piece3: blank on left (left blank). Check edges: edge1: piece1 right=blank, piece2 left=tab -> good; edge2: piece2 right=tab, piece3 left=blank -> good. Works. Many others. Could we always? Possibly yes.
But wait: there might be a parity constraint if m is large? Let's consider the sum of something. Because each piece contributes at most one blank side overall. If we have many edges, we need many blank sides used. In a single row, number of internal edges = m-1. The total number of blanks that can be used (i.e., that face a neighbor) is at most the number of pieces, because each piece can contribute at most one blank to an internal edge (since it has exactly one blank, but if that blank faces a neighbor, it's used; if it faces the boundary, it's unused; if it faces top/bottom (in a row), those are boundaries? Actually top and bottom are boundaries because no neighbor (since single row, there is no piece above or below), so blank on top or bottom would be unused (i.e., not facing a neighbor). So if we place blank on top/bottom, then that piece contributes 0 blanks to internal edges. If we place blank on left or right, that side might face a neighbor if the piece is interior (except ends). For interior pieces (not ends), if blank is on left, then that blank is used if the left neighbor exists; similarly blank on right is used. For end pieces, if blank is on the outer side (left of leftmost or right of rightmost), it faces boundary and is unused. So the total number of blanks used in internal edges is exactly the number of pieces that have blank on a side that is not a boundary side (i.e., left side for pieces except leftmost, right side for pieces except rightmost). Actually a piece could have blank on left, and if it's not leftmost, that blank is used. Similarly, blank on right, if not rightmost, used. But a piece could have blank on left and be leftmost: unused; blank on right and be rightmost: unused. Also blank on top/bottom always unused (since no vertical neighbors in a single row). So maximum number of blanks used is at most the number of pieces, but we need exactly m-1 blanks used because each edge needs one blank. So we need m-1 blanks used. Is it possible to have that many? For m pieces, each can contribute at most 1 blank used, so need m-1 ≤ m, always true. But can we achieve exactly m-1? That would require that all but one piece contribute a used blank. Since each piece that contributes a used blank must have its blank on a side that faces a neighbor. That means for interior pieces, blank must be on left or right (and not both because only one blank). For end pieces, they have only one neighbor, so to contribute a used blank, they must place blank on the interior side (right for leftmost, left for rightmost). So if we want m-1 blanks used, we need every piece except possibly one to have blank on the side facing a neighbor. That means at most one piece can have blank on top/bottom (unused) or on the exterior side (unused). Is that feasible? Let's try for m=3: we need 2 blanks used. Can we have all three pieces with used blanks? That would be 3 >2; but we need exactly 2. So we need one piece to have its blank unused, others used. Example above: piece1 blank right (used), piece2 blank top (unused), piece3 blank left (used) => 2 used, works. So it's possible.
For general m, can we always assign such that exactly m-1 blanks are used? Possibly yes. But we also need the tab-blank pairing condition (opposite sides) at each edge, not just count of blanks. So we need to find assignment.
This can be modeled as a graph coloring or something. Let's think more systematically.
Define for each piece i (1..m) we need to choose orientation. Let's denote for each piece a variable indicating which side is blank: L, R, T (top), B (bottom). Since it's a single row, top and bottom are symmetric (both boundary), so we can treat them as same: "vertical" orientation (blank on top/bottom). In vertical orientation, both left and right are tabs.
Now for edges, we need: for i from 1 to m-1, the pair (right_i, left_{i+1}) must be (blank, tab) or (tab, blank). So we need that right_i != left_{i+1} in terms of blank vs tab.
Given that each piece's left and right are determined by its blank side:
- If blank = L: left = blank, right = tab.
- If blank = R: left = tab, right = blank.
- If blank = T or B: left = tab, right = tab.
Thus left and right can be: (blank,tab), (tab,blank), (tab,tab). They are never (blank,blank).
Now the condition for each edge: right_i and left_{i+1} must be opposite. So we need:
Case: (right_i, left_{i+1}) can't be (blank, blank) [impossible anyway] nor (tab,tab) [both tabs], nor (blank,blank). The only allowed combos are (blank, tab) or (tab, blank). So basically right_i and left_{i+1} must be different.
We can think of assigning each piece a "type" that defines its left and right. Let's denote piece's type as: L-type: (L,R) = (B,T) where B=blank, T=tab? Actually let's use values: blank = 0, tab = 1. Then:
- Type L: left=0, right=1.
- Type R: left=1, right=0.
- Type V (vertical): left=1, right=1.
We need for each i: right_i ≠ left_{i+1}. Since right_i is either 1 or 0, left_{i+1} is either 1 or 0. So condition: right_i XOR left_{i+1} = 1.
Now, we have a sequence of variables. This is like a constraint satisfaction that is always solvable? Let's check small m systematically to see if any m fails.
We can treat as a finite automaton: we can decide piece by piece, but we have to also respect that each piece has exactly one blank overall, which we already encoded.
The constraints: For each i from 1 to m-1: if piece i is type L: right_i=1; type R: right_i=0; type V: right_i=1. Wait careful: L: left=0, right=1. So right_i=1. R: left=1, right=0 -> right_i=0. V: right_i=1. So right_i can be 1 for L or V, 0 for R.
Similarly, left_{i+1}: for piece i+1, left is: L: left=0, R: left=1, V: left=1.
So constraint: right_i ≠ left_{i+1}. Let's list possibilities:
-
If piece i is L (right_i=1), then left_{i+1} must be 0 → piece i+1 must be L (since only L gives left=0). R gives left=1, V gives left=1. So if piece i = L, then piece i+1 must be L.
-
If piece i is R (right_i=0), then left_{i+1} must be 1 → piece i+1 can be R (left=1) or V (left=1). So piece i+1 can be R or V.
-
If piece i is V (right_i=1), then left_{i+1} must be 0 → piece i+1 must be L.
Thus transition rules:
- L -> must be followed by L.
- R -> can be followed by R or V.
- V -> must be followed by L.
Also, consider the start and end: no constraints on leftmost piece's left or rightmost piece's right.
Now we need to see if for any length m, there exists a sequence of types (each being L, R, V) such that these constraints hold for all consecutive pairs. This is a directed graph of allowed transitions. Let's build state machine.
States: L, R, V.
Edges: from L to L only; from R to R and V; from V to L only.
We need a walk of length m (m vertices) through this state graph. That is, we need a sequence s1, s2, ..., sm such that for i=1..m-1, (s_i, s_{i+1}) is allowed.
Is there any sequence for any m? Let's check reachability.
From L, the only outgoing is to L, so once you are L, you must stay L forever. So a sequence starting with L is all L's.
From V, you must go to L, then all L's afterwards (since from L you can only go to L). So a sequence starting with V: s1=V, then s2 must be L, then s3 must be L, ... So sequence: V, L, L, L, ...
From R, you can go to R or V. If you go to R, you can continue staying R (since R->R) or go to V at some point; if you go to V, then next must be L and then L forever.
So many possibilities.
Now, is it possible for any m? Let's test m=1: any state allowed (L, R, V) because no constraints.
m=2: we need a pair (s1,s2) allowed. Allowed pairs: (L,L), (R,R), (R,V), (V,L). So many. So m=2 possible.
m=3: we need s1,s2,s3 with allowed transitions s1->s2 and s2->s3. Let's see if there exists at least one. For example, R,R,R works? Check: R->R allowed, R->R allowed. Yes. Also V,L,L works (V->L, L->L). L,L,L works. So m=3 ok.
m=4: many sequences, e.g., R,R,R,R; or R,R,V,L; etc.
Seems for any m≥1, there is at least one sequence. But need to check if there is any restriction: from R you can go to R arbitrarily many times, so you can have all R's for any length. So sequence of all R's is always valid. Check: R type: left=1, right=0. For two adjacent R's: right_i=0, left_{i+1}=1, so 0≠1 okay. So all R's works. However, does all R's satisfy the blank count? It uses blank on right side for each piece? Actually R type: blank on right. So each piece has blank on right. For interior pieces (i not last), that blank faces the right neighbor, so used. For the rightmost piece, its blank is on right, which faces boundary, so unused. So total blanks used = m-1 (all except rightmost). That matches requirement. Also left side of each piece is tab, so leftmost piece's left is tab (boundary), okay. So all R's works for any m. So for single row, it's always possible.
But wait: For all R's, check edge condition: For edge between i and i+1: piece i (R) has right=blank, piece i+1 (R) has left=tab? Actually R: left=tab, right=blank. So right_i = blank, left_{i+1} = tab => blank vs tab, opposite, good. So yes.
Similarly, for single column (n rows, m=1), by symmetry, all pieces with blank on bottom? Actually we can rotate accordingly. So it's also possible.
Thus for n=1 or m=1, always YES.
Now for n=2,m=2: grid 2x2. We need to assign orientations. The counting condition gives (2-1)*(2-1)=1 ≤ 1, so possible. We need to check if indeed possible to satisfy all four internal edges? In 2x2, there are: horizontal edges: row1 between (1,1)-(1,2) and row2 between (2,1)-(2,2); vertical edges: column1 between (1,1)-(2,1) and column2 between (1,2)-(2,2). Total 4 internal edges. Each edge must be tab-blank.
We need to find orientations. Example given shows a solution. Let's verify with reasoning.
We can attempt to assign. Because (n,m)=(2,2) is small, we could brute conceptually. We want to see if always possible? It seems yes.
But is there any other case where (n-1)(m-1) ≤ 1 but still impossible? That would be (n,m) = (1, any) or (any,1) or (2,2). Also (n,m)=(1,1) trivial. Also (2,1) and (1,2) covered. So only other possibilities: (2,2) works, (1, any) works, (any,1) works. Also (0?) not allowed. So seems condition sufficient.
But before finalizing, we should also consider the case n=2,m=3? (n-1)(m-1)=1*2=2>1, so fails counting condition, so NO. That seems plausible.
But we must confirm that the counting argument (E ≤ nm) is indeed necessary, and that there are no additional hidden constraints that could make it impossible even when E ≤ nm. The counting argument is about total blanks used. However, it's not sufficient to guarantee existence, because the distribution of blanks among pieces may be constrained by adjacency conditions. So we need to prove sufficiency for the cases (n=1 or m=1 or (n,m)=(2,2)). For n=1 or m=1 we already gave constructive assignment: all pieces orient blank to the right (for row) or down (for column). But careful: for a single row, orientation blank on right works; for single column, orientation blank on bottom works. That yields all internal edges satisfied. So those are fine.
For (2,2) we need to show existence. Let's construct explicitly: Let's label cells (1,1) top-left, (1,2) top-right, (2,1) bottom-left, (2,2) bottom-right.
We need to assign orientations. Let's denote blank side per cell. I'll attempt to find one:
One solution from sample: perhaps they show. I can think: We need to satisfy four edges:
Edge between (1,1)-(1,2): horizontal top row.
Edge between (2,1)-(2,2): horizontal bottom row.
Edge between (1,1)-(2,1): vertical left column.
Edge between (1,2)-(2,2): vertical right column.
We need each edge to have one blank and one tab.
Consider using orientation assignments:
Let’s try: (1,1): blank on right (so right blank, left tab, top tab, bottom tab). (1,2): blank on left (so left blank, right tab, top tab, bottom tab). Then top row edge: (1,1) right blank, (1,2) left blank -> both blank! Not allowed. So can't have both blank on the sides facing each other.
Try: (1,1): blank on right (right blank), (1,2): blank on bottom (so left tab, right tab, top tab, bottom blank). Then top edge: (1,1) right blank, (1,2) left tab -> good. Now vertical edges: (1,1)-(2,1): (1,1) bottom tab, (2,1) top ? need opposite. We'll assign (2,1) accordingly. (1,2)-(2,2): (1,2) bottom blank, (2,2) top ? need opposite, so (2,2) top should be tab. Also bottom row edge: (2,1)-(2,2): need opposite.
Let's design systematically. Perhaps use a known pattern: Place pieces such that each interior edge has opposite. Since (2,2) is like a small cycle, there might be parity constraints. Let's treat it as a graph problem: each cell has four sides; we need to assign blank side such that for each edge, the two sides are opposite. This is equivalent to a kind of 2-coloring of directed edges? But we can just brute mentally.
We have four cells. Let variable for each cell: which side is blank: N,E,S,W.
Constraints:
- Top row: between (1,1) and (1,2): we need (E of (1,1)) and (W of (1,2)) opposite.
- Bottom row: between (2,1) and (2,2): (E of (2,1)) and (W of (2,2)) opposite.
- Left column: between (1,1) and (2,1): (S of (1,1)) and (N of (2,1)) opposite.
- Right column: between (1,2) and (2,2): (S of (1,2)) and (N of (2,2)) opposite.
We also have boundaries: no constraints.
We can try to assign. Suppose we set (1,1) blank on E. Then for top edge, (1,2) must have W=tab (since (1,1).E=blank, so (1,2).W must be tab). So (1,2) cannot have blank on W. So (1,2) blank is not W. It could be N, E, S.
Now consider left vertical: (1,1) blank on E implies (1,1).S is tab (since only one blank). So (2,1).N must be blank (to be opposite: tab vs blank). So (2,1) must have blank on N.
Now bottom row: (2,1) blank on N means (2,1).W and .E and .S are tabs. So (2,1).E is tab. Then (2,2).W must be blank? Because edge between (2,1) and (2,2) needs (2,1).E (tab) opposite (2,2).W, so (2,2).W must be blank. So (2,2) must have blank on W.
Now right vertical: (1,2) we haven't fixed. We have constraint from top edge: (1,2).W must be tab (since (1,1).E=blank). Also from right vertical: (1,2).S and (2,2).N must be opposite. (2,2) we set blank on W, so (2,2).N is tab (since only one blank, which is W). So (2,2).N = tab. Then (1,2).S must be blank? Because opposite: if (2,2).N=tab, then (1,2).S must be blank. So (1,2) must have blank on S. Check if (1,2) can have blank on S: yes, and then its W is tab (since blank on S means other sides are tabs). That matches the requirement (W=tab). Also (1,2).N and .E are tabs. So this assignment seems consistent.
Now check all constraints:
(1,1): blank=E -> E blank, others tabs.
(1,2): blank=S -> S blank, others tabs. So W tab, N tab, E tab, S blank.
(2,1): blank=N -> N blank, others tabs. So N blank, S tab, E tab, W tab.
(2,2): blank=W -> W blank, others tabs. So W blank, N tab, S tab, E tab.
Now verify each edge:
Top: (1,1).E blank, (1,2).W tab -> ok.
Bottom: (2,1).E tab, (2,2).W blank -> ok.
Left: (1,1).S tab, (2,1).N blank -> ok.
Right: (1,2).S blank, (2,2).N tab -> ok.
All satisfied. So (2,2) works.
Thus (2,2) is possible.
Now, are there any other cases where (n-1)(m-1) ≤ 1? That gives n=1, m=any; m=1, n=any; (2,2). Also (n=2,m=1) and (1,2) are covered by n=1 or m=1. Also (n=2,m=2) as shown. Also (n=1,m=1) trivial. So all such cases are YES.
But wait: (n,m) = (1,2) we said works; (2,1) works similarly.
Thus the condition seems both necessary and sufficient.
But is there any possibility for (n,m) = (2,3) to satisfy? Let's test counting: (2-1)(3-1)=12=2>1, so necessary condition fails, so NO. But could there be some clever arrangement that uses some blanks on boundary to reduce used blanks? Actually the counting argument is: total blanks available = nm. Each internal edge requires exactly one blank to be used (on one of the two sides). So total blanks used = number of internal edges = E. But each blank can only be used if the piece's blank side faces a neighbor. However, a blank side could be used on two edges? No, each side of a piece corresponds to exactly one edge (if internal) or boundary. So each blank is used at most once. So indeed we need E ≤ nm. That gave (n-1)(m-1) ≤ 1. That is a solid necessary condition. So if that inequality is false, impossible. So it's necessary.
Thus answer: YES if (n-1)*(m-1) ≤ 1 else NO.
Check sample: (1,3) -> (0)(2)=0 ≤1 -> YES; (100000,100000) -> (99999)(99999) huge >1 -> NO; (2,2) -> (1)*(1)=1 ≤1 -> YES.
Edge cases: n=1,m=10^5 -> YES; n=10^5,m=1 -> YES; n=2,m=1 -> YES; n=1,m=1 -> YES.
Thus solution is simple.
But wait: The puzzle piece has three tabs and one blank. Could the piece be rotated such that the blank is on a side, but also the shape might have orientation constraints beyond just tabs/blanks? Usually in jigsaw puzzles, tabs and blanks interlock; but the problem statement says: "Every piece has three tabs and one blank, as pictured below." The picture probably shows a typical piece with three protrusions and one indentation. Since pieces are identical, they can be rotated. The only condition is that for any two adjacent pieces, a tab of one fits into a blank of the other. So it's exactly the condition we used.
Thus the solution stands.
But before finalizing, let's double-check the counting argument carefully.
Define grid with n rows, m columns. Each piece is a square with four sides. Exactly one side is blank (indentation), the other three are tabs (protrusions). Adjacent pieces must interlock: the sides that touch must be a tab on one and a blank on the other. So for each edge, we need one blank and one tab.
Now, consider all internal edges. Count number of blanks that are used (i.e., blank sides that are adjacent to another piece). Since each such blank is used on exactly one edge, and each edge requires exactly one blank, the total number of used blanks equals the number of internal edges, E.
But each piece has exactly one blank side. However, that blank side may be on the boundary (facing outside) and thus not used. So the maximum number of blanks that can be used is at most the number of pieces, but only if all pieces have their blank facing a neighbor. However, is it possible for a piece to have its blank side used on two edges? No, because a piece only has one blank side, so it can only be adjacent to at most one neighbor via that blank side. So each piece contributes at most 1 used blank. Therefore, total used blanks ≤ nm. So we need E ≤ nm.
Similarly, we could also consider tabs used: each edge also requires one tab. Each piece has three tabs, and a tab side can be used if it faces a neighbor. So maximum total tabs used is at most 3nm. That yields E ≤ 3nm, which is always true for positive n,m. So only the blank constraint matters.
Now compute E = n(m-1) + (n-1)m = 2nm - n - m. The inequality E ≤ nm becomes 2nm - n - m ≤ nm → nm - n - m ≤ 0 → (n-1)(m-1) ≤ 1.
Thus necessary.
But wait, is it possible that a blank side could be used on two edges? That would require that the blank side is adjacent to two pieces, but in a grid, each side of a cell is adjacent to exactly one neighboring cell (if any). So no.
Thus condition is necessary.
Now sufficiency: For cases where (n-1)(m-1) ≤ 1, we must show a construction exists. We've given constructions for row/column and 2x2. But need to ensure that for n=1 or m=1, the construction with all blanks facing right (or down) works indeed. Let's formalize:
Case 1: n = 1 (single row). Set each piece such that blank is on the right side. For the rightmost piece, blank on right is on boundary (unused). For any adjacent pair (i,i+1): piece i has right blank, piece i+1 has left tab (since its blank is on right, left is tab). So condition satisfied. So YES.
Similarly, m = 1 (single column). Set each piece blank on bottom. Works.
Case 2: n = m = 2. Provide explicit orientation as above or any. So YES.
Also note: Could there be case n=2,m=1? That's covered by m=1 case. Similarly n=1,m=2 covered.
Thus answer: YES if (n==1 || m==1 || (n==2 && m==2)) else NO? But careful: (n-1)(m-1) ≤ 1 also includes the possibility n=0? but n,m ≥ 1. So condition reduces to: either n=1 or m=1 or (n=2 and m=2). Because (n-1)(m-1) = 0 if either n=1 or m=1; =1 if n=2 and m=2. Are there any other integer pairs giving (n-1)(m-1)=1? That would require both n-1=1 and m-1=1, so n=2,m=2; or one factor =1 and the other=1; also could have n-1 = -1? but n,m positive. So only (2,2). Also (n-1)(m-1) could be 0 if either factor 0, so n=1 or m=1. So indeed condition is exactly that.
But wait: Could (n-1)(m-1)=1 also occur if n-1= -1 and m-1= -1? Not possible.
Thus answer: YES if n == 1 || m == 1 || (n == 2 && m == 2) else NO.
But is there any possibility for n=2,m=2? Already handled.
Thus solution is straightforward.
Double-check with more examples: n=2,m=3: (12=2>1) -> NO. Could there be a solution? Let's test if maybe we can orient such that some blanks are unused and others used multiple times? Not possible. The counting says we would need E=223 -2 -3 =12-5=7 internal edges. We have 23=6 pieces, so at most 6 blanks can be used, but need 7, impossible. So NO. So correct.
What about n=3,m=3: (22=4>1) -> NO. E=29-3-3=18-6=12 >9 -> impossible.
Edge: n=1,m=1: E=0 ≤1 -> YES. Our condition yields YES.
Thus solution.
Now, we should consider if there is any other scenario like n=2,m=2 we already did. But what about n=2,m=2 also satisfying the inequality. Good.
Thus answer for each test case: print "YES" if (n==1 or m==1 or (n==2 and m==2)) else "NO".
But note: The problem constraints allow n,m up to 1e5, t up to 1000. So O(1) per test case.
But wait: There is also the possibility of n=2,m=1? That's n=1? Actually n=2,m=1 => m=1 so it's covered by m=1 condition. So formula holds.
Thus we can implement.
But before finalizing, let's read the problem statement again carefully: "The jigsaw puzzle is considered solved if the following conditions hold: 1. The pieces are arranged into a grid with n rows and m columns. 2. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece." So it's exactly what we used.
One might wonder about the shape: maybe the pieces are not square but have tabs on edges; but it's typical. Also they said "three tabs and one blank" which we used.
Now, confirm with the note: For first test case (1,3) they show an example solution; second (100000,100000) no; third (2,2) yes. So consistent.
Thus answer: YES if (n-1)*(m-1) <= 1 else NO.
But is there any other nuance? For n=1,m=2, the condition holds. Could there be a scenario where n=1,m=2 but pieces cannot be oriented because they need to also satisfy something else? We already gave all pieces blank on right: piece1 blank right, piece2 blank right? Actually for m=2, we need edge between them: piece1 right blank, piece2 left must be tab. If piece2 blank on right, then left is tab (since blank on right gives left tab). So works. So yes.
But what about n=1,m=1? No edges, any orientation works.
Thus answer is correct.
Now, we should also consider the possibility of n=2,m=2 but we must also consider that the pieces have three tabs, one blank, but maybe the shape is such that a blank cannot be placed on a side that is adjacent to another blank? That's already the condition. Our construction uses blanks on E, S, N, W respectively. So all four sides are used as blanks somewhere. That's fine because each piece has one blank.
Thus solution.
Implementation: read t, loop, read n,m, if (n==1 or m==1 or (n==2 and m==2)) print "YES" else "NO".
But careful: (n==2 && m==2) is included. Also (n==1 || m==1) includes (2,1) etc.
But is there any case where n=2,m=2 fails due to parity? We already constructed one. So good.
Double-check with the counting: For n=2,m=2, E=4, n*m=4, so exactly equal. So we need every piece's blank to be used (i.e., each blank faces a neighbor). In our construction, (1,1) blank on E (faces right neighbor), (1,2) blank on S (faces bottom neighbor), (2,1) blank on N (faces top neighbor), (2,2) blank on W (faces left neighbor). So all blanks are used. So indeed we use all blanks.
Thus the condition E <= nm is necessary and also when equality holds (E = nm), we need every blank used, which may impose additional constraints. For n=2,m=2 equality holds; we found a configuration. For n=1,m arbitrary, E = m-1, while n*m = m. So we need only m-1 blanks used, so one blank can be unused. That's possible as we saw with all blanks on right (rightmost unused). So works. For n=1,m=1, E=0, no blanks needed, works.
What about n=1,m=0? Not possible.
Thus answer is clear.
Now, one more thought: Could there be a case where (n-1)(m-1)=0 but n>1 and m>1? That would require either n=1 or m=1. So no.
Thus solution.
But we must ensure that the puzzle pieces are identical and we can rotate them arbitrarily. Yes.
Thus final answer: For each test case, output YES if (n == 1 || m == 1 || (n == 2 && m == 2)) else NO.
Alternatively, compute (n-1)*(m-1) <= 1.
Now implement.
Edge Cases: Large numbers, but simple.
Time complexity O(t).
Now, let's produce final answer.
But wait: The problem might have more nuance: The pieces are "identical" meaning they all have three tabs and one blank in the same arrangement? But does the arrangement of tabs and blanks matter beyond which side is blank? Since pieces are symmetric under rotation except for the location of the blank, the exact pattern of tabs on the other three sides might be fixed relative to the blank? Usually a puzzle piece's tabs are on specific sides relative to the blank? The piece has a specific shape: three tabs and one blank, but the tabs are not all identical? They are just tabs, so only the presence of tab or blank matters, not which specific tab. So orientation is just choosing which side gets the blank. So good.
Now, check if any tricky case: n=2,m=3 fails. But what if we try to use some blanks on boundary to free up? No, the counting argument says total blanks used must be at least E, and we only have nm blanks. So if E > nm, impossible. So indeed.
Thus final solution.
But before finalizing, I recall a known Codeforces problem: "Jigsaw Puzzle" maybe from CF Round. Actually I think this is problem "1365A" or something? Not sure. But indeed answer is that.
Let's also consider n=2,m=2 already done. Could there be n=2,m=2 but pieces cannot be oriented because they might need to have the blank on a specific side to satisfy both horizontal and vertical constraints simultaneously? We found one. So fine.
Thus solution is correct.
Now I'll produce final answer in the required format: For each test case, output "YES" or "NO".
Implementation in Python:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
t = int(next(it))
out = []
for _ in range(t):
n = int(next(it)); m = int(next(it))
if (n-1)*(m-1) <= 1:
out.append("YES")
else:
out.append("NO")
sys.stdout.write("\n".join(out))
That's it.
But let's test with sample:
t=3:
1 3: (02=0) YES
100000 100000: (9999999999=9999800001) >1 NO
2 2: (1*1=1) YES
Matches.
Thus final answer.
We should also note that for n=2,m=1: (1*0=0) YES; that makes sense: single column of 2 pieces: vertical edge between them. We can set both pieces with blank on bottom? Let's test: For column of 2, we need one edge. If we set both blanks on bottom: top piece bottom blank, bottom piece top? bottom piece blank on bottom, so top is tab? Actually bottom piece blank on bottom => top side is tab. Then edge: top piece bottom blank, bottom piece top tab -> OK. So works.
Thus all good.
Now produce final answer. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
t = int(next(it))
out_lines = []
for _ in range(t):
n = int(next(it))
m = int(next(it))
# necessary and sufficient condition:
if (n - 1) * (m - 1) <= 1:
out_lines.append("YES")
else:
out_lines.append("NO")
sys.stdout.write("\n".join(out_lines))
if name == "main":
solve()