結果
問題 |
No.1605 Matrix Shape
|
ユーザー |
![]() |
提出日時 | 2021-08-09 10:11:26 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 171 ms / 2,000 ms |
コード長 | 1,978 bytes |
コンパイル時間 | 3,564 ms |
コンパイル使用メモリ | 181,544 KB |
最終ジャッジ日時 | 2025-01-23 17:09:34 |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 34 |
ソースコード
#include <cstdio> #include <cstring> #include <iostream> #include <string> #include <cmath> #include <bitset> #include <vector> #include <map> #include <set> #include <queue> #include <deque> #include <algorithm> #include <complex> #include <unordered_map> #include <unordered_set> #include <random> #include <cassert> #include <fstream> #include <utility> #include <functional> #include <time.h> #include <stack> #include <array> #include <list> #include <atcoder/all> #define popcount __builtin_popcount using namespace std; using namespace atcoder; typedef long long ll; typedef pair<int, int> P; struct unionfind{ vector<int> par, sz; unionfind() {} unionfind(int n):par(n), sz(n, 1){ for(int i=0; i<n; i++) par[i]=i; } int find(int x){ if(par[x]==x) return x; return par[x]=find(par[x]); } void unite(int x, int y){ x=find(x); y=find(y); if(x==y) return; if(sz[x]>sz[y]) swap(x, y); par[x]=y; sz[y]+=sz[x]; } bool same(int x, int y){ return find(x)==find(y); } int size(int x){ return sz[find(x)]; } }; int main() { int n; cin>>n; int d1[200020]={}, d2[200020]={}, cnt[200020]={}; unionfind uf(200001); int e=0, v=0; for(int i=0; i<n; i++){ int h, w; cin>>h>>w; cnt[h]++; cnt[w]++; if(!uf.same(h, w)) e++; uf.unite(h, w); d1[h]++; d2[w]++; } for(int i=1; i<=200000; i++) if(cnt[i]) v++; if(v-1!=e){ cout<<0<<endl; return 0; } int c0=0, c1=0, c2=0; for(int i=1; i<=200000; i++){ if(!cnt[i]) continue; int d=d1[i]-d2[i]; if(d==0) c0++; else if(d==1) c1++; else if(d==-1) c2++; else{ cout<<0<<endl; return 0; } } if(c1 || c2){ if(c1==1 && c2==1) cout<<1<<endl; else cout<<0<<endl; return 0; } cout<<c0<<endl; return 0; }