結果
問題 | No.1078 I love Matrix Construction |
ユーザー | yosupot |
提出日時 | 2020-06-12 21:29:44 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 243 ms / 2,000 ms |
コード長 | 8,662 bytes |
コンパイル時間 | 1,868 ms |
コンパイル使用メモリ | 134,764 KB |
実行使用メモリ | 54,456 KB |
最終ジャッジ日時 | 2024-06-24 04:28:28 |
合計ジャッジ時間 | 6,380 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,944 KB |
testcase_02 | AC | 27 ms
12,120 KB |
testcase_03 | AC | 80 ms
24,256 KB |
testcase_04 | AC | 112 ms
29,792 KB |
testcase_05 | AC | 84 ms
26,560 KB |
testcase_06 | AC | 25 ms
10,256 KB |
testcase_07 | AC | 10 ms
6,944 KB |
testcase_08 | AC | 90 ms
26,860 KB |
testcase_09 | AC | 5 ms
6,944 KB |
testcase_10 | AC | 243 ms
54,456 KB |
testcase_11 | AC | 122 ms
31,604 KB |
testcase_12 | AC | 198 ms
46,452 KB |
testcase_13 | AC | 222 ms
51,512 KB |
testcase_14 | AC | 145 ms
42,004 KB |
testcase_15 | AC | 212 ms
48,836 KB |
testcase_16 | AC | 8 ms
6,944 KB |
testcase_17 | AC | 2 ms
6,944 KB |
testcase_18 | AC | 18 ms
8,536 KB |
testcase_19 | AC | 45 ms
15,452 KB |
testcase_20 | AC | 49 ms
15,284 KB |
testcase_21 | AC | 3 ms
6,944 KB |
ソースコード
//#pragma GCC optimize("Ofast") //#pragma GCC target("avx") //#undef LOCAL #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <complex> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); } template <class T> using V = vector<T>; template <class T> using VV = V<V<T>>; #include <unistd.h> struct Scanner { int fd = -1; char line[(1 << 15) + 1]; size_t st = 0, ed = 0; void reread() { memmove(line, line + st, ed - st); ed -= st; st = 0; ed += ::read(fd, line + ed, (1 << 15) - ed); line[ed] = '\0'; } bool succ() { while (true) { if (st == ed) { reread(); if (st == ed) return false; } while (st != ed && isspace(line[st])) st++; if (st != ed) break; } if (ed - st <= 50) { bool sep = false; for (size_t i = st; i < ed; i++) { if (isspace(line[i])) { sep = true; break; } } if (!sep) reread(); } return true; } template <class T, enable_if_t<is_same<T, string>::value, int> = 0> bool read_single(T& ref) { if (!succ()) return false; while (true) { size_t sz = 0; while (st + sz < ed && !isspace(line[st + sz])) sz++; ref.append(line + st, sz); st += sz; if (!sz || st != ed) break; reread(); } return true; } template <class T, enable_if_t<is_integral<T>::value, int> = 0> bool read_single(T& ref) { if (!succ()) return false; bool neg = false; if (line[st] == '-') { neg = true; st++; } ref = T(0); while (isdigit(line[st])) { ref = 10 * ref + (line[st++] & 0xf); } if (neg) ref = -ref; return true; } template <class T> bool read_single(V<T>& ref) { for (auto& d : ref) { if (!read_single(d)) return false; } return true; } void read() {} template <class H, class... T> void read(H& h, T&... t) { bool f = read_single(h); assert(f); read(t...); } Scanner(FILE* fp) : fd(fileno(fp)) {} }; struct Printer { public: template <bool F = false> void write() {} template <bool F = false, class H, class... T> void write(const H& h, const T&... t) { if (F) write_single(' '); write_single(h); write<true>(t...); } template <class... T> void writeln(const T&... t) { write(t...); write_single('\n'); } Printer(FILE* _fp) : fp(_fp) {} ~Printer() { flush(); } private: static constexpr size_t SIZE = 1 << 15; FILE* fp; char line[SIZE], small[50]; size_t pos = 0; void flush() { fwrite(line, 1, pos, fp); pos = 0; } void write_single(const char& val) { if (pos == SIZE) flush(); line[pos++] = val; } template <class T, enable_if_t<is_integral<T>::value, int> = 0> void write_single(T val) { if (pos > (1 << 15) - 50) flush(); if (val == 0) { write_single('0'); return; } if (val < 0) { write_single('-'); val = -val; // todo min } size_t len = 0; while (val) { small[len++] = char(0x30 | (val % 10)); val /= 10; } for (size_t i = 0; i < len; i++) { line[pos + i] = small[len - 1 - i]; } pos += len; } void write_single(const string& s) { for (char c : s) write_single(c); } void write_single(const char* s) { size_t len = strlen(s); for (size_t i = 0; i < len; i++) write_single(s[i]); } template <class T> void write_single(const V<T>& val) { auto n = val.size(); for (size_t i = 0; i < n; i++) { if (i) write_single(' '); write_single(val[i]); } } }; Scanner sc = Scanner(stdin); Printer pr = Printer(stdout); struct SCC { V<int> id; VV<int> groups; }; template <class E> struct SCCExec : SCC { int n; const VV<E>& g; int tm = 0; V<bool> flag; V<int> low, ord, st; void dfs(int v) { low[v] = ord[v] = tm++; st.push_back(v); flag[v] = true; for (auto e : g[v]) { if (ord[e.to] == -1) { dfs(e.to); low[v] = min(low[v], low[e.to]); } else if (flag[e.to]) { low[v] = min(low[v], ord[e.to]); } } if (low[v] == ord[v]) { V<int> gr; while (true) { int u = st.back(); st.pop_back(); gr.push_back(u); if (u == v) break; } for (int x : gr) flag[x] = false; groups.push_back(gr); } } SCCExec(const VV<E>& _g) : n(int(_g.size())), g(_g), flag(n), low(n), ord(n, -1) { id = V<int>(n); for (int i = 0; i < n; i++) { if (ord[i] == -1) dfs(i); } reverse(groups.begin(), groups.end()); for (int i = 0; i < int(groups.size()); i++) { for (int x : groups[i]) { id[x] = i; } } } }; template <class E> SCC get_scc(const VV<E>& g) { return SCCExec<E>(g); } template <size_t N> struct BitsetSCCExec : SCC { using B = bitset<N>; int n; const V<B>& g; const V<B>& rg; V<int> vs; B unvis; void dfs(int v) { unvis.reset(v); while (true) { int d = (unvis & g[v])._Find_first(); if (d >= n) break; dfs(d); } vs.push_back(v); } void rdfs(int v, int k) { unvis.reset(v); id[v] = k; groups[k].push_back(v); while (true) { int d = (unvis & rg[v])._Find_first(); if (d >= n) break; rdfs(d, k); } } BitsetSCCExec(const V<B>& _g, const V<B>& _rg) : n(int(_g.size())), g(_g), rg(_rg) { unvis.set(); for (int i = 0; i < n; i++) { if (unvis[i]) dfs(i); } reverse(vs.begin(), vs.end()); unvis.set(); id = V<int>(n); int k = 0; for (int i : vs) { if (unvis[i]) { groups.push_back({}); rdfs(i, k++); } } } }; template <size_t N> SCC get_bitset_scc(const V<bitset<N>>& g, const V<bitset<N>>& rg) { return BitsetSCCExec<N>(g, rg); } struct TwoSat { V<bool> res; struct Edge { int to; }; VV<Edge> g; //(a == a_exp) || (b == b_exp) void add_cond(int a, bool a_exp, int b, bool b_exp) { g[2 * a + (a_exp ? 0 : 1)].push_back(Edge{2 * b + (b_exp ? 1 : 0)}); g[2 * b + (b_exp ? 0 : 1)].push_back(Edge{2 * a + (a_exp ? 1 : 0)}); } bool exec() { int n = int(res.size()); auto s = get_scc(g); for (int i = 0; i < n; i++) { if (s.id[2 * i] == s.id[2 * i + 1]) return false; res[i] = s.id[2 * i] < s.id[2 * i + 1]; } return true; } TwoSat() {} TwoSat(int n) { g = VV<Edge>(2 * n); res = V<bool>(n); } }; int main() { int n; sc.read(n); TwoSat st(n * n); V<int> s(n), t(n), u(n); for (int i = 0; i < n; i++) { sc.read(s[i]); s[i]--; } for (int i = 0; i < n; i++) { sc.read(t[i]); t[i]--; } for (int i = 0; i < n; i++) { sc.read(u[i]); } auto id = [&](int r, int c) { return r * n + c; }; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { bool x = (u[i] & 1) != 0; bool y = (u[i] & 2) != 0; st.add_cond(id(s[i], j), !x, id(j, t[i]), !y); } } if (!st.exec()) { pr.writeln(-1); return 0; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { pr.write(st.res[id(i, j)] ? 1 : 0); pr.write(' '); } pr.writeln(); } return 0; }