結果

問題 No.1605 Matrix Shape
ユーザー aut_jul_duraut_jul_dur
提出日時 2021-07-16 22:44:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,817 bytes
コンパイル時間 5,014 ms
コンパイル使用メモリ 263,232 KB
実行使用メモリ 24,920 KB
最終ジャッジ日時 2023-09-20 15:07:20
合計ジャッジ時間 9,616 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
12,468 KB
testcase_01 AC 7 ms
12,504 KB
testcase_02 AC 8 ms
12,472 KB
testcase_03 WA -
testcase_04 AC 6 ms
12,456 KB
testcase_05 AC 8 ms
12,380 KB
testcase_06 AC 8 ms
12,356 KB
testcase_07 AC 8 ms
12,560 KB
testcase_08 AC 8 ms
12,440 KB
testcase_09 AC 8 ms
12,444 KB
testcase_10 AC 7 ms
12,488 KB
testcase_11 AC 8 ms
12,372 KB
testcase_12 AC 7 ms
12,460 KB
testcase_13 AC 7 ms
12,308 KB
testcase_14 AC 8 ms
12,488 KB
testcase_15 AC 8 ms
12,564 KB
testcase_16 AC 9 ms
12,568 KB
testcase_17 AC 7 ms
12,520 KB
testcase_18 AC 7 ms
12,432 KB
testcase_19 AC 273 ms
24,920 KB
testcase_20 AC 218 ms
20,896 KB
testcase_21 AC 222 ms
20,944 KB
testcase_22 AC 243 ms
22,020 KB
testcase_23 AC 269 ms
22,976 KB
testcase_24 AC 270 ms
24,060 KB
testcase_25 AC 91 ms
16,336 KB
testcase_26 AC 90 ms
16,692 KB
testcase_27 AC 90 ms
16,612 KB
testcase_28 AC 90 ms
16,636 KB
testcase_29 AC 91 ms
16,448 KB
testcase_30 AC 205 ms
17,704 KB
testcase_31 AC 200 ms
17,864 KB
testcase_32 AC 172 ms
18,560 KB
testcase_33 AC 162 ms
17,768 KB
testcase_34 WA -
testcase_35 AC 180 ms
19,848 KB
testcase_36 AC 105 ms
18,768 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] = {};
  rep(i,N){
    ll a,b;
    cin>>a>>b;
    if(a == b){
      aru.push_back(a);
      continue;
    }
    G[a].push_back(b);
    G[b].push_back(a);
    H[a]++;
    W[b]++;
  }
  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