結果

問題 No.2072 Anatomy
ユーザー hiro71687khiro71687k
提出日時 2023-03-03 09:36:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 130 ms / 2,000 ms
コード長 1,446 bytes
コンパイル時間 5,788 ms
コンパイル使用メモリ 264,276 KB
実行使用メモリ 11,256 KB
最終ジャッジ日時 2023-10-18 00:51:48
合計ジャッジ時間 12,993 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 113 ms
9,936 KB
testcase_09 AC 121 ms
10,464 KB
testcase_10 AC 116 ms
9,144 KB
testcase_11 AC 121 ms
10,200 KB
testcase_12 AC 95 ms
8,352 KB
testcase_13 AC 126 ms
10,992 KB
testcase_14 AC 80 ms
7,824 KB
testcase_15 AC 75 ms
8,088 KB
testcase_16 AC 127 ms
10,992 KB
testcase_17 AC 123 ms
10,728 KB
testcase_18 AC 65 ms
7,296 KB
testcase_19 AC 130 ms
11,256 KB
testcase_20 AC 130 ms
11,256 KB
testcase_21 AC 125 ms
11,256 KB
testcase_22 AC 130 ms
11,256 KB
testcase_23 AC 128 ms
11,256 KB
testcase_24 AC 125 ms
11,256 KB
testcase_25 AC 130 ms
11,256 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 128 ms
11,256 KB
testcase_28 AC 126 ms
11,256 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0