結果

問題 No.2072 Anatomy
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-05-08 23:06:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 119 ms / 2,000 ms
コード長 1,568 bytes
コンパイル時間 985 ms
コンパイル使用メモリ 109,384 KB
実行使用メモリ 8,704 KB
最終ジャッジ日時 2023-08-16 21:40:13
合計ジャッジ時間 4,748 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 102 ms
7,940 KB
testcase_09 AC 109 ms
7,984 KB
testcase_10 AC 106 ms
6,708 KB
testcase_11 AC 111 ms
7,716 KB
testcase_12 AC 88 ms
6,392 KB
testcase_13 AC 116 ms
8,340 KB
testcase_14 AC 73 ms
5,872 KB
testcase_15 AC 68 ms
6,492 KB
testcase_16 AC 117 ms
8,372 KB
testcase_17 AC 113 ms
8,608 KB
testcase_18 AC 59 ms
6,000 KB
testcase_19 AC 118 ms
8,540 KB
testcase_20 AC 119 ms
8,504 KB
testcase_21 AC 114 ms
8,596 KB
testcase_22 AC 119 ms
8,660 KB
testcase_23 AC 116 ms
8,560 KB
testcase_24 AC 113 ms
8,512 KB
testcase_25 AC 118 ms
8,704 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 116 ms
8,512 KB
testcase_28 AC 115 ms
8,536 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <numeric>
#include <deque>

using namespace std;
 
struct UnionFind {
    vector<long long> par;
    vector<long long> siz;

    UnionFind(long long N) : par(N), siz(N) {
        for(long long i = 0; i < N; i++){
            par[i] = i;
            siz[i] = 1;
        }   
    }   
 
    long long root(long long x) {
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }   
 
    long long unite(long long x, long long y) {
        long long rx = root(x);
        long long ry = root(y);
        if (rx == ry) return rx; 
        if (siz[rx] > siz[ry]) swap(rx, ry);
        par[rx] = ry; 
        siz[ry] += siz[rx];
        return ry; 
    }   
 
    bool same(long long x, long long y) {
        long long rx = root(x);
        long long ry = root(y);
        return rx == ry; 
    }   
 
    long long size(long long x){ 
        return siz[root(x)];
    }   
};

int main(){
 
    int N, M, mx=0, pu, pv;
    cin >> N >> M;
    vector<int> u(M), v(M);
    for (int i=0; i<M; i++) cin >> u[i] >> v[i];
    UnionFind tree(N+1);
    vector<int> ans(N+1);
 
    for (int i=M-1; i>=0; i--){
        pu = tree.root(u[i]); pv = tree.root(v[i]);
        if (pu == pv) ans[pu]++;
        else{
            tree.unite(pu, pv);
            ans[tree.root(pu)] = max(ans[pu], ans[pv])+1;
        }
    }

    for (int i=1; i<=N; i++) mx = max(mx, ans[i]);

    cout << mx << endl;

    return 0;
}
0