POJ - 1469 COURSES (匈牙利算法入门题)

题意:

  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");
    }
} 

原文地址:https://www.cnblogs.com/Pneuis/p/8688618.html

时间: 2024-10-18 22:26:10

POJ - 1469 COURSES (匈牙利算法入门题)的相关文章

poj 1469 COURSES(匈牙利算法模板)

http://poj.org/problem?id=1469 COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19419   Accepted: 7642 Description Consider a group of N students and P courses. Each student visits zero, one or more than one courses. Your task is

Poj 1469 COURSES -二分图最大匹配裸题

题目:问course是否有完美匹配 /************************************************ Author :DarkTong Created Time :2016/7/30 22:28:35 File Name :Poj_1469.cpp *************************************************/ #include <cstdio> #include <cstring> #include <

POJ 1469 COURSES //简单二分图

COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17211   Accepted: 6769 Description Consider a group of N students and P courses. Each student visits zero, one or more than one courses. Your task is to determine whether it is poss

poj 1469 COURSES 解题报告

题目链接:http://poj.org/problem?id=1469 题目意思:略 for 循环中遍历的对象要特别注意,究竟是遍历课程数P 还是 学生数N,不要搞混! 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 6 const int maxn = 300 + 5; 7 int match[maxn], map[maxn][maxn];

POJ 3041 Asteroids (匈牙利算法)

Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14388 Accepted: 7828 Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K astero

poj 1469 COURSES (二分匹配)

COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16877   Accepted: 6627 Description Consider a group of N students and P courses. Each student visits zero, one or more than one courses. Your task is to determine whether it is poss

POJ 1469 COURSES【二分图最大匹配】

分析:二分图最大匹配 代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 using namespace std; 6 7 const int maxn = 305; 8 9 int n; 10 11 vector<int> G[maxn]; 12 int vis[maxn]; 13 int Link[maxn]; 14

hdu 1083 匈牙利算法模板题

题意:有p个课程,n个学生,每个课程有x个学生喜欢.如果每个课程都至少可以分配一个学生就输出YES,否则输出NO. 题解:匈牙利算法裸题. 代码: #include <cstdio>#include <cstring>#define N 400int Map[N][N],S[N],vis[N],n;bool dfs(int u){    for(int i=1;i<=n;i++){        if(Map[u][i]&&!vis[i]){         

hdu 2063 匈牙利算法模板题

题意:有x个男生各自有喜欢的女生,y个女生各自有喜欢的男生.互相喜欢的在一起有好感度.问怎样好感度最高. 题解:匈牙利算法裸题. 代码: #include <cstdio>#include <cstring>#define N 1500int Map[N][N],M[N],vis[N];int k,m,n;bool dfs(int u){    for(int i=1;i<=n;i++){        if(Map[u][i]&&!vis[i]){