結果

問題 No.1605 Matrix Shape
ユーザー umezoumezo
提出日時 2021-07-17 05:17:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,494 bytes
コンパイル時間 4,568 ms
コンパイル使用メモリ 268,492 KB
実行使用メモリ 57,016 KB
最終ジャッジ日時 2023-09-20 21:18:39
合計ジャッジ時間 8,769 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
8,760 KB
testcase_01 AC 3 ms
8,816 KB
testcase_02 AC 3 ms
8,784 KB
testcase_03 AC 4 ms
8,788 KB
testcase_04 AC 3 ms
8,760 KB
testcase_05 AC 3 ms
8,720 KB
testcase_06 AC 3 ms
8,928 KB
testcase_07 AC 4 ms
8,732 KB
testcase_08 AC 3 ms
8,756 KB
testcase_09 AC 3 ms
8,784 KB
testcase_10 AC 3 ms
8,736 KB
testcase_11 AC 4 ms
8,984 KB
testcase_12 AC 4 ms
8,784 KB
testcase_13 AC 3 ms
8,760 KB
testcase_14 AC 4 ms
8,860 KB
testcase_15 AC 3 ms
8,844 KB
testcase_16 WA -
testcase_17 AC 3 ms
8,728 KB
testcase_18 AC 3 ms
8,780 KB
testcase_19 AC 203 ms
57,016 KB
testcase_20 AC 166 ms
35,732 KB
testcase_21 AC 166 ms
35,784 KB
testcase_22 AC 202 ms
41,056 KB
testcase_23 AC 196 ms
50,028 KB
testcase_24 AC 218 ms
44,476 KB
testcase_25 AC 83 ms
15,264 KB
testcase_26 AC 84 ms
15,344 KB
testcase_27 AC 84 ms
15,368 KB
testcase_28 AC 85 ms
15,424 KB
testcase_29 AC 83 ms
15,260 KB
testcase_30 AC 152 ms
20,304 KB
testcase_31 AC 156 ms
20,740 KB
testcase_32 AC 139 ms
22,292 KB
testcase_33 AC 140 ms
22,196 KB
testcase_34 AC 51 ms
15,308 KB
testcase_35 AC 158 ms
34,656 KB
testcase_36 AC 95 ms
32,764 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;

#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;

int indeg[200200],rdeg[200200];
vector<int> G[200200];
int seen[200200];

template <typename T>
vector<T> compress(vector<T> &X){
  vector<T> vals=X;
  sort(vals.begin(),vals.end());
  vals.erase(unique(vals.begin(), vals.end()),vals.end());
  for(int i=0;i<(int)X.size(); i++){
    X[i]=lower_bound(vals.begin(),vals.end(),X[i])-vals.begin();
  }
  return vals;
}

void dfs(int v){
  seen[v]=1;
  for(auto nv:G[v]){
    if(seen[nv]) continue;
    dfs(nv);
  }
}
 
int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  
  int n;
  cin>>n;
  
  vector<int> A(2*n);
  rep(i,2*n) cin>>A[i];
  compress(A);
  vector<int> H(n),W(n);
  int manum=0;
  rep(i,n){
    H[i]=A[2*i],W[i]=A[2*i+1];
    manum=max({manum,H[i],W[i]});
  }

  scc_graph g(manum+1);
  rep(i,n){
    g.add_edge(H[i],W[i]);
    rdeg[H[i]]++;
    indeg[W[i]]++;
    G[H[i]].push_back(W[i]);
  }

  auto scc=g.scc();
  int cnt=0;
  rep(i,n){
    if((rdeg[i]+indeg[i])%2==1) cnt++;
  }
  
  if(scc.size()==1){
    if(cnt==0) cout<<manum+1<<endl;
    else if(cnt==2) cout<<1<<endl;
    else cout<<0<<endl;
  }
  else{
    int s=scc[0][0];
    dfs(s);
    bool b=true;
    rep(i,manum+1){
      if(seen[i]==0 || abs(indeg[i]-rdeg[i])>1) b=false;
    }
    if(b) cout<<1<<endl;
    else cout<<0<<endl;
  }
    
  return 0;
}
0