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 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

Source

Southeastern Europe 2000

题目大意:有n个学生,每个学生都和一些人又关系,现在要你找出最大的人数,使得这些人之间没关系

解题思路:裸的最大独立集,最大独立集=点数-最大匹配数
这里注意因为是两两匹配,所以求出的匹配值要除上个2

优化了几下,时间不断减少

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 506
int n;
int match[N];
int vis[N];
int mp[N][N];
bool dfs(int x){
    for(int i=0;i<n;i++){
        if(!vis[i] && mp[x][i]){
            vis[i]=1;
            if(match[i]==-1 || dfs(match[i])){
                match[i]=x;
                return true;
            }
        }
    }
    return false;
}
void solve(){
    int ans=0;
    memset(match,-1,sizeof(match));

    for(int i=0;i<n;i++){
        memset(vis,0,sizeof(vis));
        if(dfs(i)){
            ans++;
        }
    }
    printf("%d\n",n-ans/2);
}
int main()
{

    while(scanf("%d",&n)==1){
        memset(mp,0,sizeof(mp));
        char s[6];
        for(int i=0;i<n;i++){
            scanf("%s",s);
            scanf("%s",s);
            int num=0;
            for(int j=0;j<strlen(s);j++){
                if(s[j]>=‘0‘ && s[j]<=‘9‘){
                    num=num*10+s[j]-‘0‘;
                }
            }
            //printf("---%d\n",num);
            int x;
            for(int j=0;j<num;j++){
                scanf("%d",&x);
                mp[i][x]=mp[x][i]=1;
            }
        }
        solve();
    }
    return 0;
}
时间: 2024-11-10 16:10:56

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 Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 11694   Accepted: 5230 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 求最大独立点集

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

hduoj-----(1068)Girls and Boys(二分匹配)

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

hdu1068Girls and Boys(二分匹配,最大独立集)

Problem Description 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

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 黑白染色 + 二分匹配 (最大独立集) 好题

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

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 (匈牙利算法 最大独立集)

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