1+ package p1790 ;
2+
3+ import java .nio .charset .StandardCharsets ;
4+ import java .util .Arrays ;
5+ import java .util .HashMap ;
6+ import java .util .Map ;
7+ import java .util .Scanner ;
8+ import java .util .stream .Collectors ;
9+
10+ public class CF1790C {
11+ public static void main (String [] args ) {
12+ Scanner scanner = new Scanner (System .in , StandardCharsets .UTF_8 );
13+ int t = scanner .nextInt ();
14+ while (t -- > 0 ) {
15+ int n = scanner .nextInt ();
16+ int [][] p = new int [n ][n - 1 ];
17+ for (int i = 0 ; i < n ; i ++) {
18+ for (int j = 0 ; j < n - 1 ; j ++) {
19+ p [i ][j ] = scanner .nextInt ();
20+ }
21+ }
22+ System .out .println (solve (n , p ));
23+ }
24+ }
25+
26+ private static String solve (int n , int [][] p ) {
27+ Map <Integer , Integer > cntMap = new HashMap <>();
28+ for (int i = 0 ; i < n ; i ++) {
29+ int x = p [i ][0 ];
30+ cntMap .put (x , cntMap .getOrDefault (x , 0 ) + 1 );
31+ }
32+ int first = -1 ;
33+ int sec = -1 ;
34+ for (Map .Entry <Integer , Integer > entry : cntMap .entrySet ()) {
35+ if (entry .getValue () > 1 ) {
36+ first = entry .getKey ();
37+ } else {
38+ sec = entry .getKey ();
39+ }
40+ }
41+ int [] res = new int [n ];
42+ res [0 ] = first ;
43+ for (int i = 0 ; i < n ; i ++) {
44+ int x = p [i ][0 ];
45+ if (x == sec ) {
46+ System .arraycopy (p [i ], 0 , res , 1 , n - 1 );
47+ break ;
48+ }
49+ }
50+ return Arrays .stream (res ).mapToObj (String ::valueOf ).collect (Collectors .joining (" " ));
51+ }
52+ }
53+ /*
54+ C. Premutation
55+ https://codeforces.com/contest/1790/problem/C
56+
57+ 题目大意:
58+ 给定整数 n 和 n 行,每行 n-1 个数,每行跳过了元素 pi,pi 不重复,不知道它们被写的顺序,要求重建原来的序列
59+
60+ 首位必定出现 n-1 次,后面加上一个仅出现 1 次的序列即可。
61+ ======
62+
63+ input
64+ 5
65+ 4
66+ 4 2 1
67+ 4 2 3
68+ 2 1 3
69+ 4 1 3
70+ 3
71+ 2 3
72+ 1 3
73+ 1 2
74+ 5
75+ 4 2 1 3
76+ 2 1 3 5
77+ 4 2 3 5
78+ 4 1 3 5
79+ 4 2 1 5
80+ 4
81+ 2 3 4
82+ 1 3 4
83+ 1 2 3
84+ 1 2 4
85+ 3
86+ 2 1
87+ 1 3
88+ 2 3
89+
90+ output
91+ 4 2 1 3
92+ 1 2 3
93+ 4 2 1 3 5
94+ 1 2 3 4
95+ 2 1 3
96+ */
0 commit comments