POJ 1466 && ZOJ 1137--Girls and Boys【二分图 && 最大点独立集】

Girls and Boys

Time Limit: 5000MS   Memory Limit: 10000K
Total Submissions: 11134   Accepted: 4981

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 out the maximum set satisfying
the condition: there are no two students in the set who have been "romantically involved". The result of the program is the number of students in such a set.

Input

The input contains several data sets in text format. Each data set represents one set of subjects of the study, with the following description:

the number of students

the description of each student, in the following format

student_identifier:(number_of_romantic_relations) student_identifier1 student_identifier2 student_identifier3 ...

or

student_identifier:(0)

The student_identifier is an integer number between 0 and n-1 (n <=500 ), for n subjects.

Output

For each given data set, the program should write to standard output a line containing the result.

Sample Input

7
0: (3) 4 5 6
1: (2) 4 6
2: (0)
3: (0)
4: (2) 0 1
5: (1) 0
6: (2) 0 1
3
0: (2) 1 2
1: (1) 0
2: (1) 0

Sample Output

5
2

题意:

在大学的第二年有人开始研究男女学生之间的“浪漫”关系,所谓“浪漫”关系是在男女学生之间的,给你每个学生的编号以及他们之间的浪漫关系,请你找出一个最大的学生集合,使得这个集合里任意两个学生之间没有浪漫关系,求这个集合的学生人数。

解析:

赤裸裸的最大独立集的问题,先建二分图,还是最原始的拆点建图,每个点拆成左右两个点, 有关系的点之间建边,然后求最大匹配数,但这里要注意,由于关系是相互的,所以这里求出的最大匹配数是原来的二倍,要 / 2才对。

二分图的最大独立集 =二分图的点数 - 二分图的最大匹配数。

#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 550
using namespace std;
int map[maxn][maxn];
int used[maxn];
int link[maxn];
int n;

void init(){
    memset(map, 0, sizeof(map));
}

void getmap(){
    for(int j = 0; j < n; ++j){
        int id, m, a;
        scanf("%d: (%d)", &id, &m);
        for(int i = 0; i < m; ++i){
            scanf("%d", &a);
            map[id][a] = 1;
        }
    }
}

bool dfs(int x){
    for(int i = 0; i < n; ++i){
        if(map[x][i] && !used[i]){
            used[i] = 1;
            if(link[i] == -1 || dfs(link[i])){
                link[i] = x;
                return true;
            }
        }
    }
    return false;
}

int hungary(){
    int ans = 0;
    memset(link, -1, sizeof(link));
    for(int i = 0; i < n; ++i){
        memset(used, 0, sizeof(used));
        if(dfs(i))
            ans++;
    }
    return ans;
}

int main (){
    while(scanf("%d", &n) != EOF){
        init();
        getmap();
        int sum = hungary();
        //printf("%d\n", sum);
        printf("%d\n", n - sum / 2);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-23 17:23:21

POJ 1466 && ZOJ 1137--Girls and Boys【二分图 && 最大点独立集】的相关文章

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

POJ 1466 Girls and Boys【最大点独立集】

大意:最大点独立集 分析:最大点独立集 代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 using namespace std; 6 7 const int maxn = 505; 8 9 int n; 10 int Link[maxn]; 11 int vis[maxn]; 12 vector<int> G[maxn

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 3342 Party at Hali-Bula 判断二分图最大点独立集是否唯一

题意: 给一个二分图,判断最大点独立集是否唯一. 分析: 匈牙利算法. 代码: //poj 3342 //sep9 #include <iostream> #include <string> #include <map> using namespace std; const int maxN=212; int n; int M,v1,v2; bool g[maxN][maxN]; bool vis[maxN]; int link[maxN]; map<string

POJ 2771 Guardian of Decency (二分图最大点独立集)

Guardian of Decency Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6133   Accepted: 2555 Description Frank N. Stein is a very conservative high-school teacher. He wants to take some of his students on an excursion, but he is afraid that

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

题目大意:有n个学生,某些学生之间存在着一种特殊的关系...现在要找出m个学生,要求这m个学生之间的任意两人不存在这种特殊的关系 解题思路:二分图问题,因为没办法划分成相应的两个集合且特殊关系是对称的,所以可以将两个点集都设置为n个点,求出最大匹配后再除以2即可得到(因为关系是对称的,所以所求得的最大匹配是双倍的) 得到最大匹配了,可以由定理得到 最大独立集 = n - 最大匹配数 #include<cstdio> #include<vector> #include<cstr

[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