time limit per test 2 seconds memory limit per test 256 megabytes
Farmer John has n arrays a1,a2,…,an that may have different lengths. He will stack the arrays on top of each other, resulting in a grid with n rows. The arrays are left-aligned and can be stacked in any order he desires.
Then, gravity will take place. Any cell that is not on the bottom row and does not have an element beneath it will fall down a row. This process will be repeated until there are no cells that satisfy the aforementioned constraint.
Over all possible ways FJ can stack the arrays, output the lexicographically minimum bottom row after gravity takes place.
Input
The first line contains t (1≤t≤1000) — the number of test cases.
The first line of each test case contains n (1≤n≤2⋅105).
For the following n lines, the first integer ki (1≤k≤2⋅105) denotes the length of ai.
Then, ki space-separated integers ai1,ai2,…,aiki follow (1≤aij≤2⋅105).
It is guaranteed that the sum of n and the sum of all ki over all test cases does not exceed 2⋅105.
Output
For each test case, output the lexicographically minimum bottom row on a new line.
int T; if (!(cin >> T)) return0; while (T--) { int n; cin >> n; vector<vector<int>> a(n); int L = 0; // 最大长度 for (int i = 0; i < n; ++i) { int k; cin >> k; a[i].resize(k); for (int j = 0; j < k; ++j) cin >> a[i][j]; L = max(L, k); }
// parts[j]: 能到达第 j 列的行 vector<vector<int>> parts(L); for (int i = 0; i < n; ++i) for (int j = 0; j < (int)a[i].size(); ++j) parts[j].push_back(i);
// ranks[i][j]: 第 i 行从 j 开始后缀的名次;越界 ranks[i][len]=0 vector<vector<int>> ranks(n); for (int i = 0; i < n; ++i) ranks[i].assign((int)a[i].size() + 1, 0);