One of simplest c++ code


#1

int find1(int a1[],int x)
{
if(a1[x]==-1) return x;
return a1[x]=find1(a1,a1[x]);
}

int Solution::solve(int a, vector &b, vector &c)
{
int i,j,k,n=b.size();
int a1[a+1];
for(i=1;i<a+1;i++) a1[i]=-1;
for(i=0;i<n;i++)
{
int p=b[i],q=c[i];
if(find1(a1,p)==find1(a1,q))
{
return 0;
}
else
{
a1[p]=q;
}
}
return 1;
}


#2

hey this solution is being accepted but it is not correct
try
1->3->4->5->1->2

5
5 1 3 4 1 5
5 3 4 5 2 1

your code gives 1 but the expected is 0.