Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <queue>
- using namespace std;
- using vi = vector<int>;
- bool isSame(vector<vi> a, vector<vi> b) {
- sort(a.begin(), a.end());
- sort(b.begin(), b.end());
- return a == b;
- }
- int main() {
- ios::sync_with_stdio(0); cin.tie(0);
- int n, m; cin >> n >> m;
- vector a(n, vi(m)); for (auto& v : a) for (int& x : v) cin >> x;
- vector b(n, vi(m)); for (auto& v : b) for (int& x : v) cin >> x;
- vector<int> inv(m), act;
- for (int i = 0; i < m; ++i) {
- for (int j = 1; j < n; ++j) {
- if (b[j][i] < b[j - 1][i]) ++inv[i];
- }
- }
- int ans = -1;
- vector<int> cut(n - 1);
- queue<int> q;
- for (int i = 0; i < m; ++i) if (!inv[i]) q.push(i);
- while (!q.empty()) {
- int v = q.front(); q.pop();
- act.push_back(v);
- for (int i = 1; i < n; ++i) {
- if (b[i][v] > b[i - 1][v] && !cut[i - 1]) {
- cut[i - 1] = 1;
- for (int j = 0; j < m; ++j) {
- if (b[i][j] < b[i - 1][j]) {
- --inv[j];
- if (inv[j] == 0) {
- q.push(j);
- }
- }
- }
- }
- }
- }
- reverse(act.begin(), act.end());
- for (int j : act) {
- stable_sort(a.begin(), a.end(), [&](auto& l, auto& r) {
- return l[j] < r[j];
- });
- }
- if (a == b) ans = act.size();
- cout << ans << endl;
- if (ans != -1) {
- for (int i : act) cout << i + 1 << ' ';
- cout << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement