C++ union bound solution


  • 0
    M
    class Solution {
    public:
        bool validTree(int n, vector<pair<int, int>>& edges) {
            if(edges.size() != n-1)
                return false;
            vector<int> result(n);
            iota(result.begin(),result.end(), 0);
            for(int i=0;i<edges.size();i++){
                if(result[edges[i].first] != result[edges[i].second]){
                    int temp = result[edges[i].first];
                    for(int j=0;j<n;j++){
                        if(result[j] == temp){
                            result[j] = result[edges[i].second];
                        }
                    }
                }
            }
            for(int i=0;i<n-1;i++){
                if(result[i]!=result[i+1])
                    return false;
            }
            return true;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.