zoukankan      html  css  js  c++  java
  • poj 1469(二分图最大匹配)

    题意:学生和课程构成二部图,然后需要找到一个集合每个学生代表一门课,每一门课有一个学生代表。

    思路:二分图入门题。当最大匹配等于课程数的时候可以找到集合。

    代码如下:

     1 /**************************************************
     2  * Author     : xiaohao Z
     3  * Blog     : http://www.cnblogs.com/shu-xiaohao/
     4  * Last modified : 2014-02-20 17:48
     5  * Filename     : poj_1469.cpp
     6  * Description     : 
     7  * ************************************************/
     8 
     9 #include <iostream>
    10 #include <cstdio>
    11 #include <cstring>
    12 #include <cstdlib>
    13 #include <cmath>
    14 #include <algorithm>
    15 #include <queue>
    16 #include <stack>
    17 #include <vector>
    18 #include <set>
    19 #include <map>
    20 #define MP(a, b) make_pair(a, b)
    21 #define PB(a) push_back(a)
    22 
    23 using namespace std;
    24 typedef long long ll;
    25 typedef pair<int, int> pii;
    26 typedef pair<unsigned int,unsigned int> puu;
    27 typedef pair<int, double> pid;
    28 typedef pair<ll, int> pli;
    29 typedef pair<int, ll> pil;
    30 
    31 const int INF = 0x3f3f3f3f;
    32 const double eps = 1E-6;
    33 const int LEN = 1010;
    34 vector<int> Map[LEN];
    35 int n, match[LEN], vis[LEN];
    36 
    37 bool dfs(int u){
    38     vis[u] = 1;
    39     for(int i=0; i<Map[u].size(); i++){
    40         int v = Map[u][i], w = match[v];
    41         if((!vis[w] && dfs(w)) || w < 0){
    42             match[u] = v;
    43             match[v] = u;
    44             return true;
    45         }    
    46     }
    47     return false;
    48 }
    49 
    50 int hungary()
    51 {
    52     int ret = 0;
    53     memset(match, -1, sizeof match);
    54     for(int i=0; i<n; i++){
    55         if(match[i] >= 0) continue;
    56         memset(vis, 0, sizeof vis);
    57         if(dfs(i)) ret ++;
    58     }
    59     return ret;
    60 }
    61 
    62 int main()
    63 {
    64 //    freopen("in.txt", "r", stdin);
    65 
    66     int T, stu, cr, b;
    67     scanf("%d", &T);
    68     while(T--){
    69         for(int i=0; i<LEN; i++) Map[i].clear();
    70         scanf("%d%d", &cr, &stu);
    71         for(int i=0; i<cr; i++){
    72             int tn;
    73             scanf("%d", &tn);
    74             for(int j=0; j<tn; j++){
    75                 scanf("%d", &b);
    76                 b--;
    77                 Map[i].PB(cr+b);
    78                 Map[cr+b].PB(i);     
    79             }
    80         }
    81         n = cr + stu;
    82         int ans = hungary();
    83         if(ans == cr) printf("YES
    ");
    84         else printf("NO
    ");
    85     }
    86     return 0;
    87 }
    View Code
    奔跑吧!少年!趁着你还年轻
  • 相关阅读:
    js字符串数组['1','2','3']转number
    antd-vue中给table表格整行加点击事件
    vue中路由在新的标签页打开
    antd中的form表单 initialValue导致数据不更新问题
    vue中computed的作用以及用法
    gitlab新增ssh
    CentOS7安装配置ActiveMQ
    利用已有的缓存地图文件发布ArcGIS Server瓦片服务
    CentOS7上使用源码安装物联网大数据平台TDengine
    一些可以使用的在线地图服务
  • 原文地址:https://www.cnblogs.com/shu-xiaohao/p/3558239.html
Copyright © 2011-2022 走看看