結果

問題 No.1605 Matrix Shape
ユーザー chocoruskchocorusk
提出日時 2021-08-09 10:11:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 159 ms / 2,000 ms
コード長 1,978 bytes
コンパイル時間 3,525 ms
コンパイル使用メモリ 190,376 KB
実行使用メモリ 7,280 KB
最終ジャッジ日時 2023-10-21 08:02:25
合計ジャッジ時間 7,658 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
7,280 KB
testcase_01 AC 5 ms
7,280 KB
testcase_02 AC 5 ms
7,280 KB
testcase_03 AC 5 ms
7,280 KB
testcase_04 AC 5 ms
7,280 KB
testcase_05 AC 5 ms
7,280 KB
testcase_06 AC 6 ms
7,280 KB
testcase_07 AC 5 ms
7,280 KB
testcase_08 AC 5 ms
7,280 KB
testcase_09 AC 5 ms
7,280 KB
testcase_10 AC 5 ms
7,280 KB
testcase_11 AC 5 ms
7,280 KB
testcase_12 AC 5 ms
7,280 KB
testcase_13 AC 5 ms
7,280 KB
testcase_14 AC 5 ms
7,280 KB
testcase_15 AC 6 ms
7,280 KB
testcase_16 AC 5 ms
7,280 KB
testcase_17 AC 5 ms
7,280 KB
testcase_18 AC 5 ms
7,280 KB
testcase_19 AC 154 ms
7,280 KB
testcase_20 AC 137 ms
7,280 KB
testcase_21 AC 135 ms
7,280 KB
testcase_22 AC 154 ms
7,280 KB
testcase_23 AC 159 ms
7,280 KB
testcase_24 AC 150 ms
7,280 KB
testcase_25 AC 86 ms
7,280 KB
testcase_26 AC 87 ms
7,280 KB
testcase_27 AC 86 ms
7,280 KB
testcase_28 AC 87 ms
7,280 KB
testcase_29 AC 87 ms
7,280 KB
testcase_30 AC 135 ms
7,280 KB
testcase_31 AC 138 ms
7,280 KB
testcase_32 AC 120 ms
7,280 KB
testcase_33 AC 123 ms
7,280 KB
testcase_34 AC 113 ms
7,280 KB
testcase_35 AC 124 ms
7,280 KB
testcase_36 AC 72 ms
7,280 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0