Hdu-1829-A Bug's Life

Source: Internet
Author: User

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;
}


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.