C++ | Maps Used. Easy


#1
int Solution::solve(int A, vector<vector<int> > &B) {
    map<int, int> degree;
    map<int, int> parent;
    int mx = INT_MIN;
    for(int i = 0; i<B.size(); i++){
        int parent_ = B[i][0];
        int child_ = B[i][1];
        degree[parent_]++;
        parent[child_] = parent_;
        mx = max({mx, parent_, child_});
    }
    int cnt = 0;
    for(int i = mx; i>1; i--){
        if(degree[i]%2){
            cnt++;
            degree[parent[i]]--;
        }
    }
    return cnt;
    
}

#2

can you elaborate how you came up with this clean solution thanks in advance


#3

This solution fails if any of value from B[j][[1] or B[j][1] is near to INT_MAX
Try
6
5 2 1 2 1 3 1 4 3 5 4 999999999