結果

問題 No.2072 Anatomy
ユーザー asaringoasaringo
提出日時 2022-09-16 22:25:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 51 ms / 2,000 ms
コード長 2,713 bytes
コンパイル時間 1,942 ms
コンパイル使用メモリ 203,828 KB
実行使用メモリ 8,564 KB
最終ジャッジ日時 2023-08-23 16:03:56
合計ジャッジ時間 4,327 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 45 ms
7,800 KB
testcase_09 AC 43 ms
7,984 KB
testcase_10 AC 43 ms
6,676 KB
testcase_11 AC 45 ms
7,768 KB
testcase_12 AC 34 ms
6,396 KB
testcase_13 AC 49 ms
8,236 KB
testcase_14 AC 30 ms
5,936 KB
testcase_15 AC 27 ms
6,420 KB
testcase_16 AC 50 ms
8,404 KB
testcase_17 AC 44 ms
8,352 KB
testcase_18 AC 24 ms
5,880 KB
testcase_19 AC 51 ms
8,552 KB
testcase_20 AC 49 ms
8,524 KB
testcase_21 AC 46 ms
8,488 KB
testcase_22 AC 49 ms
8,520 KB
testcase_23 AC 45 ms
8,504 KB
testcase_24 AC 46 ms
8,512 KB
testcase_25 AC 50 ms
8,564 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 47 ms
8,560 KB
testcase_28 AC 44 ms
8,508 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