DFS + Set in C++


#1
void find(int x, set<int> &s1, set<int> &s2, vector<int> &A, vector<int> &B, vector<int> &visited, unordered_map<int, vector<int>> &m){
    int i, n = m[x].size();
    s1.insert(A[x]);
    s2.insert(B[x]);
    for(i=0;i<n;i++){
        if(visited[m[x][i]] == 0){
            visited[m[x][i]] = 1;
            find(m[x][i], s1, s2, A,B, visited, m);
        }
    }
}

int Solution::solve(vector<int> &A, vector<int> &B, vector<vector<int> > &C) {
    unordered_map<int, vector<int>> m;
    for(vector<int> x : C){
        m[x[0]-1].push_back(x[1]-1);
        m[x[1]-1].push_back(x[0]-1);
    }
    int i, j, n = A.size();
    vector<int> visited(n, 0);
    
    for(i=0;i<n;i++){
        if(visited[i] == 0){
            visited[i] = 1;
            set<int> s1, s2;
            s1.insert(A[i]);
            s2.insert(B[i]);
            find(i,s1, s2,  A, B, visited, m);
            //cout<<s1.size()<<s2.size();
            if(s1 != s2)
                return 0;
        }
    }
    return 1;
}