Question: There are n students who have connections with each other. They ask you how many students can be found to form a set, so that no two students in the set have connections.
Idea: Maximum Independent Set Problem: Select m points in graph G of N points so that there is no edge between the m points. calculates the maximum m value. if graph G meets the bipartite graph condition, you can use bipartite graph matching. the maximum number of independent set points = N-the maximum number of matches/2, which is then implemented by the Hungary algorithm.
> After the split, the split is shown as the male and female are on the other side. Therefore, if the vertex is determined as one side, it cannot be connected to the vertex of the same edge. In addition, because we do not enumerate vertices on one side when looking for the maximum match, but enumerate all vertices on both sides, the ans obtained are twice the maximum number of matches, because every matching edge is calculated twice.
Code:
[Cpp]
<Span style = "font-size: 18px;" >#include <iostream>
# Include <algorithm>
# Include <cmath>
Using namespace std;
Const int MAXN = 125;
Int uN, vN;
Int map [MAXN] [MAXN];
Int match [MAXN];
Bool visit [MAXN];
Bool search (int u ){
Int v;
For (v = 1; v <= vN; v ++)
If (map [u] [v] &! Visit [v]) {
Visit [v] = true;
If (match [v] =-1 | search (match [v]) {
Match [v] = u;
Return true;
}
}
Return false;
}
Int hungary (){
Int res = 0;
Int u;
Memset (match,-1, sizeof (match ));
For (u = 1; u <= uN; u ++ ){
Memset (visit, 0, sizeof (visit ));
If (search (u) res ++;
}
Return res;
}
Long a [1010];
Int main (){
Int t;
Int n;
Int I, j, m, a, B;
Scanf ("% d", & t );
While (t --){
Scanf ("% d", & n, & m );
Memset (map, 0, sizeof (map ));
UN = n;
VN = n;
For (I = 0; I <m; I ++ ){
Scanf ("% d", & a, & B );
Map [a] [B] = 1;
}
Int ans = n-hungary ();
Printf ("% d \ n", ans );
}
Return 0;
}
</Span>