• + 1 comment

    Runtime error on #11 testcase others are successfully executed

    int parent[100001]; int Size[100001];

    void make(int n){ for(int i=0;i

    int find(int vertex){ if(parent[vertex]==vertex)return vertex; return parent[vertex]=find(parent[vertex]); }

    void Union(int a,int b){ a=find(a); b=find(b); if(a==b)return ; if(Size[b]>Size[a])swap(a,b); parent[b]=a; Size[a]+=Size[b]; }

    int journeyToMoon(int n, vector> astronaut) { make(n);

    for(int i=0;i<astronaut.size();i++){
        if(find(astronaut[i][0])!=find(astronaut[i][1])){
            Union(astronaut[i][0],astronaut[i][1]);
        }
    }
    long long ans=0;
    vector<int> a;
    for(int i=0;i<n;i++){
        if(parent[i]==i){
            a.push_back(Size[i]);
        }
    }
    for(int i=0;i<a.size();i++){
        for(int j=i+1;j<a.size();j++)ans+=(a[i]*a[j]);
    }
    return ans;