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