competitivecoder's blog

By competitivecoder, history, 9 years ago, In English

I have been trying to debug my code for hours still don't know why my code is printing wrong answer.Actually I am trying to find number of connected components of a graph.Please help me in this regard.Thank you _/_ .Question link

  • Vote: I like it
  • -8
  • Vote: I do not like it

| Write comment?
»
9 years ago, # |
  Vote: I like it +8 Vote: I do not like it

You have already transformed your input from 'A'-'Z' to 0-25 at

        while((cin>>s))
        {  
            G[s[0]-'A'].pb(s[1]-'A');
            G[s[1]-'A'].pb(s[0]-'A');
        }

but in dfs you try transform data too:

        if(color[G[s][i] - 'A']==-1)
        {  
            dfs((G[s][i] - 'A') ,col);
        }

So you need to remove "-A" transform in dfs and your code will be ok:

        if(color[G[s][i]]==-1)
        {  
            dfs(G[s][i],col);
        }