結果

問題 No.1605 Matrix Shape
ユーザー milanis48663220milanis48663220
提出日時 2021-07-16 21:45:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,286 bytes
コンパイル時間 1,234 ms
コンパイル使用メモリ 129,624 KB
実行使用メモリ 25,768 KB
最終ジャッジ日時 2023-09-20 13:30:27
合計ジャッジ時間 6,268 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,372 KB
testcase_01 AC 4 ms
5,388 KB
testcase_02 AC 3 ms
5,664 KB
testcase_03 AC 4 ms
5,488 KB
testcase_04 AC 3 ms
5,420 KB
testcase_05 AC 3 ms
5,364 KB
testcase_06 AC 4 ms
5,428 KB
testcase_07 AC 3 ms
5,356 KB
testcase_08 WA -
testcase_09 AC 3 ms
5,368 KB
testcase_10 AC 3 ms
5,480 KB
testcase_11 AC 3 ms
5,368 KB
testcase_12 WA -
testcase_13 AC 3 ms
5,468 KB
testcase_14 AC 4 ms
5,492 KB
testcase_15 AC 4 ms
5,420 KB
testcase_16 AC 4 ms
5,392 KB
testcase_17 AC 3 ms
5,368 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 355 ms
25,700 KB
testcase_20 WA -
testcase_21 AC 155 ms
11,792 KB
testcase_22 AC 237 ms
16,252 KB
testcase_23 WA -
testcase_24 AC 235 ms
16,260 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 51 ms
7,000 KB
testcase_28 AC 51 ms
7,020 KB
testcase_29 AC 52 ms
7,052 KB
testcase_30 WA -
testcase_31 AC 152 ms
11,704 KB
testcase_32 WA -
testcase_33 AC 109 ms
9,352 KB
testcase_34 AC 39 ms
6,980 KB
testcase_35 AC 148 ms
14,068 KB
testcase_36 AC 75 ms
10,980 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;
using P = pair<int,int>;


struct UnionFind {
    vector<int> data;
    UnionFind(int size) : data(size, -1) {}
    bool unionSet(int x, int y) {
        x = root(x); y = root(y);
        if (x != y) {
        if (data[y] < data[x]) swap(x, y);
        data[x] += data[y]; data[y] = x;
        }
        return x != y;
    }
    bool findSet(int x, int y) {
        return root(x) == root(y);
    }
    int root(int x) {
        return data[x] < 0 ? x : data[x] = root(data[x]);
    }
    int size(int x) {
        return -data[root(x)];
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n; cin >> n;
    vector<int> in(200000), out(200000);
    UnionFind uf(200000);
    vector<int> h(n), w(n);
    set<int> used;
    for(int i = 0; i < n; i++){
        cin >> h[i] >> w[i]; h[i]--; w[i]--;
        out[w[i]]++;
        in[h[i]]++;
        uf.unionSet(h[i],w[i]);
        used.insert(h[i]);
        used.insert(w[i]);
    }
    if(uf.size(h[0]) != used.size()){
        cout << 0 << endl;
        return 0;
    }
    int cnt_s = 0;
    int cnt_t = 0;
    for(int i = 0; i < 200000; i++){
        if(abs(in[i]-out[i]) > 1){
            cout << 0 << endl;
            return 0;
        }
        if(in[i] > out[i]) cnt_t++;
        if(out[i] > in[i]) cnt_s++;
    }
    if(cnt_t ==  cnt_s){
        if(cnt_t == 0) {
            set<P> st;
            for(int i = 0; i < n; i++) st.insert(P(h[i], w[i]));
            cout << st.size() << endl;
        }
        else if(cnt_t == 1) cout << 1 << endl;
        else cout << 0 << endl;
    }else{
        cout << 0 << endl;
    }
}
0