poj1469:http://poj.org/problem?id=1469
COURSES
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 19967 | Accepted: 7860 |
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 possible to form a
committee
(委员会) of exactly P students that satisfies
simultaneously
(同时地) the conditions:
- every student in the committee represents a different course (a student can represent a course if he/she visits that course)
- each course has a representative(代表) in the committee
Input
Your program should read sets of data from the std
input
(投入). The first line of the input contains the number of the data sets. Each data set is presented in the following format:
P N
Count1 Student 1 1 Student 1 2 ... Student 1 Count1
Count2 Student 2 1 Student 2 2 ... Student 2 Count2
...
CountP Student P 1 Student P 2 ... Student P CountP
The first line in each data set contains two positive (积极的) integers (整数) separated by one blank: P (1 <= P <= 100) - the number of courses and N (1 <= N <= 300) - the number of students. The next P lines describe in sequence (序列) of the courses �from course 1 to course P, each line describing a course. The description of course i is a line that starts with an integer Count i (0 <= Count i <= N) representing the number of students visiting course i. Next, after a blank, you抣l find the Count i students, visiting the course, each two consecutive (连贯的) separated by one blank. Students are numbered with the positive integers from 1 to N.
There are no blank lines between consecutive sets of data. Input (投入) data are correct.
P N
Count1 Student 1 1 Student 1 2 ... Student 1 Count1
Count2 Student 2 1 Student 2 2 ... Student 2 Count2
...
CountP Student P 1 Student P 2 ... Student P CountP
The first line in each data set contains two positive (积极的) integers (整数) separated by one blank: P (1 <= P <= 100) - the number of courses and N (1 <= N <= 300) - the number of students. The next P lines describe in sequence (序列) of the courses �from course 1 to course P, each line describing a course. The description of course i is a line that starts with an integer Count i (0 <= Count i <= N) representing the number of students visiting course i. Next, after a blank, you抣l find the Count i students, visiting the course, each two consecutive (连贯的) separated by one blank. Students are numbered with the positive integers from 1 to N.
There are no blank lines between consecutive sets of data. Input (投入) data are correct.
Output
The result of the program is on the standard
output
(输出). For each input data set the program prints on a single line "YES" if it is possible to form a
committee
(委员会) and "NO" otherwise. There should not be any leading blanks at the start of the line.
Sample Input
2 3 3 3 1 2 3 2 1 2 1 1 3 3 2 1 3 2 1 3 1 1
Sample Output
YES NO题意:输入P,N;下面P行第一个代表课程K,后面有K个人……问是否每个课程都有人代表。
典型的二分匹配。
关于二分匹配:http://dsqiu.iteye.com/blog/1689505
(这题并没有体现出Hopcroft-Karp算法在时间复杂度上的优越性)
(一)匈牙利算法
(1)、DFS解法:好理解,写起来简单
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <cmath>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 350;
int graph[MAXN][MAXN];
int vis[MAXN],cx[MAXN],cy[MAXN];
int n,p;
int path(int u) // 找增广路
{
for(int i = 1; i <= n; i++)
{
if(graph[u][i] && !vis[i])
{
vis[i] = 1;
if(cy[i] == -1 || path(cy[i]))
{
cy[i] = u;
cx[u] = i;
return 1;
}
}
}
return 0;
}
int dfs()
{
int cnt = 0;
memset(cx,0xff,sizeof(cx)); // 0xff 代表-1 哦;
memset(cy,0xff,sizeof(cy));
for(int i = 1; i <= p; i++) // P对N的匹配(P->N)
{
if(cx[i] == -1)
{
memset(vis,0,sizeof(vis));
cnt += path(i);
}
}
return cnt ;
}
int main()
{
// freopen("in.txt","r",stdin);
int t;
scanf("%d",&t);
while(t --)
{
memset(graph,0,sizeof(graph));
int falg = 0;
scanf("%d%d",&p,&n);
for(int i = 1; i <= p; i++)
{
int k;
scanf("%d",&k);
for(int j = 1; j <= k; j++)
{
int m;
scanf("%d",&m);
graph[i][m] = 1;
}
}
int cnt = dfs();
if(cnt == p)printf("YES\n");
else printf("NO\n");
}
return 0;
}
(二)Hopcroft-Karp算法
直接上模板
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <cmath>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 350;
int graph[MAXN][MAXN];
int vis[MAXN];
int cx[MAXN],cy[MAXN],dx[MAXN],dy[MAXN];
int n,p,dis;
int Search()
{
queue<int >Q;
dis = INF;
memset(dx,-1,sizeof(dx));
memset(dy,-1,sizeof(dy));
for(int i = 1; i <= p; i++)
{
if(cx[i] == -1)Q.push(i),dx[i] = 0;
}
while(!Q.empty())
{
int u = Q.front();
Q.pop();
if(dy[u] > dis)break;
for(int v = 1; v <= n; v++)
{
if(graph[u][v] && dy[v] == -1)
{
dy[v] = dx[u] + 1;
if(cy[v] == -1 )dis = dy[v];
else
{
dx[cy[v]] = dy[v] + 1;
Q.push(cy[v]);
}
}
}
}
return dis != INF;
}
int DFS(int u)
{
for(int v = 1; v <= n; v ++)
{
if(graph[u][v] && !vis[v] && dy[v] == dx[u] + 1)
{
vis[v] = 1;
if(cy[v] != -1 && dy[v] == dis)continue;
if(cy[v] == -1 || DFS(cy[v]))
{
cy[v] = u;
cx[u] = v;
return 1;
}
}
}
return 0;
}
int MaxMatch()
{
int cnt = 0;
memset(cx,0xff,sizeof(cx));
memset(cy,0xff,sizeof(cy));
while(Search())
{
memset(vis,0,sizeof(vis));
for(int i = 1; i <= p; i++)
if(cx[i] == -1 && DFS(i))cnt ++;
}
return cnt ;
}
int main()
{
// freopen("in.txt","r",stdin);
int t;
scanf("%d",&t);
while(t --)
{
memset(graph,0,sizeof(graph));
scanf("%d%d",&p,&n);
for(int i = 1; i <= p; i++)
{
int k;
scanf("%d",&k);
for(int j = 1; j <= k; j++)
{
int m;
scanf("%d",&m);
graph[i][m] = 1;
}
}
int cnt = MaxMatch();
if(cnt == p)printf("YES\n");
else printf("NO\n");
}
return 0;
}