HihoCoder-1121-bipartite graph Determination

Source: Internet
Author: User

HihoCoder-1121-bipartite graph Determination

 

 

#1121: Bipartite Graph 1 • bipartite graph determination time limit: 10000 ms single point time limit: 1000 ms memory limit: 256 MB
Description

Hello everyone, I am Nettle, a friend of Xiao Hi and Xiao Ho. I will finish our Weekly from this week.

New Year's home, again to the annual aging male leftover female dating time. When Nettle went to his aunt's house to play, he saw a dating table written by her aunt. The table above showed her aunt's leftover male and female. Each row has two names, indicating that the two have a blind date. Because my aunt is too old to remember, and she has a lot of blind dates, she cannot remember the gender of some of them at the moment. Therefore, she asked me to check whether there were any wrong records in the blind date table, that is, whether two identical dates were arranged.

OK. Let's make a pleasant and violent search!

That's strange.

We may wish to convert the form into a graph. Take each person as a vertex (1. N). If two people have a blind date, a undirected edge is connected between the corresponding vertex. (For example)

Because dating is always performed between men and women, the two sides of each edge correspond to different genders. It is assumed that male nodes are white and female nodes are black. For an undirected graph, the two ends of each edge must be white and black. If both ends of an edge are white or black, it indicates that the record indicated by this edge is incorrect.

Because we do not know the gender of each person, our problem is to determine whether there is a reasonable dyeing scheme, this makes the undirected graph we have created meet the condition that the color of the vertices at both ends of each edge is different.

Then, we may wish to initialize all vertices into the unstained state. Select a random vertex and dye it white. Start with it and then dye all adjacent points in black. Starting from these black points, all unstained points adjacent to them are dyed white. Repeat until the entire graph is stained. (For example)

In the dyeing process, how should we find the error records? I believe you have discovered it. For a vertex that has been colored, if there is an adjacent vertex that has the same color as it, there must be an incorrect record. (For example, nodes 4 and 5)

Now we get the algorithm of the entire graph:

 

  1. Select an unstained dot u for dyeing.
  2. Traverse adjacent node v of u: If v is not stained, the color is different from that of u, and step 5 is repeated for v. If v is stained, if u and v are in the same color, determine that the traversal is not feasible.
  3. If all nodes are stained, it is feasible.

    Next, let's write it!

    Input

    Row 3: 1 positive integer T (1 ≤ T ≤ 10)

    Next, the T group data is given in the following format:

    Row 1st: two positive integers N, M (1 ≤ N ≤ 10,000, 1 ≤ M ≤ 40,000)

    2nd. M + 1 row: each row has two integers u. v indicates that there is an edge between u and v.

    Output

    Row 1st. T: Row I indicates whether the data in Group I is incorrect. If "Correct" is output correctly, otherwise "Wrong" is output"

    Sample Input
    25 51 21 33 45 21 55 51 21 33 45 23 5
    Sample output
    WrongCorrect

     

     

     

    I haven't written code for a long time. Continue to save the template!

     

    AC code:

     

    # Include
         
          
    # Include
          
           
    # Include
           
            
    # Include
            
             
    # Include
             
              
    Using namespace std; int T; int n, m; const int maxn = 10005; vector
              
                G [maxn]; bool vis [maxn]; int f [maxn]; int bfs (int I) {queue
               
                 Que; que. push (I); vis [I] = true; f [I] = 0; while (! Que. empty () {int e = que. front (); que. pop (); int d = G [e]. size (); // cout <e <endl; for (int I = 0; I <d; I ++) {int t = G [e] [I]; // cout <t <; if (vis [t]) {if (f [t] = f [e]) return 1;} else {vis [t] = true; f [t] = f [e] = 0? 1: 0; que. push (t) ;}/// cout <endl;} return 0 ;}int fun () {// note that there may be multiple connected components for (int I = 1; I <= n; I ++) {if (! Vis [I]) {if (bfs (I) = 1) return 1 ;}} return 0 ;}int main () {scanf (% d, & T ); while (T --) {// for (int I = 0; I <maxn; I ++) G [I]. clear (); scanf (% d, & n, & m); for (int I = 0; I <m; I ++) {int u, v; scanf (% d, & u, & v); G [u]. push_back (v); G [v]. push_back (u);} memset (vis, false, sizeof (vis); if (fun () = 1) {printf (Wrong);} else printf (Correct ); // for (int I = 1; I <= n; I ++) {// cout <f [I] <; //} for (int I = 1; I <= n; I ++) G [I]. clear () ;}return 0 ;}
               
              
             
            
           
          
         


     

     

     

Related Article

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.