Search
 
SCRIPT & CODE EXAMPLE
 

CPP

c++ union set q5

class Solution {
private:
    vector<int> prnt1, prnt2;
    vector<int> rank1, rank2;
    
    int find(int u, vector<int> &prnt) {
        if(prnt[u] == -1)
            return u;
        return prnt[u] = find(prnt[u], prnt);
    }
    
    void merge(int u, int v, vector<int> &prnt, vector<int> &rank) {
        u = find(u, prnt);
        v = find(v, prnt);
        
        if(u == v) return;
        
        if(rank[u] < rank[v]) 
            swap(u, v);

        rank[u] += rank[v];
        prnt[v] = u;
    }
    
public:
    int maxNumEdgesToRemove(int n, vector<vector<int>>& edges) {
        prnt1.resize(n+1, -1);
        prnt2.resize(n+1, -1);
        
        rank1.resize(n+1, 1);
        rank2.resize(n+1, 1);
        
        int tot_t3 = 0, used_t3 = 0;
        for(auto it: edges) {
            if(it[0] != 3) 
                continue;
            tot_t3 += 1;
            if(find(it[1], prnt1) != find(it[2], prnt1)) {
                used_t3 += 1;
                merge(it[1], it[2], prnt1, rank1);
                merge(it[1], it[2], prnt2, rank2);
            }
        }
        
        int tot_t1 = 0, used_t1 = 0;
        int tot_t2 = 0, used_t2 = 0;
        for(auto it: edges) {
            if(it[0] == 1) {
                tot_t1 += 1;
                if(find(it[1], prnt1) != find(it[2], prnt1)) {
                    used_t1 += 1;
                    merge(it[1], it[2], prnt1, rank1);
                }
            } else if(it[0] == 2) {
                tot_t2 += 1;
                if(find(it[1], prnt2) != find(it[2], prnt2)) {
                    used_t2 += 1;
                    merge(it[1], it[2], prnt2, rank2);
                }
            }
        }
        
        if(used_t3 + used_t1 != n-1 || used_t3 + used_t2 != n-1)
            return -1;
        
        return (tot_t3 + tot_t2 + tot_t1) - (used_t3 + used_t2 + used_t1);
    }
};
Comment

PREVIOUS NEXT
Code Example
Cpp :: C++ concept simple requirements 
Cpp :: check if string in vector c++ 
Cpp :: attack on titan junior high list of episodes 
Cpp :: c++ n in regex 
Cpp :: The program must enter a natural number n from the console and find the number previous to n that is not divisible by 2 , 3 and 5 . 
Cpp :: cast c++ 
Cpp :: Chef and Races codechef solution in c++ 
Cpp :: 976. Largest Perimeter Triangle leetcode solution in c++ 
Cpp :: gcd of two number in c++ stl 
Cpp :: three-way comparison c++ 
Cpp :: 771. Jewels and Stones leetcode solution in c++ 
Cpp :: declare a structer in cpp 
Cpp :: online compiler cpp 
Cpp :: what is c++ function 
Cpp :: sort c++ array 
Cpp :: function prototype c++ 
Cpp :: cpp tutorial 
Cpp :: qt how to make a file browser 
Cpp :: vector erase iterator 
Cpp :: create a bitset of 1024 bits, 
C :: find string in all files powershell 
C :: factorial in c 
C :: roll binary c 
C :: Numeri in ordine crescente C 
C :: remove from string c 
C :: factorial in c using recursion 
C :: unity set transform position code 
C :: read from a file c 
C :: xor swap 
C :: initialize array in c with 0 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =