POJ 1466 Girls and Boys (maximum independent point set)

Source: Internet
Author: User

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>

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.