HDU 1068 Girls and Boys(最大独立集合 = 顶点数 - 最大匹配数)

HDU 1068 :题目链接

题意:一些男孩和女孩,给出一些人物关系,然后问能找到最多有多少个人都互不认识。

转换一下:就是大家都不认识的人,即最大独立集合

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <math.h>
#define init(a) memset(a,0,sizeof(a))
#define PI acos(-1,0)
using namespace std;
const int maxn = 510;
const int maxm = 100001;
#define lson left, m, id<<1
#define rson m+1, right, id<<1|1
#define min(a,b) (a>b)?b:a
#define max(a,b) (a>b)?a:b
const int N = 50010;
int ma[maxn][maxn];
int line[maxn];
bool vis[maxn];
int k,n,m;

int DFS(int u)
{
   // printf("u = %d\n",u);
    for(int v = 0;v<k;v++)
    {
        if(ma[u][v]== 1 && vis[v]==0)
        {
            vis[v] = 1;
            if(line[v]== -1 || DFS(line[v]))
            {
                line[v] = u;
                return 1;
            }
        }
    }
    return 0;
}

int K_M()
{
    int ans = 0;
    memset(line,-1,sizeof(line));
    for(int i = 0;i<k;i++)
    {
        init(vis);
       ans += DFS(i);
    }
    return ans;
}
int main()
{
    int x,y;
    while(~scanf("%d",&k))
    {
        init(ma);
        for(int j = 1;j<=k;j++)
        {
        scanf("%d: (%d)",&m,&n);
        for(int i = 1;i<=n;i++)
        {
            scanf("%d",&x);
            ma[m][x] = 1;
        }
        }
//最大独立集合 = 顶点数 - 最大匹配数
        int ans = K_M();
        ans /= 2;
        printf("%d\n",k-ans);
    }
    return 0;
}

HDU 1068 Girls and Boys(最大独立集合 = 顶点数 - 最大匹配数),布布扣,bubuko.com

时间: 2024-08-09 11:21:17

HDU 1068 Girls and Boys(最大独立集合 = 顶点数 - 最大匹配数)的相关文章

hdu 1068 Girls and Boys(匈牙利算法求最大独立集)

Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7044    Accepted Submission(s): 3178 Problem Description the second year of the university somebody started a study on the roman

HDU 1068 Girls and Boys (二分图最大独立集)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1068 有n个同学,格式ni:(m) n1 n2 n3表示同学ni有缘与n1,n2,n3成为情侣,求集合中不存在有缘成为情侣的同学的最大同学数. 独立集(图的顶点集的子集,其中任意两点不相邻) 二分图中 最大独立集 = 顶点个数 - 最大匹配数 因为男女不知道,将一个人拆成两个性别,求最大匹配后,除以2就行了. 这种做法比较难理解. 1 #include <iostream> 2 #include

[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&g

hdu 1068 Girls and Boys

英语不太好,但是题意大概是给定一个序列,每个序列给出有可能成为情侣的人,现在要你求出一个所有人都不可能成为情侣的最大集合. 解题思路就是最大独立集 , 最大独立集 = 顶点数 - 最大匹配数 ; 由于这题是拆分自己,或者说把一个点分成2个部分,所以最后的最大匹配数要除以2; 一定要记得初始化,忘了Map数组的初始化= =,超时好多次; 给出一个讲解的网址 : http://dsqiu.iteye.com/blog/1689505 1 #include <iostream> 2 #include

hdu 1068 Girls and Boys 最大独立点集 二分匹配

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1068 思路: 求一集合满足,两两之间没有恋爱关系 思路: 最大独立点集=顶点数-最大匹配数 这里给出的关系,看似有向边,实则是无向边,那么按照二分匹配处理的话,相当于一个人看作两个人来用,最后还是顶点数目-最大二分匹配数 因为之间一个人当作两个人用,二分匹配数目减半,即 = n - match()/2 或者也可以写成 = ( 2n -  match() )/2 更容易理解 题目中n的大小没有说明,最

poj 1466 HDU 1068 Girls and Boys (最大独立集)

Girls and Boys Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 11141   Accepted: 4983 Description In the second year of the university somebody started a study on the romantic relations between the students. The relation "romantically in

HDU——1068 Girls and Boys

Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12246    Accepted Submission(s): 5768 Problem Description the second year of the university somebody started a study on the roma

hdu - 1068 Girls and Boys (二分图最大独立集+拆点)

http://acm.hdu.edu.cn/showproblem.php?pid=1068 因为没有指定性别,所以要拆点,把i拆分i和i’ 那么U=V-M (M是最大匹配,U最大独立集,V是顶点数) 2U=2V-2M  所以 U=n-M'/2. (没怎么看明白)  但是不这样会wa. 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <vector> 5 #inc

hdu 1068 Girls and Boys 二分图的最大匹配

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1068 #include <iostream> #include <stdio.h> #include <string.h> using namespace std; int n; int used[505]; int link[505][505]; int boy[505]; int find(int x){ int i; for(i=0;i<n;i++){ if(!