Acm hdu 2444 the accomodation of students (determine whether it is a two-part graph, and then find the maximum matching)

Source: Internet
Author: User

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2444

Question:

Yes N Students, including M People know each other. Each person can be assigned a room and asked if they can N The students are divided into two parts. The students in each part do not know each other, but the students in the two parts do not know each other. If it can be divided into two parts, the maximum number of rooms is calculated; otherwise, the output is No .

Analysis:

First, determine whether it is a two-part graph, and then find the maximum matching.

 

HereProgramHungary ()UseVectorImplementation

 /*  
HDU 2444 the accomodation of Students

*/
# Include < Iostream >
# Include < String . H >
# Include < Vector >
Using Namespace STD;
# Define Maxn 202
Vector < Int > Ev [maxn];
Int Linker [maxn];
Bool Used [maxn];
Int UN;
Int Matchs [maxn], CNT [maxn];
Bool DFS ( Int U)
{
Int I;
For (I = 0 ; I < Ev [u]. Size (); I ++ )
{
Int V = Ev [u] [I];
If ( ! Used [v])
{
Used [v] = True ;
If (Linker [v] =- 1 | DFS (linker [v])
{
Linker [v] = U;
Return True ;
}
}
}
Return False ;
}
Int Hungary ()
{
Int Res = 0 ;
Int U;
Memset (linker, - 1 , Sizeof (Linker ));
For (U = 1 ; U <= UN; u ++ )
{
Memset (used, False , Sizeof (Used ));
If (DFS (u) res ++ ;
}
Return Res;
}
Bool Judge ( Int X, Int Y)
{
Int I;
For (I = 0 ; I < Ev [X]. Size (); I ++ )
{
If (CNT [EV [x] [I] = 0 )
{
CNT [EV [x] [I] =- 1 * Y;
Matchs [EV [x] [I] = True ;
If ( ! Judge (EV [x] [I], - 1 * Y )) Return False ;
}
Else If (CNT [EV [x] [I] = Y) Return False ;
}
Return True ;
}
Bool Matched ()
{
Int I;
Memset (matchs, False , Sizeof (Matchs ));
For (I = 1 ; I <= UN; I ++ )
{
If (EV [I]. Size () &&! Matchs [I])
{
Memset (CNT, 0 , Sizeof (CNT ));
CNT [I] =- 1 ;
Matchs [I] = True ;
If ( ! Judge (I, - 1 )) Return False ;
}
}
Return True ;
}
Int Main ()
{
Int M;
Int I;
Int U, V;
While (Scanf ( " % D " , & UN, & M) ! = EOF)
{
For (I = 1 ; I <= UN; I ++ )
If (EV [I]. Size () eV [I]. Clear ();
While (M -- )
{
Scanf ( " % D " , & U, & V );
Ev [u]. push_back (v );
Ev [v]. push_back (U );
}

If (Matched ())
Printf ( " % D \ n " , Hungary () / 2 );
Else Printf ( " No \ 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.