zoukankan      html  css  js  c++  java
  • [HDU] 1068 Girls and Boys(二分图最大匹配)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1068

    本题求二分图最大独立点集。因为最大独立点集=顶点数-最大匹配数。所以转化为求最大匹配。因为没有给出男女,所以每个人都用了两遍,所以结果应该除以2。

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<string.h>
     4 #include<algorithm>
     5 #include<math.h>
     6 #include<stdbool.h>
     7 #include<time.h>
     8 #include<stdlib.h>
     9 #include<set>
    10 #include<map>
    11 #include<stack>
    12 #include<queue>
    13 #include<vector>
    14 using namespace std;
    15 #define clr(x,y)    memset(x,y,sizeof(x))
    16 #define sqr(x)      ((x)*(x))
    17 #define rep(i,a,b)  for(int i=(a);i<=(b);i++)
    18 #define LL          long long
    19 #define INF         0x3f3f3f3f
    20 #define A           first
    21 #define B           second
    22 #define PI          3.14159265358979323
    23 const int N=2000+131;
    24 int n,m,f[N],g[N][N],link[N];
    25 
    26 void init()
    27 {
    28     clr(f,0);
    29     clr(g,0);
    30     clr(link,-1);
    31 }
    32 
    33 bool find(int x)
    34 {
    35     for(int i=0;i<n;i++) {
    36         if(!f[i] && g[x][i]) {
    37             f[i]=1;
    38             if(link[i]==-1 || find(link[i])) {
    39                 link[i]=x;
    40                 return true;
    41             }
    42         }
    43     }
    44     
    45     return false;
    46 }
    47 
    48 int hungary()
    49 {
    50     int ans=0;
    51     for(int i=0;i<n;i++) {
    52         clr(f,0);
    53         if(find(i)) ans++;
    54     }
    55     return ans;
    56 }
    57 
    58 int main()
    59 {
    60     int u,v;
    61     
    62     while(~scanf("%d",&n)) {    
    63         init();
    64         for(int i=0;i<n;i++) {
    65             scanf("%d: (%d)",&u,&m);
    66             while(m--){
    67                 scanf("%d",&v);
    68                 g[u][v]=1;
    69             }
    70         }
    71         printf("%d
    ",n-hungary()/2);
    72     }
    73     
    74     return 0;
    75 }
  • 相关阅读:
    虚拟机安装配置
    大整数加法 面试题
    结构体
    操作文件
    Gcd HDU
    牛客练习赛47 A DongDong破密码 (异或性质,递推)
    ACM常用之 异或运算的性质。
    Wannafly挑战赛22 C 多项式(大数,多项式极限)
    大数全能模板
    Wannafly挑战赛22 D 整数序列 (线段树维护三角函数值)
  • 原文地址:https://www.cnblogs.com/sxiszero/p/4372869.html
Copyright © 2011-2022 走看看