結果

問題 No.1605 Matrix Shape
ユーザー rabimea
提出日時 2025-09-27 09:06:37
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 68 ms / 2,000 ms
コード長 1,371 bytes
コンパイル時間 7,973 ms
コンパイル使用メモリ 279,092 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2025-09-27 09:06:54
合計ジャッジ時間 6,882 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

// #pragma GCC optimize ("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target ("avx,avx2,fma")
#include <bits/stdc++.h>

using std::cin, std::cout, std::cerr;
using ll = long long;

int main() {
    std::ios::sync_with_stdio(false);
    int n; cin >> n;
    int m = 2e5;
    std::vector<int> p(m + 1), sz(m + 1, 1), ecnt(m + 1), deg(m + 1);
    std::iota(p.begin(), p.end(), 0);
    
    std::function<int(int)> root = [&](int x) {
        if(p[x] == x) return x;
        return p[x] = root(p[x]);
    };
    for(int i = 1; i <= n; i ++) {
        int x, y; cin >> x >> y;
        deg[x] --; deg[y] ++;
        int rx = root(x), ry = root(y);
        if(rx != ry) {
            ecnt[rx] ++;
            sz[rx] += sz[ry];
            p[ry] = rx;
        } else {
            ecnt[rx] ++;
        }
    }

    int cnt = 0, id;
    for(int i = 1; i <= m; i ++) if(p[i] == i && ecnt[i] > 0) {
        cnt ++;
        id = i;
    }
    
    if(cnt == 1) {
        if(std::count(deg.begin(), deg.end(), 0) == m + 1) {
            cout << sz[id] << '\n';
            return 0;
        } else if(std::count(deg.begin(), deg.end(), 0) == m + 1 - 2
            && std::count(deg.begin(), deg.end(), 1) == 1
            && std::count(deg.begin(), deg.end(), -1) == 1) {
            cout << 1 << '\n';
            return 0;
        }
    }
    cout << 0 << '\n';
}
0