結果
問題 | No.1605 Matrix Shape |
ユーザー |
![]() |
提出日時 | 2021-07-16 23:26:31 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 240 ms / 2,000 ms |
コード長 | 2,133 bytes |
コンパイル時間 | 1,906 ms |
コンパイル使用メモリ | 208,784 KB |
最終ジャッジ日時 | 2025-01-23 02:47:06 |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 34 |
ソースコード
#include <bits/stdc++.h> using namespace std; struct iofast_t { iofast_t() { ios::sync_with_stdio(false); cin.tie(nullptr); } } iofast; struct uns_t {} uns; template <typename Element, typename Head, typename ...Args> auto vec(Element init, Head arg, Args ...args) { if constexpr (sizeof...(Args) == 0) return std::vector(arg, init); else return std::vector(arg, vec(init, args...)); } template <typename Element, typename Head, typename ...Args> auto vec(uns_t, Head arg, Args ...args) { return vec(Element(), arg, args...); } template <typename T, typename Compare = less<T>> T &chmin(T &l, T r, Compare &&f = less<T>()) { return l = min(l, r, f); } template <typename T, typename Compare = less<T>> T &chmax(T &l, T r, Compare &&f = less<T>()) { return l = max(l, r, f); } #include <atcoder/dsu> int main() { constexpr int lim = 200000; int n; cin >> n; auto g = vec<int>(uns, lim, 0); auto r = vec<int>(uns, lim, 0); auto h = vec<int>(uns, n); auto w = vec<int>(uns, n); for (int i = 0; i < n; ++i) { cin >> h[i] >> w[i]; --h[i]; --w[i]; g[h[i]].push_back(w[i]); r[w[i]].push_back(h[i]); } atcoder::dsu dsu(lim); auto in = vec<int>(0, lim); auto out = vec<int>(0, lim); for (int i = 0; i < lim; ++i) { for (auto v : g[i]) { ++out[i]; --out[v]; ++in[v]; --in[i]; dsu.merge(i, v); } } for (int i = 0; i < n; ++i) { if (!dsu.same(h[0], h[i]) || !dsu.same(w[0], w[i])) { cout << 0 << endl; return 0; } } if (count(begin(out), end(out), 0) == lim) { set<int> ans; for (int i = 0; i < n; ++i) { ans.insert(h[i]); ans.insert(w[i]); } cout << size(ans) << endl; return 0; } int p = 0; int m = 0; int o = 0; for (auto v : in) { p += v == +1; m += v == -1; o += v == 0; } if (!(p == 1 && m == 1 && o == lim - 2)) { cout << 0 << endl; return 0; } cout << 1 << endl; }