basically the code searches for a path in the graph using dfs…that there is no node repeated in the path…
(for ex. 1->2->3->4->5)for 5 nodes…and runs for all 5 nodes…if there is a path with no node repeated than it prints yes else prints no …i don’t know why u am getting wrong answer
#include<stdio.h>
int a[101][101],reach[101],n,visit[101];
void dfs(int v)
{
int i;
reach[v]=1;
for(i=1;i<=n;i++)
if(a[v][i] && !reach[i])
{
visit[v]++;
dfs(i);
}
}
int main()
{
int i,j,count=0,m,t,x,y,count2;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
{
reach[i]=0;
for(j=1;j<=n;j++)
a[i][j]=0;
visit[i]=0;
}
for(i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
if(x!=y)
{
a[x][y]=1;
a[y][x]=1;
}
}
for(i=1;i<=n;i++)
{
count=0;count2=0;
for(j=1;j<=n;j++)
{
visit[j]=0;
reach[j]=0;
}
dfs(i);
for(j=1;j<=n;j++)
{
if(reach[j])
count++;
if(visit[j]==1)
count2++;
}
if(count==n&&count2==n-1&&m>=n&&n>1)
break;
}
if(i==n+1)
printf("NO\n");
else
printf("YES\n");
}
return 0;
}