結果

問題 No.3024 全単射的
ユーザー otoshigo
提出日時 2025-02-14 22:10:28
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 222 ms / 5,000 ms
コード長 1,292 bytes
コンパイル時間 2,501 ms
コンパイル使用メモリ 216,196 KB
実行使用メモリ 20,484 KB
最終ジャッジ日時 2025-02-14 22:11:55
合計ジャッジ時間 4,855 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/dsu>
using namespace std;
using ll=long long;
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
template<class T> bool chmax(T &a, T b){if (a < b){a = b;return true;} else return false;}
template<class T> bool chmin(T &a, T b){if (a > b){a = b;return true;} else return false;}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N;
    ll M;
    cin>>N>>M;
    vector<ll>X(N),Y(N);
    vector<ll>V;
    for(int i=0;i<N;i++){
        cin>>X[i]>>Y[i];
        V.push_back(X[i]);
        V.push_back(Y[i]);
    }
    sort(all(V));
    V.erase(unique(all(V)),V.end());
    int L=V.size();
    map<ll,int>mp;
    for(int i=0;i<L;i++)mp[V[i]]=i;
    for(int i=0;i<N;i++){
        X[i]=mp[X[i]];
        Y[i]=mp[Y[i]];
    }
    atcoder::dsu uf(L);
    vector<int>P;
    for(int i=0;i<N;i++){
        if(uf.same(X[i],Y[i])){
            P.push_back(X[i]);
        }else{
            uf.merge(X[i],Y[i]);
        }
    }
    vector<bool>flag(L);
    vector<int>W(L);
    for(int i=0;i<L;i++)W[uf.leader(i)]++;
    for(auto x:P)flag[uf.leader(x)]=true;
    ll ans=0;
    for(int i=0;i<L;i++){
        if(W[i]>0){
            if(flag[i])ans+=W[i];
            else ans+=W[i]-1;
        }
    }
    cout<<ans<<"\n";
}
0