I attempted this question as a practice for dfs and now I am getting WA. This is my first graph problem.I tried the test cases given in comments, but got the correct answer and now I have no idea what I am doing wrong. Can anyone help me with this.
This is my code.
#include<bits/stdc++.h>
using namespace std;
vector<bool> visited;
vector<vector<int> > graph;
vector<char> gender;
int dfs(int a)
{
visited[a] =true;
for(vector<int>::iterator it = graph[a].begin();it!=graph[a].end();it++)
{
if(gender[a]==gender[*it])
return 0;
if(gender[*it]=='u')
{
if(gender[a]=='m')
gender[*it]= 'f';
else
gender[*it]='m';
}
if(!visited[*it])
dfs(*it);
}
return 1;
}
int main()
{
int t;
scanf("%d",&t);
for(int jk = 1;jk<t+1;jk++)
{
int n,m;
scanf("%d %d",&n,&m);
visited = vector<bool> (n,0);
graph = vector<vector<int> > (n);
gender = vector<char> (n,'u');
for(int i = 0;i<m;i++)
{
int u,v;
scanf("%d %d",&u,&v);
u--;v--;
graph[u].push_back(v);
graph[v].push_back(u);
}
int ans=1;
for(int i =0;i<n;i++)
{
if(visited[i])
continue;
else
{
gender[i]='m';
ans = ans and dfs(i);
}
}
if(ans)
printf("Scenario #%d:\nNo suspicious bugs found!\n",jk);
else
printf("Scenario #%d:\nSuspicious bugs found!\n",jk);
}
return 0;
}