Grid Shading Puzzle
Time limit1sMemory limit128 MB
Given per-row and per-column shaded counts for an n by n board, decide whether a valid 0/1 board exists.
Problem
You want to solve a puzzle printed in a newspaper.
You are given a board divided into unit squares. Each square may be shaded or left blank. For every row and every column you are told the exact number of squares that must be shaded.
Decide whether the board can be shaded so that all of the given per-row and per-column counts are satisfied.
Input
The first line contains the number of test cases (). The descriptions of the test cases follow.
The first line of each test case contains the board size (). The second line contains integers and the third line contains integers (). Here is the number of squares that must be shaded in row , and is the number of squares that must be shaded in column .
You may assume that the sum of over all test cases in a single input does not exceed .
Output
For each test case, print a single line containing TAK if the puzzle can be solved, or NIE otherwise.