#P2055. [ZJOI2009] 假期的宿舍

    ID: 997 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>2009各省省选网络流浙江二分图最大流

[ZJOI2009] 假期的宿舍

Description

The school is on vacation. Some students go home, while some of their old friends come to visit, so accommodation becomes a problem.

For example, A and B are both students of the school. A is going home, and C comes to visit B. C and A do not know each other. We assume each person can only sleep in the bed of someone they know directly. Then one solution is: B sleeps in A’s bed, and C sleeps in B’s bed. In reality, things can be much more complicated: some people may know many students who are on campus, and students on campus may not all know each other.

We know there are nn people in total, and for each person we know whether they are a student of this school, and for each student we know whether they will go home. Determine whether there exists an arrangement such that all students who do not go home and all the other people who come to visit them have a place to stay.

Input Format

The first line contains an integer TT denoting the number of test cases. Then follow TT test cases. For each test case, the first line contains an integer nn denoting the total number of people involved.

The next line contains nn numbers; the ii-th number indicates whether person ii is a student of the school (00 means no, 11 means yes). The next line contains nn numbers; the ii-th number indicates whether person ii will go home (00 means not going home, 11 means going home; note that if person ii is not a student, the number at this position is random and should be ignored after reading).

Then follow nn lines, each with nn numbers. The number in row ii, column jj indicates whether ii and jj know each other (11 means yes, 00 means no; the value at row ii, column ii is 00, but of course everyone can still sleep in their own bed). The acquaintance relationship is mutual.

Output Format

For each test case, output ^_^ if a valid arrangement exists; otherwise, output T_T. Note that the outputs must be half-width characters (ASCII).

1
3
1 1 0
0 1 0
0 1 1
1 0 0
1 0 0
^_^

Hint

For 30%30\% of the testdata, 1n121 \le n \le 12.

For 100%100\% of the testdata, 1n501 \le n \le 50, 1T201 \le T \le 20.

Translated by ChatGPT 5