Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
914 B
1
Indexable
Never
#include <bits/stdc++.h>

using namespace std;

unordered_map<int, int>parent;
unordered_map<int, int> ranks;

int ans = 1;

int find(int x){
    return parent[x] == x?x:parent[x] = find(parent[x]);
}

void merge(int a, int b){
    int pa = find(a);
    int pb = find(b);
    if(ranks[pa] > ranks[pb]){
        parent[pb] = pa;
        ranks[pa] += ranks[pb];
        ans = max(ans, ranks[pa]);
    }
    else{
        parent[pa] = pb;
        ranks[pb] += ranks[pa];
        ans = max(ans, ranks[pb]);
    }
    return;
}

int helper(int x){
    if(parent.count(x-1))
        merge(x, x-1);
    if(parent.count(x+1))
        merge(x, x+1);
    return ans;
}

int main(){
    vector<int> input = {5, 2, 3, 4, 7, 8};
    for(auto ele: input){
        parent[ele] = ele;
        ranks[ele] = 1;
        cout << helper(ele) << ' ';
    }
    cout << '\n';
    return 0;
}
Leave a Comment