POJ-1422 Air Raid bipartite graph maximum matching

Source: Internet
Author: User

POJ-1422 Air Raid bipartite graph maximum matching

There are n points and m unidirectional line segments. Now I want to start from a few vertices to traverse all vertices.

Solution: The maximum matching of a bipartite graph. If one match is required, the two points are connected. You only need to select one point for the two points. Therefore, how many matches are there, the number of vertices that can be subtracted.

#include
  
   #include
   
    using namespace std;const int N = 130;int g[N][N], vis[N], link[N];int n, m;void init() {    memset(g, 0, sizeof(g));    memset(link, 0, sizeof(link));    int x, y;    for(int i = 0; i < m; i++) {        scanf("%d%d", &x, &y);        g[x][y] = 1;    }}bool dfs(int u) {    for(int i = 1; i <= n; i++) {        if(!vis[i] && g[u][i]) {            vis[i] = 1;            if(!link[i] || dfs(link[i])) {                link[i] = u;                return true;            }        }    }    return false;}void hungary() {    int ans = 0;    for(int i = 1; i <= n; i++) {        memset(vis, 0, sizeof(vis));        if(dfs(i))            ans++;    }    printf("%d\n", n - ans);}int main() {    int test;    scanf("%d", &test);    while(test--) {        scanf("%d%d", &n, &m);        init();        hungary();    }    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.