結果

問題 No.2072 Anatomy
ユーザー asaringoasaringo
提出日時 2022-09-16 22:25:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 55 ms / 2,000 ms
コード長 2,713 bytes
コンパイル時間 2,113 ms
コンパイル使用メモリ 205,532 KB
実行使用メモリ 8,832 KB
最終ジャッジ日時 2024-06-01 13:30:56
合計ジャッジ時間 4,480 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 47 ms
7,936 KB
testcase_09 AC 46 ms
8,320 KB
testcase_10 AC 46 ms
7,040 KB
testcase_11 AC 48 ms
8,064 KB
testcase_12 AC 38 ms
6,940 KB
testcase_13 AC 53 ms
8,448 KB
testcase_14 AC 34 ms
6,940 KB
testcase_15 AC 30 ms
6,944 KB
testcase_16 AC 54 ms
8,576 KB
testcase_17 AC 49 ms
8,448 KB
testcase_18 AC 27 ms
6,940 KB
testcase_19 AC 55 ms
8,704 KB
testcase_20 AC 55 ms
8,704 KB
testcase_21 AC 51 ms
8,704 KB
testcase_22 AC 55 ms
8,576 KB
testcase_23 AC 52 ms
8,704 KB
testcase_24 AC 49 ms
8,576 KB
testcase_25 AC 54 ms
8,832 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 51 ms
8,704 KB
testcase_28 AC 47 ms
8,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std ;
#define fast_input_output ios::sync_with_stdio(false); cin.tie(nullptr);
typedef long long ll ;
typedef long double ld ;
typedef pair<ll,ll> P ;
typedef tuple<ll,ll,ll> TP ;
#define chmin(a,b) a = min(a,b)
#define chmax(a,b) a = max(a,b)
#define bit_count(x) __builtin_popcountll(x)
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) a / gcd(a,b) * b
#define rep(i,n) for(int i = 0 ; i < n ; i++)
#define rrep(i,a,b) for(int i = a ; i < b ; i++)
#define endl "\n"

struct UnionFind {
    private:
        vector<int> par ; //親
        vector<int> lank ; //木の深さ
        vector<int> volume ; //構成する集合のサイズ
        vector<int> edge ; //構成する集合の辺の数
        vector<int> value;

    public:
        UnionFind(int n){
            //n要素で初期化
            par.resize(n) ;
            lank.resize(n) ;
            volume.resize(n) ;
            edge.resize(n) ;
            value.resize(n) ;
            for(int i = 0 ; i < n ; i++){
                par[i] = i ;
                lank[i] = 0 ;
                volume[i] = 1 ;
                edge[i] = 0 ;
                value[i] = 0;
            }
        }
        //木の根を求める
        int root(int x) {
            if(par[x] == x) return x ;
            else return par[x] = root(par[x]) ;
        }

        //xとyの属する集合を合併
        void unite(int x , int y){
            x = root(x);
            y = root(y) ;
            if(x == y) {
                edge[x]++ ;
                value[x] = max(value[x],value[y]) + 1;
                return ;
            }
            if(lank[x] < lank[y]){
                par[x] = y ;
                volume[y] += volume[x] ;
                edge[y] += edge[x] + 1 ;
                value[y] = max(value[x],value[y]) + 1;
            } else {
                par[y] = x ;
                volume[x] += volume[y] ;
                edge[x] += edge[y] + 1 ;
                value[x] = max(value[x],value[y]) + 1;
                if(lank[x] == lank[y]) lank[x]++ ;
            }
        }

        bool same(int x , int y) { return root(x) == root(y) ; }
        int size(int x) { return volume[root(x)] ; }
        int edge_num(int x) { return edge[root(x)] ; }
        int solve(int x) { return value[root(x)] ; }
};

int n, m ;
pair<int,int> E[202020];

int main(){
    fast_input_output
    cin >> n >> m ;
    UnionFind uf(n);
    rep(i,m){
        int a, b;
        cin >> a >> b;
        a--; b--;
        E[i] = {a,b};
    }
    for(int i = m - 1 ; i >= 0 ; i--){
        auto [a, b] = E[i];
        uf.unite(a,b);
    }
    int res = 0;
    rep(i,n) chmax(res,uf.solve(i));
    cout << res << endl;
}
0