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];
 8 int vis[maxn];
 9 int P, N, cnt;
10
11 int dfs(int x)
12 {
13     for (int i = 1; i <= N; i++)   // 遍历学生数
14     {
15         if (!vis[i] && map[x][i])
16         {
17             vis[i] = 1;
18             if (match[i] == 0|| dfs(match[i]))
19             {
20                 match[i] = x;
21                 return 1;
22             }
23         }
24     }
25     return 0;
26 }
27
28 void Hungary()
29 {
30     cnt = 0;
31     for (int i = 1; i <= P; i++)  // 遍历课程数
32     {
33         memset(vis, 0, sizeof(vis));
34         cnt += dfs(i);
35     }
36 }
37
38 int main()
39 {
40     int T, num, stu_num;
41     while (scanf("%d", &T) != EOF)
42     {
43         while (T--)
44         {
45             memset(match, 0, sizeof(match));
46             memset(map, 0, sizeof(map));
47
48             scanf("%d%d", &P, &N);
49             for (int i = 1; i <= P; i++)
50             {
51                 scanf("%d", &num);
52                 for (int j = 1; j <= num; j++)
53                 {
54                     scanf("%d", &stu_num);
55                     map[i][stu_num] = 1;
56                 }
57             }
58             Hungary();
59             printf("%s\n", cnt == P ? "YES" : "NO");
60         }
61     }
62     return 0;
63 }

poj 1469 COURSES 解题报告,布布扣,bubuko.com

时间: 2024-08-05 15:36:32

poj 1469 COURSES 解题报告的相关文章

poj 1258 Agri-Net 解题报告

题目链接:http://poj.org/problem?id=1258 题目意思:给出 n 个 farm,每个farm 之间通过一定数量的fiber 相连,问使得所有farm 直接或间接连通的 最少 fiber 数是多少. 赤裸裸的最小生成树,用prim做的. 有个地方写错,wa 了 几次. 1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 5 const int farm_num = 10000

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 2923 Relocation 解题报告

题目链接:http://poj.org/problem?id=2923 题目意思:给出两部卡车能装的最大容量,还有n件物品的分别的weight.问以最优方式装入,最少能运送的次数是多少. 二进制表示物品状态:0表示没运走,1表示已被运走. 枚举出两辆车一趟可以运出的状态.由于物品是一趟一趟运出来的.所以就可以由一个状态通过两辆车一趟的状态转移到另一个状态. dp[i]=MIN(dp[k]+1).k可以由两车一趟转移到i. 我是参考此人的:http://blog.csdn.net/bossup/a

poj 2253 Frogger 解题报告

题目链接:http://poj.org/problem?id=2253 题目意思:找出从Freddy's stone  到  Fiona's stone  最短路中的最长路. 很拗口是吧,举个例子.对于 i 到 j 的一条路径,如果有一个点k, i 到 k 的距离 && k 到 j 的距离都小于 i 到 j 的距离,那么就用这两条中较大的一条来更新 i 到 j 的距离 .每两点之间都这样求出路径.最后输出 1 到 2 的距离(1:Freddy's stone   2:Fiona's sto

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 1724 ROADS 解题报告

题目链接:http://poj.org/problem?id=1724 题目意思:给出一个含有N个点(编号从1~N).R条边的有向图.Bob 有 K 那么多的金钱,需要找一条从顶点1到顶点N的路径(每条边需要一定的花费),前提是这个总花费  <= K. 首先这里很感谢 yunyouxi0 ,也就是我们的ACM队长啦~~~,他一下子指出了我的错误——存储重边的错误.这条题卑鄙的地方是,有重边,discuss 中的数据过了也不一定会AC啦.大家不妨试试这组数据(队长深情奉献^_^) 2 2 2 1

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: 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 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