Question: determine whether an image is a bipartite graph.
--> Direct dfs.
[Cpp] # include <cstdio>
# Include <cstring>
# Include <vector>
Using namespace std;
Const int maxn = 2000 + 10;
Int color [maxn], n, m;
Bool vis [maxn];
Vector <int> G [maxn];
Bool dfs (int u) // determines whether the connected branch of u is a bipartite graph.
{
Vis [u] = 1;
Int I, len = G [u]. size ();
For (I = 0; I <len; I ++)
{
Int v = G [u] [I];
If (color [u] = color [v]) return false;
If (! Color [v])
{
Color [v] = 3-color [u];
If (! Dfs (v) return false;
}
}
Return true;
}
Int main ()
{
Int cnt = 1, T, I, x, y;
Scanf ("% d", & T );
While (T --)
{
Scanf ("% d", & n, & m );
For (I = 0; I <= n; I ++) G [I]. clear ();
For (I = 1; I <= m; I ++)
{
Scanf ("% d", & x, & y );
G [x]. push_back (y );
G [y]. push_back (x );
}
Memset (color, 0, sizeof (color ));
Memset (vis, 0, sizeof (vis ));
Printf ("Scenario # % d: \ n", cnt ++ );
Bool OK = 1;
For (I = 1; I <= n; I ++)
{
If (! Vis [I])
{
Color [I] = 1;
If (! Dfs (I ))
{
OK = 0;
Break;
}
}
}
If (OK) printf ("No suspicious bugs found! \ N ");
Else printf ("Suspicious bugs found! \ N ");
}
Return 0;
}
# Include <cstdio>
# Include <cstring>
# Include <vector>
Using namespace std;
Const int maxn = 2000 + 10;
Int color [maxn], n, m;
Bool vis [maxn];
Vector <int> G [maxn];
Bool dfs (int u) // determines whether the connected branch of u is a bipartite graph.
{
Vis [u] = 1;
Int I, len = G [u]. size ();
For (I = 0; I <len; I ++)
{
Int v = G [u] [I];
If (color [u] = color [v]) return false;
If (! Color [v])
{
Color [v] = 3-color [u];
If (! Dfs (v) return false;
}
}
Return true;
}
Int main ()
{
Int cnt = 1, T, I, x, y;
Scanf ("% d", & T );
While (T --)
{
Scanf ("% d", & n, & m );
For (I = 0; I <= n; I ++) G [I]. clear ();
For (I = 1; I <= m; I ++)
{
Scanf ("% d", & x, & y );
G [x]. push_back (y );
G [y]. push_back (x );
}
Memset (color, 0, sizeof (color ));
Memset (vis, 0, sizeof (vis ));
Printf ("Scenario # % d: \ n", cnt ++ );
Bool OK = 1;
For (I = 1; I <= n; I ++)
{
If (! Vis [I])
{
Color [I] = 1;
If (! Dfs (I ))
{
OK = 0;
Break;
}
}
}
If (OK) printf ("No suspicious bugs found! \ N ");
Else printf ("Suspicious bugs found! \ N ");
}
Return 0;
}