poj 1469 COURSES

来源:互联网 发布:skrillex用什么软件 编辑:程序博客网 时间:2024/04/30 07:43

题意:给出p个课程,n个学生,问能否满足每个学生选一门不同的课,且每个课程有一个学生。


分析:把这个问题看成二分图来做的话,简直就是裸题~。求出最大匹配看是否和p相等,相等输出YES,否则输出NO。

第一道二分图,用了匈牙利算法求出了最大匹配。


以下附上代码:


#include <algorithm>#include <iostream>#include <sstream>#include <fstream>#include <cstring>#include <cstdio>#include <vector>#include <cctype>#include <cmath>#include <stack>#include <queue>#include <list>#include <map>#include <set>using namespace std;typedef long long ll;typedef unsigned long long ull;const int maxn = 305;int g[maxn][maxn];int p,n;int t;int cx[maxn],cy[maxn];//cx->student cy->coursebool vis[maxn];// void input(){  int i,j;  int k;  int v;    for(i = 0; i < maxn; i++){    for(j = 0; j < maxn; j++){      g[i][j] = 0;    }  }    scanf("%d%d",&p,&n);  for(i = 1; i <= p; i++){    scanf("%d",&k);    for(j = 0; j < k; j++){      scanf("%d",&v);      g[v][i] = 1;    }  }  }//寻找可增广轨增广 int path(int u){  for(int v = 1; v <= p; v++){    if(g[u][v] && !vis[v]){      vis[v] = 1;      if(cy[v] == -1 || path(cy[v])){        cx[u] = v;        cy[v] = u;        return 1;//找到可增广轨       }    }  }  return 0;//不存在可增广轨 }//匈牙利算法 int maxMatch(){  int res = 0;  fill(cx,cx+maxn,-1);  fill(cy,cy+maxn,-1);    for(int i = 1; i <= n; i++){    if(cx[i] == -1){//当前没匹配       fill(vis,vis+maxn,0);      res += path(i);    }  }  return res;}bool solve(){  return maxMatch() == p;}int main(){  scanf("%d",&t);  while(t--){    input();    puts(solve() ? "YES" : "NO");  }return 0;}

0 0
原创粉丝点击