zoukankan      html  css  js  c++  java
  • POJ 1469 COURSES 二分图最大匹配

    就是判断一下是不是每一个课程都能找到自己的代表人,做一遍最大匹配看看匹配数是否等于p即可

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <string>
    #include <iostream>
    #include <map>
    #include <cstdlib>
    #include <list>
    #include <set>
    #include <queue>
    #include <stack>
    
    using namespace std;
    
    typedef long long LL;
    const int maxn = 400;
    int p,n,bx[maxn],by[maxn],vis[maxn];
    bool g[maxn][maxn];
    
    int dfs(int now) {
        for(int i = 1;i <= n;i++) if(g[now][i] && !vis[i]) {
            vis[i] = true;
            if(!by[i] || dfs(by[i])) {
                bx[now] = i; by[i] = now;
                return 1;
            }
        }
        return 0;
    }
    
    void solve() {
        memset(bx,0,sizeof(bx));
        memset(by,0,sizeof(by));
        //为每一门课程寻找代表
        int ans = 0;
        for(int i = 1;i <= p;i++) {
            if(!bx[i]) {
                memset(vis,0,sizeof(vis));
                ans += dfs(i);
            }
        }
        if(ans == p) puts("YES");
        else puts("NO");
    }
    
    int main() {
        int T; scanf("%d",&T);
        while(T--) {
            scanf("%d%d",&p,&n);
            memset(g,0,sizeof(g));
            for(int i = 1;i <= p;i++) {
                int cnt; scanf("%d",&cnt);
                for(int j = 1;j <= cnt;j++) {
                    int tmp; scanf("%d",&tmp);
                    g[i][tmp] = true;
                }
            }
            solve();
        }
        return 0;
    }
    

      

  • 相关阅读:
    四月十五号日报
    四月十一号日报
    四月八号日报
    五月六号日报
    CCSUOJ评测系统——第四次scrum冲刺
    CCSUOJ评测系统——第三次scrum冲刺
    CCSUOJ评测系统——第二次scrum冲刺
    C# Process 进程管理
    [C#][收集整理]
    [C#][收集整理]
  • 原文地址:https://www.cnblogs.com/rolight/p/3880546.html
Copyright © 2011-2022 走看看