The objective is to find whether a undirected, unweighted graph is a tree, i.e. check whether it contains a back edge. I'm using a modified form of White-Gray-Black DFS algorithm(which is given in Cormen and on the notes mentioned here: http://www.cs.cornell.edu/~wdtseng/icpc/notes/graph_part1.pdf )
But somehow, it doesn't work. It worked once, but it resulted in a Run Time Error at SPOJ (http://www.spoj.com/problems/PT07Y/).
Update: I'm now getting a WRONG answer for this problem. The code works for the sample test cases though.
Test case where it fails:
10 9
1 2
2 3
3 4
4 5
5 6
6 4
6 7
7 8
8 9
#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <vector>
#include <cstdlib>
using namespace std;
bool M[10001][10001] = { 0 } ;
int color[10001] = { 0 };
long long int n;
bool answer=false;
void condition()
{
cout<<"NO"<<endl;
exit(0);
}
void condition2()
{
cout<<"YES"<<endl;
exit(0);
}
void dfs(int u, int p)
{
color[u] = 1;
for(int v=0; v < n; v++)
{
if(M[u][v] && v != p)
{
if (color[v] == 0)
dfs(v, u);
else if (color[v]==1)
{
condition(); /* It has a backedge so it is not a tree, so print No */
}
}
}
condition2(); /* It does not have backedge so it is not a tree so print YES */
}
int main()
{
long Z;
cin>>n>>Z;
// for(int i=0; i<n; i++) /* **Removed THIS nested loop to reduce runtime, successfully eliminated TLE** */
// for(int j=0; j<n;j++)
// M[i][j]=0;
for(int i=0; i < Z; i++)
{
long temp1, temp2;
cin>>temp1;
cin>>temp2;
temp1--;
temp2--;
M[temp1][temp2]=1;
M[temp2][temp1]=1;
}
if(Z==n-1)
dfs(0, -1);
else cout<<"NO"<<endl;
return 0;
}
color
should probably have 20001 elements. And you usen
before it has a value. – Bill Lynchu
,p
andv
. – Bill Lynchn
in the loop condition. And unless I'm mistaken,color
is still an array of 128bool
s. – Bill LynchM[temp1][temp2]=1;
andM[temp2][temp1]=1;
? – Bill Lynch