結果

問題 No.1605 Matrix Shape
ユーザー umezoumezo
提出日時 2021-07-17 05:29:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 238 ms / 2,000 ms
コード長 1,835 bytes
コンパイル時間 4,602 ms
コンパイル使用メモリ 267,644 KB
実行使用メモリ 57,004 KB
最終ジャッジ日時 2023-09-20 21:31:14
合計ジャッジ時間 9,354 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,768 KB
testcase_01 AC 3 ms
8,708 KB
testcase_02 AC 4 ms
8,856 KB
testcase_03 AC 4 ms
8,692 KB
testcase_04 AC 4 ms
8,860 KB
testcase_05 AC 4 ms
8,784 KB
testcase_06 AC 4 ms
8,768 KB
testcase_07 AC 4 ms
8,660 KB
testcase_08 AC 4 ms
8,708 KB
testcase_09 AC 4 ms
8,784 KB
testcase_10 AC 4 ms
8,956 KB
testcase_11 AC 4 ms
8,708 KB
testcase_12 AC 4 ms
8,684 KB
testcase_13 AC 4 ms
8,664 KB
testcase_14 AC 3 ms
8,728 KB
testcase_15 AC 4 ms
8,704 KB
testcase_16 AC 4 ms
8,724 KB
testcase_17 AC 4 ms
8,724 KB
testcase_18 AC 4 ms
8,660 KB
testcase_19 AC 214 ms
57,004 KB
testcase_20 AC 172 ms
35,668 KB
testcase_21 AC 170 ms
35,816 KB
testcase_22 AC 213 ms
41,040 KB
testcase_23 AC 206 ms
50,084 KB
testcase_24 AC 238 ms
44,440 KB
testcase_25 AC 86 ms
15,244 KB
testcase_26 AC 87 ms
15,432 KB
testcase_27 AC 87 ms
15,400 KB
testcase_28 AC 87 ms
15,304 KB
testcase_29 AC 86 ms
15,328 KB
testcase_30 AC 158 ms
20,312 KB
testcase_31 AC 159 ms
20,640 KB
testcase_32 AC 142 ms
22,204 KB
testcase_33 AC 141 ms
22,304 KB
testcase_34 AC 51 ms
15,124 KB
testcase_35 AC 162 ms
34,544 KB
testcase_36 AC 100 ms
32,524 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;
  vector<int> B;
  rep(i,n){
    if((rdeg[i]+indeg[i])%2==1){
      cnt++;
      B.push_back(i);
    }
  }
  
  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==false){
      cout<<0<<endl;
      return 0;
    }
    bool c1=false,c2=false;
    for(auto t:scc[0]){
      if(t==B[0]) c1=true;
      if(t==B[1]) c2=true;
    }
    for(auto t:scc[scc.size()-1]){
      if(t==B[0]) c1=true;
      if(t==B[1]) c2=true;
    }
    if(c1==false || c2==false) cout<<0<<endl;
    else cout<<1<<endl;
  }
    
  return 0;
}
0