Max match of poj2239 Bipartite Graph

Source: Internet
Author: User

Problem: poj2239

Analysis:

This question shows the course duration for each course. Finding the maximum number of courses can be converted to the maximum matching problem in the bipartite graph.

Set set a as the course set, Set B as the class time set, and create a bipartite graph based on the input. The largest course selection book is the maximum matching number of the Bipartite Graph, which can be solved using the Hungary algorithm.

AC code

 1 //Memory: 252K        Time: 16MS 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5  6 using namespace std; 7  8 const int maxl = 301; 9 const int maxc = 85;10 int edge[maxc][maxl];11 int ne[maxc];12 int match[maxl];13 bool vis[maxl];14 int n, t;15 int a, b;16 17 bool findPath(int start)18 {19     for (int i = 0; i < ne[start]; i++){20         int current = edge[start][i];21         if (vis[current]) continue;22         vis[current] = 1;23         if ( !match[current] || findPath(match[current]) ){24             match[current] = start;25             return true;26         }27     }28     return false;29 }30 31 int solve()32 {33     memset(match, 0, sizeof(match));34     int cnt = 0;35     for (int i = 1; i <= 84; i++) {36         memset(vis, 0, sizeof(vis));37         if (findPath(i)) cnt++;38     }39     return cnt;40 }41 42 void input()43 {44     memset(edge, 0, sizeof(edge));45     memset(ne, 0, sizeof(ne));46     for (int i = 0; i < n; i++){47         scanf("%d", &t);48         while (t--){49             scanf("%d%d", &a, &b);50             int class_num = (a - 1) * 12 + b;51             edge[ class_num ][ ne[class_num]++ ] = i;52         }53     }54 }55 56 int main()57 {58     while ( ~scanf("%d", &n) ){59         input();60         int ans = solve();61         printf("%d\n", ans);62     }63     return 0;64 }

 

 

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.