UVALive 3523 Knights of the Round Table (bipartite graph + dual-Connected Component)

Source: Internet
Author: User

UVALive 3523 Knights of the Round Table (bipartite graph + dual-Connected Component)

Description

Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress, and drinking with the other knights are fun things to do. therefore, it is not very surprising that in recent years the kingdom of King Arthur has experienced an unprecedented increase in the number of knights. there are so far knights now, that it is very rare that every Knight of the Round Table can come at the same time to Camelot and sit around the round table; usually only a small group of the knights isthere, while the rest are busy doing heroic deeds around the country.

Knights can easily get over-excited during discussions-especially after a couple of drinks. after some unfortunate accidents, King Arthur asked the famous wizard Merlin to make sure that in the future no fights break out between the knights. after studying the problem carefully, Merlin realized that the fights can only be prevented if the knights are seated according to the following two rules:

 

The knights shoshould be seated such that two knights who hate each other shoshould not be neighbors at the table. (Merlin has a list that says who hates whom .) the knights are sitting around und a roundtable, thus every knight has exactly two neighbors. an odd number of knights shocould sit around the table. this ensures that if the knights cannot agree on something, then they can settle the issue by voting. (If the number of knights is even, then itcan happen that ''yes "and ''no" have the same number of votes, and the argument goes on .)

Merlin will let the knights sit down only if these two rules are satisfied, otherwise he cancels the meeting. (If only one knight shows up, then the meeting is canceled as well, as one person cannot sit around a table .) merlin realized that this means that there can be knights who cannot be part of any seating arrangements that respect these rules, and these knights will never be able to sit at the Round Table (one such case is if a knight hates every other knight, but there are missing other possible reasons ). if a knight cannot sit at the Round Table, then he cannot be a member of the Knights of the Round Table and must be expelled from the order. these knights have to be transferred to a less-prestigious order, such as the Knights of the Square Table, the Knights of the Octagonal Table, or the Knights of the Banana-Shaped Table. to help Merlin, you have to write a program that will determine the number of knights that must be expelled.

 

Input

The input contains several blocks of test cases. Each case begins with a line containing two integers 1N1000 and 1M1000000. The numberNIs the number of knights. The nextMLines describe which knight hates which knight. Each of theseMLines contains two integersK1 andK2, which means that knight numberK1 and knight numberK2 hate each other (the numbersK1 andK2 are between 1 andN).

The input is terminated by a blockN=M= 0.

 

Output

For each test case you have to output a single integer on a separate line: the number of knights that have to be expelled.

 

Sample Input

 

5 51 41 52 53 44 50 0

 

Sample Output

 

2

 

Question: give you n, m n is the number of people, m is the number of groups of relationships, each group of relationships represents mutual hate.

 

Idea: we can really think about it. According to people who hate each other, we can actually connect people who do not hate each other and determine whether a dual-connected component of a connected component is a bipartite graph, if not, mark all the points as available for the meeting, indicating that this person is not allowed to attend the meeting.

It's still a bit tricky. Although I thought of the idea, I finally typed it out with reference to the template.

AC code:

 

#include
 
  #include
  
   #include
   
    #include
    
     #includeusing namespace std;#define maxn 1005struct Edge{    Edge() {}    Edge(int uu, int vv) {        u = uu;        v = vv;    }    int u, v;};int pre[maxn],iscut[maxn],bccno[maxn],dfs_clock,bcc_cnt;vector
     
       G[maxn], bcc[maxn];stack
      
       S;int dfs(int u,int fa){ //printf("hehe\n"); int lowu=pre[u]=++dfs_clock; int child=0; for(int i=0;i
       
        =pre[u]) { iscut[u]=1; bcc_cnt++; bcc[bcc_cnt].clear(); while(1) { Edge x=S.top(); S.pop(); if(bccno[x.u]!=bcc_cnt) { bcc[bcc_cnt].push_back(x.u); bccno[x.u]=bcc_cnt; } if(bccno[x.v]!=bcc_cnt) { bcc[bcc_cnt].push_back(x.v); bccno[x.v]=bcc_cnt; } if(x.u==u&&x.v==v)break; } } } else if(pre[v]
        

 

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.