POJ Girls and Boys (最大独立点集)

Girls and Boys

Time Limit: 5000MS   Memory Limit: 10000K
Total Submissions: 12192   Accepted: 5454

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【分析】给出一些有关系的男女们,问最多有多少人之间没有关系。那就是求最大独立点集了,最大独立点集==顶点总数-匹配数。 由于此题我是两两匹配了两次,所以除以二。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <queue>
#include <vector>
#define inf 0x7fffffff
#define met(a,b) memset(a,b,sizeof a)
typedef long long ll;
using namespace std;
const int N = 1005;
const int M = 25005;
int read() {
    int x=0,f=1;
    char c=getchar();
    while(c<‘0‘||c>‘9‘) {
        if(c==‘-‘)f=-1;
        c=getchar();
    }
    while(c>=‘0‘&&c<=‘9‘) {
        x=x*10+c-‘0‘;
        c=getchar();
    }
    return x*f;
}
int n,k,cnt;
int mp[N][N],vis[N],link[N];
int head[N],x[N],y[N];
struct man
{
    int to,next;
}edg[M];
void init()
{
    met(head,-1);met(x,0);met(edg,0);met(y,0);cnt=0;
}
void add(int u,int v)
{
    edg[cnt].to=v;edg[cnt].next=head[u];head[u]=cnt++;
}
bool dfs(int u) {
    for(int i=head[u];i!=-1;i=edg[i].next) {
        int v=edg[i].to;
        if(!vis[v]) {
            vis[v]=1;
            if(!y[v]||dfs(y[v])) {
                x[u]=v;
                y[v]=u;
                return true;
            }
        }
    }
    return false;
}
void MaxMatch() {
    int ans=0;
    for(int i=0; i<n; i++) {
        if(!x[i]) {
            met(vis,0);
            if(dfs(i))ans++;
        }
    }
    //printf("ans=%d\n",ans);
    printf("%d\n",n-ans/2);
}
int main()
{
    while(~scanf("%d\n",&n)){
        init();
        int u,nn,v;
        for(int i=0;i<n;i++){
            scanf("%d: (%d)",&u,&nn);
            for(int j=0;j<nn;j++){
                scanf("%d",&v);
                add(i,v);
            }
        }
        MaxMatch();
    }
    return 0;
}
时间: 2024-10-12 01:00:55

POJ Girls and Boys (最大独立点集)的相关文章

hdu 1068 Girls and Boys 最大独立点集 二分匹配

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1068 思路: 求一集合满足,两两之间没有恋爱关系 思路: 最大独立点集=顶点数-最大匹配数 这里给出的关系,看似有向边,实则是无向边,那么按照二分匹配处理的话,相当于一个人看作两个人来用,最后还是顶点数目-最大二分匹配数 因为之间一个人当作两个人用,二分匹配数目减半,即 = n - match()/2 或者也可以写成 = ( 2n -  match() )/2 更容易理解 题目中n的大小没有说明,最

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(二分图匹配+拆点+最大独立集)

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

最小路径覆盖,最小点覆盖,最大独立点集

原文地址:http://blog.csdn.net/l04205613/article/details/6278394 node  1:最小路径覆盖 在一个PXP的有向图中,路径覆盖就是在图中找一些路经,使之覆盖了图中的所有顶点,且任何一个顶点有且只有一条路径与之关联:(如果把这些路径中的每条路径从它的起始点走到它的终点,那么恰好可以经过图中的每个顶点一次且仅一次):如果不考虑图中存在回路,那么每条路径就是一个弱连通子集.由上面可以得出:1.一个单独的顶点是一条路径:2.如果存在一路径p1,p2