結果

問題 No.2072 Anatomy
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-05-08 23:03:25
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,566 bytes
コンパイル時間 1,009 ms
コンパイル使用メモリ 112,152 KB
実行使用メモリ 8,856 KB
最終ジャッジ日時 2023-08-16 21:38:57
合計ジャッジ時間 7,166 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 WA -
testcase_09 AC 110 ms
8,124 KB
testcase_10 AC 108 ms
6,860 KB
testcase_11 WA -
testcase_12 AC 87 ms
6,416 KB
testcase_13 WA -
testcase_14 AC 74 ms
5,944 KB
testcase_15 AC 68 ms
6,220 KB
testcase_16 WA -
testcase_17 AC 113 ms
8,384 KB
testcase_18 AC 58 ms
5,952 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 115 ms
8,580 KB
testcase_22 WA -
testcase_23 AC 117 ms
8,656 KB
testcase_24 AC 113 ms
8,712 KB
testcase_25 WA -
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 117 ms
8,672 KB
testcase_28 AC 116 ms
8,712 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;
    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--){
        if (tree.same(u[i], v[i])) ans[tree.root(u[i])]++;
        else{
            tree.unite(u[i], v[i]);
            ans[tree.root(u[i])] = max(ans[tree.root(u[i])], ans[tree.root(v[i])])+1;
        }
    }

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

    cout << mx << endl;

    return 0;
}
0