結果

問題 No.483 マッチ並べ
ユーザー Mister
提出日時 2020-09-13 19:09:47
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,559 bytes
コンパイル時間 979 ms
コンパイル使用メモリ 88,212 KB
最終ジャッジ日時 2025-01-14 14:32:48
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 53
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <numeric>
#include <vector>
#include <map>

struct UnionFind {
    std::vector<int> par, sz;
    int gnum;

    explicit UnionFind(int n)
        : par(n), sz(n, 1), gnum(n) {
        std::iota(par.begin(), par.end(), 0);
    }

    int find(int v) {
        return (par[v] == v) ? v : (par[v] = find(par[v]));
    }

    void unite(int u, int v) {
        u = find(u), v = find(v);
        if (u == v) return;

        if (sz[u] < sz[v]) std::swap(u, v);
        sz[u] += sz[v];
        par[v] = u;
        --gnum;
    }

    bool same(int u, int v) { return find(u) == find(v); }
    bool ispar(int v) { return v == find(v); }
    int size(int v) { return sz[find(v)]; }
};

void solve() {
    int m;
    std::cin >> m;

    std::vector<std::pair<int, int>> es(m);
    int n = 0;
    {
        std::map<std::pair<int, int>, int> rev;

        auto input = [&]() {
            std::pair<int, int> p;
            std::cin >> p.first >> p.second;
            if (!rev.count(p)) rev[p] = n++;
            return rev[p];
        };

        for (auto& [u, v] : es) u = input(), v = input();
    }

    UnionFind uf(n);
    for (auto [u, v] : es) uf.unite(u, v);

    std::vector<int> cnt(n, 0);
    for (auto [u, v] : es) ++cnt[uf.find(u)];

    for (int v = 0; v < n; ++v) {
        if (uf.ispar(v) && uf.size(v) < cnt[v]) {
            std::cout << "NO\n";
            return;
        }
    }
    std::cout << "YES\n";
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0