結果
| 問題 | 
                            No.3024 全単射的
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2025-02-14 22:13:24 | 
| 言語 | C++17  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 99 ms / 5,000 ms | 
| コード長 | 961 bytes | 
| コンパイル時間 | 4,275 ms | 
| コンパイル使用メモリ | 259,624 KB | 
| 実行使用メモリ | 17,252 KB | 
| 最終ジャッジ日時 | 2025-02-14 22:15:57 | 
| 合計ジャッジ時間 | 5,967 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge3 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 22 | 
ソースコード
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using ll = long long;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll n, m;
    cin >> n >> m;
    vector<pair<ll,ll>> edge(n);
    vector<ll> ca;
    ca.reserve(2 * n);
    for(auto &&[u, v] : edge){
        cin >> u >> v;
        ca.emplace_back(u);
        ca.emplace_back(v);
    }
    sort(ca.begin(), ca.end());
    ca.erase(unique(ca.begin(), ca.end()), ca.end());
    vector<int> cnt(ca.size());
    atcoder::dsu uf(ca.size());
    for(auto &&[u, v] : edge){
        u = lower_bound(ca.begin(), ca.end(), u) - ca.begin();
        v = lower_bound(ca.begin(), ca.end(), v) - ca.begin();
        uf.merge(u, v);
        cnt[u]++;
    }
    int ans = 0;
    auto G = uf.groups();
    for(auto &&vec : G){
        int tot = 0;
        for(auto &&v : vec){
            tot += cnt[v];
        }
        ans += min((int)vec.size(), tot);
    }
    cout << ans << '\n';
}