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

一群男女搞暧昧,求一个最大的集合,元素满足任意两个元素都不暧昧。

额,贴完去吃饭饭~~:

/*=============================================================================
#
#      Author: liangshu - cbam
#
#      QQ : 756029571
#
#      School : 哈尔滨理工大学
#
#      Last modified: 2015-08-27 17:19
#
#     Filename: C.cpp
#
#     Description:
#        The people who are crazy enough to think they can change the world, are the ones who do !
=============================================================================*/
#

#include<iostream>
#include<sstream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<cctype>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
using namespace std;
const int INF=800;
int n;
int from[INF],tot;
bool use[INF];
vector<int>G[INF];
bool match(int x)
{
     for(int i=0;i<G[x].size();i++)
    {
         if(!use[G[x][i]])
        {
             use[G[x][i]]=1;
            if(from[G[x][i]]==-1||match(from[G[x][i]]))
            {
                 from[G[x][i]]=x;
                return 1;
            }
        }
    }
    return 0;
}

int hungary( )
{
     tot=0;
    memset(from,255,sizeof(from));
    for(int i=0;i<n;i++)
    {
         memset(use,0,sizeof(use));
        if(match(i))
           {
               ++tot;
           }
    }
    return tot;
}

int main(){
    int x, y;
    while(cin>>n){
    for(int i = 0; i < n; i++){
        scanf("%d: (%d)",&x, &y);
        for(int j = 0; j < y; j++){
            int a;
            scanf("%d",&a);
            G[x].push_back(a);
        }
    }
        cout<<n - hungary() / 2<<endl;
        for(int i = 0; i < INF; i++){
            G[i].clear();

        }
    }
    return 0;
}

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

时间: 2024-08-07 17:53:29

poj 1466 HDU 1068 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,

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

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 有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 二分图的最大匹配

题目链接: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(!

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的大小没有说明,最