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 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,并给出每个学生与哪些学生存在关系(存在关系的学生一定是异性)。现在让你求一个学生集合,这个集合中任意两个学生之间不存在这种关系。输出这样的关系集合中最大的一个的学生人数。

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;

vector<int> G[505];
int match[505],used[505];
int n;
void add_edge(int u,int v)
{
    G[u].push_back(v);
    G[v].push_back(u);
}

bool dfs(int u)
{
     used[u]=1;
     for(int i=0;i<G[u].size();i++)
     {
       int v=G[u][i];
       int w=match[v];
       if(w<0||!used[w]&&dfs(w))
         {
             match[u]=v;
             match[v]=u;//匹配的边两端点同时标记
             return true;
         }
     }
     return false;
}

int  bipartite_match()
{
    memset(match,-1,sizeof(match));
    int res=0;
    for(int i=0;i<n;i++)
      if(match[i]<0)//先前标记过就不用再标记了
      {
          memset(used,0,sizeof(used));
          if(dfs(i)) res++;
      }
    return res;
}

int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++) G[i].clear();
        for(int u=0;u<n;u++)
        {
           int k,num,v;
           scanf("%d: (%d)",&k,&num);
           for(int i=0;i<num;i++)
           {
               scanf("%d",&v);
               add_edge(u,v);
           }
        }
        printf("%d\n",n-bipartite_match());
    }
    return 0;
}

  分析:最大独立集问题,看得这篇介绍http://blog.sina.com.cn/s/blog_6635898a0100lyui.html

他们写的模板最后二分匹配出来后都要除以2,因为每条边都重复算了一次,但是因为我的模板

的问题,不需要除以2,因为同时把匹配的边两顶点都标记了

最大独立集=顶点数-匹配的顶点数/2(我的模板中即bipartite_match()返回的边数)

时间: 2025-01-02 05:35:12

POJ 1466 大学谈恋爱 二分匹配变形 最大独立集的相关文章

POJ 3014:Asteroids(二分匹配,匈牙利算法)

Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14399   Accepted: 7836 Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K as

poj 1274 The Perfect Stall (二分匹配)

The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17768   Accepted: 8104 Description Farmer John completed his new barn just last week, complete with all the latest milking technology. Unfortunately, due to engineering pr

poj 2060 Taxi Cab Scheme (二分匹配)

Taxi Cab Scheme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5710   Accepted: 2393 Description Running a taxi station is not all that simple. Apart from the obvious demand for a centralised coordination of the cabs in order to pick up

POJ 1358 Housing Complexes(二分匹配)

题目链接:http://poj.org/problem?id=1358 题意:给出K个n*m的空地,字母A-Z表示障碍.对于每一块,你可以将某一种字母全部拿走,使得出现一个h*w的空地.但是每一种字母在一块中被拿走在另一块中就不允许拿这种字母了.求K块中最多有多少块可以出现多少h*w的空地? 思路:(1)首先计算每块中拿走哪些字母可 以使得出现h*w的空地.f[n][m][26],f[i][j][k]表示从[1,1]到[i,j]的矩形内k出现的次数,然后我们就能利用区间减法求 出任意一个h*w的

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

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

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

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

POJ 2724 奶酪消毒机 二分匹配 建图 比较难想

Purifying Machine Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5004   Accepted: 1444 Description Mike is the owner of a cheese factory. He has 2N cheeses and each cheese is given a binary number from 00...0 to 11...1. To keep his chee

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