684. Redundant Connection

class Solution {
public:
    vector findRedundantConnection(vector>& edges) {
        int len = edges.size();
        vector parent(2000,0);
        vector ret;
        
        for(int i = 0; i < parent.size(); ++i){
            parent[i] = i;
        }
        
        /*auto completion*/
        for(auto v : edges ){
            int n1 = v[0], n2 = v[1];
            while(n1 != parent[n1]) n1 = parent[n1];
            while(n2 != parent[n2]) n2 = parent[n2];
            if( n1 == n2 )
                ret = v;
            else{
                parent[n2] = n1;
            }
           
                
                
        }
        
        
        return ret;
        
    }
};

你可能感兴趣的:(LINUX技术,算法学习)