POJ1466-Girls and Boys(最大独立集)

题目链接

题意:n个人,每个人有一个爱慕的集合,现在要挑出一些人,使得集合中没有人两两爱慕,问这个集合最大人数是多少

思路:每个人拆成两点,爱慕和被爱慕,建图,求出二分图最大匹配,因为是两两匹配,然后人数n - 最大匹配数 / 2就是最大独立集

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;

const int MAXN = 510;
int linker[MAXN];
bool used[MAXN];

vector<int> g[MAXN];
int n;

bool dfs(int u) {
    for (int i = 0; i < g[u].size(); i++) {
        int v = g[u][i];
        if (!used[v]) {
            used[v] = true;
            if (linker[v] == -1 || dfs(linker[v])) {
                linker[v] = u;
                return true;
            }
        }
    }
    return false;
}

int hungary() {
    int res = 0;
    memset(linker, -1, sizeof(linker));
    for (int u = 0; u < n; u++) {
        memset(used, false, sizeof(used));
        if (dfs(u))
            res++;
    }
    return res;
}

int main() {
    while (scanf("%d", &n) != EOF) {
        for (int i = 0; i < n; i++) g[i].clear();
        int u, cnt, v;
        for (int i = 0; i < n; i++) {
            scanf("%d: (%d)", &u, &cnt);
            while (cnt--) {
                scanf("%d", &v);
                g[u].push_back(v);
            }
        }
        printf("%d\n", n - hungary() / 2);
    }
    return 0;
}

时间: 2024-11-11 21:39:53

POJ1466-Girls and Boys(最大独立集)的相关文章

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,

hdu1068 Girls and Boys --- 最大独立集

有一个集合男和一个集合女,给出两集合间一些一一对应关系,问该两集合中的最大独立集的点数. 最大独立集=顶点总数-最大匹配数 此题中,若(a,b)有关,则(b,a)有关,每一个关系算了两次,相当于二分图的两边集合没有分男女,两边都是总人数, 所以此题中答案应该是 顶点总数-最大匹配数/2 #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algor

POJ1466 Girls and Boys【二分图最大独立集】

题目链接: http://poj.org/problem?id=1466 题目大意: 有N个学生,他们之间的某些人比较暧昧,只有认识的人能组成一个集合.问:最多能组成 多少个集合,使得这几个集合之间的学生都没有任何关系. 思路: 从N个图中选出M个点,使得这M个点两两之间没有边,求最大的M是多少.二分图最大独立 集问题.本来应该以男生.女生各一边建二分图求最大独立集,但是这里只有N个点,没有告 诉男生.女生的编号.那么以N个学生为一边.再以N个学生为另一边.将相互联系的人之间 建边.然后求最大匹

POJ 1466 Girls and Boys(最大独立集)

Description: In the second year of the university somebody started a study on the romantic relations between the students. The relation "romantically involved" is defined between one girl and one boy. For the study reasons it is necessary to fin

poj1466——Girls and Boys(最大独立点集)

Description In the second year of the university somebody started a study on the romantic relations between the students. The relation "romantically involved" is defined between one girl and one boy. For the study reasons it is necessary to find

Poj_1466 Girls and Boys -最大独立集

题目:找出相互不恋爱的人. 吐槽:500个点用邻接矩阵4000多ms,用了邻接表1000ms不到,所以大约500个点的时候就要考虑邻接表了. /************************************************ Author :DarkTong Created Time :2016/7/31 20:12:30 File Name :Poj_1466.cpp *************************************************/ //#i

POJ 1466 Girls and Boys (匈牙利算法 最大独立集)

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

HDU1068 Girls and Boys 【最大独立集】

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

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 step 6.3.2)Girls and Boys(求最大独立集)

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