結果
問題 | No.1605 Matrix Shape |
ユーザー | aut_jul_dur |
提出日時 | 2021-07-16 22:48:27 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 253 ms / 2,000 ms |
コード長 | 2,068 bytes |
コンパイル時間 | 3,906 ms |
コンパイル使用メモリ | 254,308 KB |
最終ジャッジ日時 | 2025-01-23 02:29:01 |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 34 |
ソースコード
#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; }