zoukankan      html  css  js  c++  java
  • POJ

    题意:

      P门课程,N个学生。给出每门课程的选课学生,求是否可以给每门课程选出一个课代表。课代表必须是选了该课的学生且每个学生只能当一门课程的。

    题解:

      匈牙利算法的入门题。

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <vector>
    using namespace std;
    const int maxn = 1005;
    int t; 
    int k, s;
    int flag;
    int p, n;
    int vis[maxn];
    int match[maxn];
    vector<int> g[maxn];
    void init() {
        flag = 0;
        memset(match, -1, sizeof(match));
        for(int i = 1; i <= p+n; i++) g[i].clear();
    }
    bool dfs(int u) {
        vis[u] = 1;
        for(int i = 0; i < g[u].size(); i++) {
            int v = g[u][i], w = match[v];
            if(w<0 || !vis[w]&&dfs(w)) {
                match[u] = v;
                match[v] = u;
                return true;
            }
        }
        return false;
    }
    int hungarian() {
        int res = 0;
        for(int u = 1; u <= s+p; u++) {
            if(match[u] < 0) {
                memset(vis, 0, sizeof(vis));
                if(dfs(u)) res++;
            }
        }
        return res;
    }
    int main() {
        scanf("%d", &t);
        while(t--) {
            init();
            scanf("%d%d", &p, &n);
            for(int i = 1; i <= p; i++) {
                scanf("%d", &k);
                if(!k) flag = 1;
                while(k--) {
                    scanf("%d", &s);
                    g[i].push_back(s+p);
                    g[s+p].push_back(i); 
                }
            }
            if(flag || n<p) {
                puts("NO");
                continue;
            }
            int ans = hungarian();
            if(ans == p) puts("YES");
            else puts("NO");
        }
    } 
    View Code
  • 相关阅读:
    《人月神话》阅读笔记2
    【个人作业】单词链
    【个人作业】找水王
    【团队】 冲刺一(10/10)
    【团队】 冲刺一(9/10)
    【个人作业】单词统计续
    【团队】 冲刺一(8/10)
    【团队】 冲刺一(7/10)
    【团队】 冲刺一(6/10)
    【团队】 冲刺一(5/10)
  • 原文地址:https://www.cnblogs.com/Pneuis/p/8688618.html
Copyright © 2011-2022 走看看