#P4249. [WC2007] 剪刀石头布

    ID: 3178 远端评测题 2000ms 500MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>图论2007WC/CTSC/集训队Special JudgeO2优化图的建立,建图费用流差分

[WC2007] 剪刀石头布

Description

In some one-on-one game competitions (such as chess, table tennis, and badminton singles), we often encounter the interesting situation where AA beats BB, BB beats CC, and CC beats AA. Let us vividly call this the rock-paper-scissors situation. Sometimes, people will enthusiastically count how many such rock-paper-scissors situations occur, that is, how many unordered triples (A,B,C)(A, B, C) satisfy that one person beats another, the second beats the third, and the third beats the first. Note that unordered means the order of elements in the triple does not matter, and (A,B,C)(A, B, C), (A,C,B)(A, C, B), (B,A,C)(B, A, C), (B,C,A)(B, C, A), (C,A,B)(C, A, B), and (C,B,A)(C, B, A) are regarded as the same situation.

There are NN people participating in such a competition. The schedule requires that every pair of people plays exactly one match, so there are a total of N(N1)2\frac{N*(N-1)}{2} matches. Some matches have already been played. We want to know, in the extreme case, what is the maximum possible number of rock-paper-scissors situations after all matches are completed. That is, given the results of the matches that have already taken place, you may assign the outcomes of the remaining matches arbitrarily to obtain as many rock-paper-scissors situations as possible.

Input Format

The first line contains an integer NN, the number of participants.

Then follows an NN-by-NN numeric matrix: NN rows, each with NN columns, and numbers separated by spaces.

In row (i+1)(i+1) and column jj, if the number is 11, it means ii has already beaten jj; if the number is 00, it means ii has already lost to jj; if the number is 22, it means the match between ii and jj has not yet taken place. The numbers on the diagonal, i.e., the number in row (i+1)(i+1) and column ii, are all 00. They are merely placeholders and have no meaning.

The input is guaranteed to be valid and without contradictions. When iji \neq j, the two numbers at row (i+1)(i+1), column jj and row (j+1)(j+1), column ii are either both 22, or one is 00 and the other is 11.

Output Format

The first line contains an integer, indicating how many rock-paper-scissors situations occur in your arranged set of match results.

Starting from line 22, output an NN-by-NN numeric matrix in the same format as the input. The number in row (i+1)(i+1) and column jj describes the result of the match between ii and jj: 11 means ii beats jj, and 00 means ii loses to jj. Unlike the input matrix, there is no number 22 indicating an unplayed match; the diagonal numbers are all 00. The output matrix must be valid and free of contradictions.

3
0 1 2
0 0 2
2 2 0

1
0 1 0
0 0 1
1 0 0

Hint

  • Scoring criteria: For each test point, you will receive full credit only if the first line of your output matches the standard answer and you provide a valid arrangement consistent with it. Otherwise, you will receive 00 points for that test point.
  • Constraints: For 30%30\% of the testdata, N6N \leq 6. For 100%100\% of the testdata, N100N \leq 100.

Translated by ChatGPT 5