Escape Room

시간 제한1초메모리 제한256 MB

요약
모든 열쇠 부분집합마다 전체 연결 여부가 주어질 때, 그 패턴을 정확히 만족하는 사이트 300개 이하의 미로를 만들거나 불가능함을 판정한다.
난이도

어려움10점 중 9점

유형
그래프, 조합론, 구현, 수학
정답자
아직 제출이 없습니다

문제

Busy Beaver is designing an escape room! His current design is a maze with NN different sites, where each of the N(N−1)2\frac{N(N-1)}{2} pairs of sites is directly connected by a bidirectional tunnel.

To make the maze more interesting, each tunnel is locked with one of KK (1≤K≤101 \leq K \leq 10) keys, numbered 1,2,…,K1, 2, \dots, K. One can only traverse a tunnel between sites ii and jj if they have its corresponding key a_ija\_{ij}.

Additionally, Busy Beaver wants to design the maze such that only certain sets of keys let you traverse the entire maze. In particular, there are x_S∈0,1x\_S \in \\{0, 1\\} for each subset S⊆1,2,…,KS \subseteq \\{1, 2, \dots, K\\} such that

  • If x_S=1x\_S = 1, it is possible to move between any two sites in the maze using only the keys in SS.
  • If x_S=0x\_S = 0, there exists some pair of sites that cannot be accessed from each other using only the keys in SS.

Decide whether or not the task is possible. Additionally, if the task is possible, provide any valid construction with at most 300300 sites.

입력

Each test contains multiple test cases. The first line contains the number of test cases TT (1≤T≤3001 \leq T \leq 300). The description of the test cases follows.

The first line of each test case contains the integer KK (1≤K≤101 \le K \le 10) --- the number of keys.

The second line of each test case contains a string xx (∣x∣=2K|x| = 2^K, x_i∈0,1x\_i \in \\{0, 1\\}) such that for each SS, if i=∑_t∈S2t−1i = \sum\_{t \in S} 2^{t - 1} then x_S:=x_ix\_S := x\_i. Note that the string xx is zero-indexed, that is, 0≤i<2K0 \leq i < 2^K.

It is guaranteed that the sum of 2K2^K across all test cases is no more than 2102^{10}.

출력

For each test case, if it is possible to satisfy the given constraints, the first line of output should contain an integer NN (1≤N≤3001 \leq N \leq 300) --- the number of sites. The ii-th of the next NN lines of output should then contain NN integers a_ija\_{ij} (a_ii=0,a_ij=a_ji,1≤a_ij≤Ka\_{ii} = 0, a\_{ij} = a\_{ji}, 1 \leq a\_{ij} \leq K for i≠ji \neq j), where for i≠ji \neq j, the tunnel between sites ii and jj uses key a_ija\_{ij}.

If there are multiple solutions, print any of them. Otherwise, if there is no solution, print a single integer −1-1 instead.

Due to judging constraints, the sum of N2N^2 over your outputs should not exceed 2⋅1052 \cdot 10^5. It can be shown that this is enough to solve the problem.

힌트

In the first test case, it can be shown that it is impossible to construct the desired maze.

In the second test case, a possible construction is a maze with 22 sites connected by a tunnel using key 11.

  • With keys S=∅S = \varnothing corresponding to x_0=‘0‘x\_0 = `0`, it is impossible to traverse the entire maze.
  • With keys S=1S = \\{1\\} corresponding to x_1=‘1‘x\_1 = `1`, it is possible to traverse the entire maze.
  • With keys S=2S = \\{2\\} corresponding to x_2=‘0‘x\_2 = `0`, it is impossible to traverse the entire maze.
  • With keys S=1,2S = \\{1, 2\\} corresponding to x_3=‘1‘x\_3 = `1`, it is possible to traverse the entire maze.

In the third test case, it can be shown that it is impossible to construct the desired maze.

In the fourth test case, a possible construction is the following maze with 44 sites:

In the fifth test case, a possible construction is a maze with only 11 site. With any set of keys, it is possible to traverse the entire maze.

예제1

  1. 예제 1

    입력
    5
    1
    00
    2
    0101
    2
    0110
    3
    00011111
    4
    1111111111111111
    
    예상 출력
    -1
    2
    0 1
    1 0
    -1
    4
    0 1 3 3
    1 0 2 3
    3 2 0 1
    3 3 1 0
    1
    0