pku 1469 COURSES(二分匹配)

 

http://acm.pku.edu.cn/JudgeOnline/problem?id=1469

 

题目大意:学生要选修选修课,给你P个选修课,每个选修课如果都被选中了,就输出YSE,否则NO。

 

解题思路:二分匹配入门题

 

#include <iostream> #include <vector> #include <string.h> #include <stdio.h> #define Max 1005 using namespace std; bool visited[Max]; int Match[Max]; int P,N; vector<int>List[Max]; void InputData() { int i,num,val; scanf("%d%d",&P,&N); for (i=1;i<=P;i++) { scanf("%d",&num); while (num--) { scanf("%d",&val); List[i].push_back(val); } } } bool DFS(int locate) { int i,j,len = List[locate].size(); for (i=0;i<len;i++) { j = List[locate][i]; if (!visited[j]) { visited[j] = 1; if (Match[j]==-1||DFS(Match[j])) { Match[j] = locate; return true; } } } return false; } int main() { int test,i,count; scanf("%d",&test); while (test--) { InputData(); count = 0; memset(Match,-1,sizeof(Match)); for (i=1;i<=P;i++) { memset(visited,0,sizeof(visited)); if(DFS(i)) count++; } if(count==P) printf("YES/n"); else printf("NO/n"); for(i=1;i<=P;i++) List[i].clear(); } return 0; }

你可能感兴趣的:(list)