#include using namespace std; #ifdef LOCAL #include "debug.h" #else #define DEBUG(...) #endif template class graph { public: struct edge { int from; int to; T cost; }; vector edges; vector< vector > g; int n; function ignore; graph(int _n) : n(_n) { g.resize(n); ignore = nullptr; } virtual int add(int from, int to, T cost) = 0; virtual void set_ignore_edge_rule(const function &f) { ignore = f; } virtual void reset_ignore_edge_rule() { ignore = nullptr; } }; template class digraph : public graph { public: using graph::edges; using graph::g; using graph::n; using graph::ignore; digraph(int _n) : graph(_n) { } int add(int from, int to, T cost = 1) { assert(0 <= from && from < n && 0 <= to && to < n); int id = (int) edges.size(); g[from].push_back(id); edges.push_back({from, to, cost}); return id; } digraph reverse() const { digraph rev(n); for (auto &e : edges) { rev.add(e.to, e.from, e.cost); } if (ignore != nullptr) { rev.set_ignore_edge_rule([&](int id) { return ignore(id); }); } return rev; } }; template vector find_scc(const digraph &g, int &cnt) { digraph g_rev = g.reverse(); vector order; vector was(g.n, false); function dfs1 = [&](int v) { was[v] = true; for (int id : g.g[v]) { if (g.ignore != nullptr && g.ignore(id)) { continue; } auto &e = g.edges[id]; int to = e.to; if (!was[to]) { dfs1(to); } } order.push_back(v); }; for (int i = 0; i < g.n; i++) { if (!was[i]) { dfs1(i); } } vector c(g.n, -1); function dfs2 = [&](int v) { for (int id : g_rev.g[v]) { if (g_rev.ignore != nullptr && g_rev.ignore(id)) { continue; } auto &e = g_rev.edges[id]; int to = e.to; if (c[to] == -1) { c[to] = c[v]; dfs2(to); } } }; cnt = 0; for (int id = g.n - 1; id >= 0; id--) { int i = order[id]; if (c[i] != -1) { continue; } c[i] = cnt++; dfs2(i); } return c; // c[i] <= c[j] for every edge i -> j } class twosat { public: digraph g; int n; twosat(int _n) : g(digraph(2 * _n)), n(_n) { } inline void add(int x, int value_x) { // (v[x] == value_x) assert(0 <= x && x < n); assert(0 <= value_x && value_x <= 1); g.add(2 * x + (value_x ^ 1), 2 * x + value_x); } inline void add(int x, int value_x, int y, int value_y) { // (v[x] == value_x || v[y] == value_y) assert(0 <= x && x < n && 0 <= y && y < n); assert(0 <= value_x && value_x <= 1 && 0 <= value_y && value_y <= 1); g.add(2 * x + (value_x ^ 1), 2 * y + value_y); g.add(2 * y + (value_y ^ 1), 2 * x + value_x); } inline vector solve() { int cnt; vector c = find_scc(g, cnt); vector res(n); for (int i = 0; i < n; i++) { if (c[2 * i] == c[2 * i + 1]) { return vector(); } res[i] = (c[2 * i] < c[2 * i + 1]); } return res; } }; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin >> n; vector s(n), t(n), u(n); for (auto&& e : s) cin >> e, --e; for (auto&& e : t) cin >> e, --e; for (auto&& e : u) cin >> e; auto _ = [&](int i, int j) { return i * n + j; }; twosat ts(n * n); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (u[i] == 0) { ts.add(_(s[i], j), 1, _(j, t[i]), 1); } else if (u[i] == 1) { ts.add(_(s[i], j), 0, _(j, t[i]), 1); } else if (u[i] == 2) { ts.add(_(s[i], j), 1, _(j, t[i]), 0); } else { ts.add(_(s[i], j), 0, _(j, t[i]), 0); } } } auto res = ts.solve(); if (empty(res)) { cout << "-1\n"; } else { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cout << res[_(i, j)] << " \n"[j == n - 1]; } } } }