Thanks for reading this blog.Actually i am stuck in a problem which can be considered as an easy dsu problem.What i am stuck with is my code.I am unable to figure out what is wrong with my logic.The problem statement says: Problem Description
Rishabh has a permutation A of N integers 1, 2, ... N but he doesn't like it. Rishabh wants to get a permutation B.
Also, Rishabh has some M good pairs given in a form of 2D matrix C of size M x 2 where (C[i][0], C[i][1]) denotes that two indexes of the permutation A.
In one operation he can swap Ax and Ay only if (x, y) is a good pair.
You have to tell whether Rishabh can obtain permutation B by performing the above operation any number of times on permutation A.
If the permutation B can be obtained return 1 else return 0.
Problem Constraints
2 <= N <= 105 1 <= M <= 105 1 <= A[i], B[i] <= N A[i] and B[i] are all distinct. 1 <= C[i][0] < C[i][1] <= N
Input Format
First argument is an integer array A of size N denoting the permutation A.
Second argument is an integer array B of size N denoting the permutation B.
Third argument is an 2D integer array C of size M x 2 denoting the M good pairs.
Output Format
If the permutation B can be obtained return 1 else return 0.
My solution : int Solution::solve(vector &A, vector &B, vector<vector > &C) { vector<vector>g(A.size()+1); vectorvis(A.size()+1);
for(int i = 0;i < C.size();i++){
g[A[C[i][0]-1]].push_back(A[C[i][1]-1]);
g[A[C[i][1]-1]].push_back(A[C[i][0]-1]);
}
function <void(int)>dfs = [&](int node){
vis[node] = true;
for(auto child : g[node])
if(!vis[child])dfs(child);
};
for(int i = 0;i < B.size();i++){
if(B[i] != A[i] && (!vis[B[i]] && !vis[A[i]])){
dfs(B[i]);
if(!vis[A[i]]){
return 0;
}
}
if(vis[B[i]] && !vis[A[i]]){
return 0;
}
}
return 1;} Can anyone help me point out if there is any mistake with my logic or code?I am not getting an AC with this code.



