POJ - 1466 Girls and Boys 二分图+最大独立集

题目大意:有n个学生,某些学生之间存在着一种特殊的关系。。。现在要找出m个学生,要求这m个学生之间的任意两人不存在这种特殊的关系

解题思路:二分图问题,因为没办法划分成相应的两个集合且特殊关系是对称的,所以可以将两个点集都设置为n个点,求出最大匹配后再除以2即可得到(因为关系是对称的,所以所求得的最大匹配是双倍的)

得到最大匹配了,可以由定理得到 最大独立集 = n - 最大匹配数

#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
const int N = 510;
vector<int> s[N];
int vis[N];
int link[N];
int n;
bool dfs(int u) {
    for(int i = 0; i < s[u].size(); i++) {
        if(!vis[s[u][i]]) {
            vis[s[u][i]] = 1;
            if(link[s[u][i]] == -1 || dfs(link[s[u][i]])) {
                link[s[u][i]] = u;
                return true;
            }
        }
    }
    return false;
}

int main() {
    while(scanf("%d", &n) != EOF) {
        for(int i = 0; i < n; i++)
            s[i].clear();
        int x, y, z;
        for(int i = 0; i < n; i++) {
            scanf("%d: (%d)", &x, &y);
            for(int j = 0; j < y; j++) {
                scanf("%d", &z);
                s[x].push_back(z);
            }
        }
        int ans = 0;
        memset(link, -1, sizeof(link));
        for(int i = 0; i < n; i++) {
            memset(vis,0,sizeof(vis));
            if(dfs(i))
                ans++;
        }
        printf("%d\n", n - ans / 2);
    }
    return 0;
}
时间: 2024-08-01 19:11:09

POJ - 1466 Girls and Boys 二分图+最大独立集的相关文章

POJ 1466 Girls and Boys(二分图匹配+拆点+最大独立集)

POJ 1466 Girls and Boys 题目链接 题意:n个人,每个人有一个爱慕的集合,现在要挑出一些人,使得集合中没有人两两爱慕,问这个集合最大人数是多少 思路:每个人拆成两点,爱慕和被爱慕,然后建图,跑二分图最大匹配,由于爱慕关系是相互的,所以匹配数会多2倍,然后人数n - 最大匹配数 / 2就是最大独立集 代码: #include <cstdio> #include <cstring> #include <vector> #include <algo

poj 1466 Girls and Boys(二分图的最大独立集)

http://poj.org/problem?id=1466 Girls and Boys Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 11085   Accepted: 4956 Description In the second year of the university somebody started a study on the romantic relations between the students

poj 1466 Girls and Boys 二分图的最大匹配

Girls and Boys Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=1466 Description In the second year of the university somebody started a study on the romantic relations between the students. The relation "romantically involved&q

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

链接:poj 1466 题意:有n个学生,每个学生都和一些人有关系,现在要你找出最大的人数,使得这些人之间没关系 思路:求最大独立集,最大独立集=点数-最大匹配数 分析:建图时应该是一边是男生的点,一边是女生的点连边,但是题目中没说性别的问题,只能将每个点拆成两个点,一个当作是男的点,一个当作是女的点了,然后连边.由于关系是相互的,这样就造成了边的重复.也就是边集是刚才的二倍,从而导致了最大匹配变成了原本的二倍,因此,此时最大独立集=点数-最大匹配数/2. #include<stdio.h>

POJ 1466 Girls and Boys 求最大独立点集

最大独立点集 = 点数 - 最大匹配数 注意这题因为是两两匹配,A匹配B B匹配A算两个,所以最大匹配数要除以2 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #in

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

POJ 1466 Girls and Boys

Girls and Boys Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=1466 Description In the second year of the university somebody started a study on the romantic relations between the students. The relation "romantically involved&q

POJ 1466 Girls and Boys 黑白染色 + 二分匹配 (最大独立集) 好题

有n个人, 其中有男生和女生,接着有n行,分别给出了每一个人暗恋的对象(不止暗恋一个) 现在要从这n个人中找出一个最大集合,满足这个集合中的任意2个人,都没有暗恋这种关系. 输出集合的元素个数. 刚开始想,把人看成顶点,若有暗恋的关系,就连一条边,构成一个图 独立集的概念:一个图中两两互不相连的顶点集合 所以这道题,就是要求最大独立集 有:最大独立集+最小顶点覆盖=|V|(顶点的总个数) 那就求最小顶点覆盖了 根据题意: 暗恋的对象性别不同,所以a暗恋b,b暗恋c,c暗恋a这种关系不可能存在 也

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