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

题目:问course是否有完美匹配

/************************************************
Author        :DarkTong
Created Time  :2016/7/30 22:28:35
File Name     :Poj_1469.cpp
*************************************************/

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>

#define INF 0x3f3f3f3f
#define esp 1e-9
typedef long long LL;
//using namespace std;
#define min(a, b) a<b?a:b
#define max(a, b) a>b?a:b
const int maxn = 300 + 10;
int W[maxn*3][maxn], n, m;
int left[maxn];
bool used[maxn];
bool match(int i)
{
    for(int j=1;j<=n;++j) if(W[j][i]&&!used[j])
    {
        used[j] = true;
        if(!left[j]||match(left[j]))
        {
            left[j] = i;
            return true;
        }
    }
    return false;
}
int hungary()
{
    int res=0;
    memset(left, 0, sizeof(left));
    for(int i=1;i<=m;++i)
    {
        memset(used, 0, sizeof(used));
        if(match(i)) res++;
    }
    return res;
}
int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        memset(W, 0, sizeof(W));

        scanf("%d%d", &m, &n);
        for(int i=1;i<=m;++i)
        {
            int t, x;
            scanf("%d", &t);
            for(int j=0;j<t;++j)
            {
                scanf("%d", &x);
                W[x][i]=1;
            }
        }
        if(hungary()==m) puts("YES");
        else puts("NO");

    }
    return 0;
}

时间: 2024-12-19 12:14:09

Poj 1469 COURSES -二分图最大匹配裸题的相关文章

POJ 1469 COURSES 二分图最大匹配

就是判断一下是不是每一个课程都能找到自己的代表人,做一遍最大匹配看看匹配数是否等于p即可 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cs

POJ 1469 COURSES (二分图最大匹配 匈牙利算法)

COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18892   Accepted: 7455 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 [二分图匈牙利算法]

COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24919   Accepted: 9679 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

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 (二分匹配)

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

poj2239 Selecting Courses --- 二分图最大匹配

匈牙利算法模板题 有n门课程,每门课程可能有不同一时候间,不同一时候间的课程等价. 问不冲突的情况下最多能选多少门课. 建立二分图,一边顶点表示不同课程,还有一边表示课程的时间(hash一下). #include <iostream> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <algorithm> #include

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];

51nod 2006 飞行员配对(二分图最大匹配) 裸匈牙利算法 求二分图最大匹配题

题目: 题目已经说了是最大二分匹配题, 查了一下最大二分匹配题有两种解法, 匈牙利算法和网络流. 看了一下觉得匈牙利算法更好理解, 然后我照着小红书模板打了一遍就过了. 匈牙利算法:先试着把没用过的左边的点和没用过的右边的点连起来, 如果遇到一个点已经连过就试着把原来的拆掉 把现在这条线连起来看能不能多连上一条线. 总结来说就是试和拆,试的过程很简单,拆的过程由于使用递归写的,很复杂.很难讲清楚,只能看代码自己理会. 代码(有注释): #include <bits\stdc++.h> usin