結果
| 問題 | No.2072 Anatomy | 
| コンテスト | |
| ユーザー |  hiro71687k | 
| 提出日時 | 2023-03-03 09:36:57 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 172 ms / 2,000 ms | 
| コード長 | 1,446 bytes | 
| コンパイル時間 | 3,920 ms | 
| コンパイル使用メモリ | 253,116 KB | 
| 最終ジャッジ日時 | 2025-02-11 01:22:13 | 
| ジャッジサーバーID (参考情報) | judge1 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 27 | 
ソースコード
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using namespace std;
using ll=long long;
using ld=double;
ld pie=3.14159265359;
ll mod=998244353;
long long inf=100000000000000001;
struct unionfind
{
    vector<ll>par,siz;
    unionfind(ll n): par(n,-1),siz(n,1){}
    ll root(ll x){
        if (par[x]==-1)
        {
            return x;
        }else{
            return par[x]=root(par[x]);
        }
    }
    bool issame(ll x,ll y){
        return root(x)==root(y);
    }
    bool unite(ll x,ll y){
        x=root(x);
        y=root(y);
        if (x==y)
        {
            return false;
        }
        if (siz[x]<siz[y])
        {
            swap(x,y);
        }
        par[y]=x;
        siz[x]+=siz[y];
        return true;
    }
    ll size(ll x){
        return siz[root(x)];
    }
};
int main(){
    ll n,m;
    cin >> n >> m;
    vector<ll>u(m),v(m);
    ll ans=0;
    unionfind uf(n);
    vector<ll>memo(n,0);
    for (ll i = 0; i < m; i++)
    {
        cin >> u[i] >> v[i];
        u[i]--,v[i]--;
    }
    reverse(u.begin(),u.end());
    reverse(v.begin(),v.end());
    for (ll i = 0; i < m; i++)
    {
        ll x=max(memo[uf.root(u[i])],memo[uf.root(v[i])]);
        memo[uf.root(u[i])]=0;
        memo[uf.root(v[i])]=0;
        uf.unite(u[i],v[i]);
        memo[uf.root(u[i])]=x+1;
    }
    
    for (ll i = 0; i < memo.size(); i++)
    {
        ans+=memo[i];
    }
    cout << ans << endl;
}
            
            
            
        