結果

問題 No.1605 Matrix Shape
ユーザー 👑 NachiaNachia
提出日時 2021-07-16 23:16:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 111 ms / 2,000 ms
コード長 1,107 bytes
コンパイル時間 806 ms
コンパイル使用メモリ 83,776 KB
実行使用メモリ 16,572 KB
最終ジャッジ日時 2023-09-20 15:44:17
合計ジャッジ時間 4,395 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
9,196 KB
testcase_01 AC 6 ms
9,324 KB
testcase_02 AC 5 ms
9,144 KB
testcase_03 AC 6 ms
9,144 KB
testcase_04 AC 6 ms
9,352 KB
testcase_05 AC 6 ms
9,192 KB
testcase_06 AC 6 ms
9,140 KB
testcase_07 AC 6 ms
9,324 KB
testcase_08 AC 7 ms
9,168 KB
testcase_09 AC 5 ms
9,128 KB
testcase_10 AC 6 ms
9,364 KB
testcase_11 AC 6 ms
9,260 KB
testcase_12 AC 6 ms
9,428 KB
testcase_13 AC 6 ms
9,200 KB
testcase_14 AC 6 ms
9,164 KB
testcase_15 AC 6 ms
9,104 KB
testcase_16 AC 6 ms
9,388 KB
testcase_17 AC 6 ms
9,412 KB
testcase_18 AC 7 ms
9,280 KB
testcase_19 AC 111 ms
16,560 KB
testcase_20 AC 69 ms
13,432 KB
testcase_21 AC 73 ms
13,444 KB
testcase_22 AC 89 ms
16,176 KB
testcase_23 AC 103 ms
16,572 KB
testcase_24 AC 98 ms
16,564 KB
testcase_25 AC 37 ms
11,212 KB
testcase_26 AC 37 ms
11,372 KB
testcase_27 AC 37 ms
11,216 KB
testcase_28 AC 35 ms
10,280 KB
testcase_29 AC 36 ms
11,448 KB
testcase_30 AC 67 ms
13,440 KB
testcase_31 AC 65 ms
13,596 KB
testcase_32 AC 58 ms
12,496 KB
testcase_33 AC 54 ms
11,652 KB
testcase_34 AC 40 ms
11,516 KB
testcase_35 AC 62 ms
14,404 KB
testcase_36 AC 35 ms
12,424 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// Nachia くんだよ

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)

const int Z = 200000;

int N;
vector<int> H,W;
vector<vector<int>> E;

int main() {
  cin >> N;
  H.assign(Z,0);
  W.assign(Z,0);
  E.resize(Z);
  rep(i,N){
    int h,w; cin >> h >> w; h--; w--;
    H[h]++;
    W[w]++;
    E[h].push_back(w);
  }
  
  int sH = -1;
  int dH = 0;
  rep(i,Z) if(H[i] > W[i]){
    dH += H[i] - W[i];
    sH = i;
  }
  if(dH > 1){ cout << 0 << "\n"; return 0; }

  vector<int> I = {sH};
  if(I.back() == -1) rep(i,Z) if(H[i] != 0) I.back() = i;
  int c = 0;
  rep(i,I.size()){
    int p = I[i];
    for(int e : E[p]) I.push_back(e);
    E[p].clear();
  }

  c = I.size() - 1;

  if(c != N) cout << "0\n";
  else if(sH == -1){
    int ans = 0;
    rep(i,Z) if(H[i] != 0) ans++;
    cout << ans << "\n";
  }
  else cout << "1\n";
  return 0;
}


struct ios_do_not_sync{
  ios_do_not_sync(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
  }
} ios_do_not_sync_instance;


0