POJ-1466 girls and boys bipartite diagram + split point diagram

Source: Internet
Author: User

Question: many male and female students like each other and now require the largest independent subset.

Solution: As the question shows that this kind of relationship exists only between men and women, you can boldly split the diagram, because men must like women, however, women cannot have edges (like relationships ). Maximum Independent Subset = number of vertices-Maximum number of matching. Because it is a split-point diagram, We need to divide the final result by 2.

The Code is as follows:

# Include <cstdlib> # include <cstring> # include <cstdio> # include <iostream> # include <algorithm> using namespace STD; int N, Match [505]; char G [505] [505], vis [505]; bool path (int u) {for (INT I = 0; I <n; ++ I) {If (! G [u] [I] | Vis [I]) continue; vis [I] = 1; if (! (~ Match [I]) | path (Match [I]) {match [I] = u; return true ;}return false ;} int query () {// number starting from 0, initialized to-1 int ret = 0; memset (match, 0xff, sizeof (MATCH); For (INT I = 0; I <N; ++ I) {memset (VIS, 0, sizeof (VIS); RET + = path (I);} return N-(Ret> 1 );} int main () {int ID, num, C; while (scanf ("% d", & N )! = EOF) {memset (G, 0, sizeof (g); For (INT I = 0; I <n; ++ I) {scanf ("% d: (% d) ", & ID, & num); For (Int J = 0; j <num; ++ J) {scanf (" % d ", & C ); G [ID] [c] = 1; // equivalent to the split point diagram} printf ("% d \ n", query ();} 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.