Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int main() {
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- int n; cin >> n;
- vector<int> g[2][2]; // bipartate graph between rows and columns
- /* g[i][j][k] specifies edges in the following way:
- i = 0 when the edge is from row to column and 1 when vice versa
- j = 0 when edge is an alpha and 1 when edge is a beta
- k = the index of the col/row connected to this row/col by this edge
- */
- for (int i = 0; i < 2; ++i) {
- for (int j = 0; j < 2; ++j) g[i][j].resize(n);
- }
- for (int i = 0; i < n; ++i) {
- int x, y; cin >> x >> y;
- g[1][0][x] = y;
- g[0][0][y] = x;
- }
- for (int i = 0; i < n; ++i) {
- int x, y; cin >> x >> y;
- g[1][1][x] = y;
- g[0][1][y] = x;
- }
- vector<int> p[2];
- p[0].resize(n), p[1].resize(n);
- for (int i = 0; i < n; ++i) p[0][i] = p[1][i] = i;
- vector<bool> seenRow(n);
- for (int i = 0; i < n; ++i) {
- if (seenRow[i]) continue;
- int a = i, b = i, side = 0; // side = 0 when on rows and 1 when on columns
- do {
- if (side == 0) seenRow[a] = seenRow[b] = true;
- swap(p[side][a], p[side][b]);
- a = g[side][1][a];
- b = g[side][0][b];
- swap(a, b);
- side = 1-side;
- } while (a != b);
- seenRow[a] = seenRow[b] = true;
- }
- for (int &x : p[0]) cout << x << ' ';
- cout << endl;
- for (int &x : p[1]) cout << x << ' ';
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement