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 bool Find(int u) {
15     for(int i = 0;i < G[u].size(); i++) {
16         int v = G[u][i];
17         if(!vis[v]) {
18             vis[v] = 1;
19             if(Link[v] == -1 || Find(Link[v]) ) {
20                 Link[v] = u;
21                 return true;
22             }
23         }
24     }
25     return false;
26 }
27
28 int solve() {
29     int cnt = 0;
30     memset(Link, -1, sizeof(Link));
31     for(int i = 1; i <= n; i++) {
32         memset(vis, 0, sizeof(vis));
33         if(Find(i)) cnt++;
34     }
35     return cnt;
36 }
37 int main() {
38     int t;
39     int p;
40     int num, _num;
41     scanf("%d",&t);
42     while(t--) {
43         scanf("%d %d",&n, &p);
44         for(int i = 0; i <= n; i++) {
45             G[i].clear();
46         }
47         for(int i = 1; i <= n; i++) {
48             scanf("%d",&num);
49             for(int j = 0; j < num; j++) {
50                 scanf("%d",&_num);
51                 G[i].push_back(_num);
52             }
53         }
54         if(solve() == n) puts("YES");
55         else puts("NO");
56     }
57     return 0;
58 }

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

时间: 2024-10-25 16:25:59

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 -二分图最大匹配裸题

题目:问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: 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 //简单二分图

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

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

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 COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17135   Accepted: 6730 Description Consider a group of N students and P courses. Each student visits zero, one or more than one courses. Your tas