結果

問題 No.1605 Matrix Shape
ユーザー aut_jul_duraut_jul_dur
提出日時 2021-07-16 22:48:27
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 276 ms / 2,000 ms
コード長 2,068 bytes
コンパイル時間 6,501 ms
コンパイル使用メモリ 263,076 KB
実行使用メモリ 27,880 KB
最終ジャッジ日時 2023-09-20 15:11:33
合計ジャッジ時間 9,401 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
15,472 KB
testcase_01 AC 9 ms
15,524 KB
testcase_02 AC 8 ms
15,412 KB
testcase_03 AC 7 ms
15,528 KB
testcase_04 AC 9 ms
15,808 KB
testcase_05 AC 9 ms
15,476 KB
testcase_06 AC 10 ms
15,496 KB
testcase_07 AC 9 ms
15,428 KB
testcase_08 AC 9 ms
15,464 KB
testcase_09 AC 10 ms
15,600 KB
testcase_10 AC 8 ms
15,424 KB
testcase_11 AC 9 ms
15,520 KB
testcase_12 AC 8 ms
15,484 KB
testcase_13 AC 8 ms
15,480 KB
testcase_14 AC 9 ms
15,588 KB
testcase_15 AC 9 ms
15,572 KB
testcase_16 AC 9 ms
15,456 KB
testcase_17 AC 9 ms
15,428 KB
testcase_18 AC 9 ms
15,416 KB
testcase_19 AC 276 ms
27,880 KB
testcase_20 AC 216 ms
24,040 KB
testcase_21 AC 215 ms
24,112 KB
testcase_22 AC 238 ms
25,056 KB
testcase_23 AC 268 ms
26,080 KB
testcase_24 AC 273 ms
27,436 KB
testcase_25 AC 92 ms
19,580 KB
testcase_26 AC 92 ms
19,720 KB
testcase_27 AC 91 ms
19,532 KB
testcase_28 AC 91 ms
19,828 KB
testcase_29 AC 89 ms
19,432 KB
testcase_30 AC 192 ms
20,756 KB
testcase_31 AC 200 ms
20,772 KB
testcase_32 AC 167 ms
21,688 KB
testcase_33 AC 161 ms
20,752 KB
testcase_34 AC 110 ms
17,648 KB
testcase_35 AC 176 ms
23,108 KB
testcase_36 AC 104 ms
22,000 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
const long long MOD = 1000000007;
const long long INF = 9999999999999999;
using ll = long long;
using mint = modint1000000007;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }


vector<vector<ll>> G(200200);
vector<ll> dist(200200);

void dfs(int v){
  for(auto e:G[v]){
    if(dist[e] == 0){
      dist[e] = 1;
      dfs(e);
    }
  }
}







int main(){
  ll N;
  cin>>N;
  vector<ll> aru;
  ll H[200200] = {};
  ll W[200200] = {};
  ll H2[200200] = {};
  ll W2[200200] = {};
  rep(i,N){
    ll a,b;
    cin>>a>>b;
    if(a == b){
      H2[a]++;
      W2[b]++;
      aru.push_back(a);
      continue;
    }
    G[a].push_back(b);
    G[b].push_back(a);
    H[a]++;
    W[b]++;
  }
  bool special = 0;
  rep(i,200200){
    if(H2[i] == N && W2[i] == N){
      special = 1;
    }
  }
  if(special){
    cout << 1 << endl;
    return 0;
  }
  
  
  
  
  
  
  
  ll youso = 0;
  ll saisyo = 0;
  rep(i,200200){
    if(H[i]>0 || W[i]>0){
      saisyo = i;
      youso++;
    }
  }
  dist[saisyo] = 1;
  dfs(saisyo);
  ll kyori = 0;
  rep(i,200200){
    kyori += dist[i];
  }
  if(kyori != youso){
    cout << 0 << endl;
    return 0;
  }
  
  
  
  ll d = 0;
  rep(i,aru.size()){
    if(H[aru[i]] == 0 && W[aru[i]] == 0){
      d = 1;
    }
  }
  ll Hh = 0;
  ll Wh = 0;
  rep(i,200200){
    if(abs(H[i]-W[i]) > 1){
      d = 1;
    }
    else if(H[i]-W[i] == 1){
      Hh++;
    }
    else if(W[i]-H[i] == 1){
      Wh++;
    }
    else{
      continue;
    }
  }
  if(d){
    cout << 0 << endl;
    return 0;
  }
  if(Hh == 0 && Wh == 0){
    ll ans = 0;
    rep(i,200200){
      if(H[i]>0){
        ans++;
      }
    }
    cout << ans << endl;
  }
  else if(Hh == 1 && Wh == 1){
    cout << 1 << endl;
  }
  else{
    cout << 0 << endl;
  }
  return 0;
}
0