Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1296702
  • 博文数量: 196
  • 博客积分: 4141
  • 博客等级: 中将
  • 技术积分: 2253
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-21 20:04
文章存档

2019年(31)

2016年(1)

2014年(16)

2011年(8)

2010年(25)

2009年(115)

分类:

2009-06-30 17:58:37

Boys and Girls

Time Limit:5sMemory limit:32M
Accepted Submit:73Total Submit:276

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.

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, for n subjects.
For each given data set, the program should write to standard output a line containing the result.


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


Output

5
2

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int num;
int one[15000], two[15000];
int mat[500], tmat[500];

int find(int v)
{
    int i;

    for (i = one[v]; i < one[v + 1]; ++i)
    {
        if (tmat[two[i]] == -1)
        {
            tmat[two[i]] = 1;
            if (mat[two[i]] == -1 || find(mat[two[i]]))
            {
                mat[two[i]] = v;
                mat[v] = two[i];
                return 1;
            }
        }
    }

    return 0;
}

int match()
{
    int i, j, n;

    for (i = 0; i < num; ++i)
        mat[i] = -1;
    n = 0;

    for (i = 0; i < num; ++i)
    {
        if (mat[i] == -1)
        {
            for (j = 0; j < num; ++j)
                tmat[j] = -1;
            n += find(i);
        }
    }

    return n;
}

int main()
{
    int i, j;
    int v, n, t, l;

    while (scanf("%d", &num) != EOF)
    {
        l = 0;

        for (i = 0; i < num; ++i)
        {
            scanf("%d: (%d)", &v, &n);
            one[v] = l;
            for (j = 0; j < n; ++j)
            {
                scanf("%d", &t);
                two[l++] = t;
            }
        }
        one[i] = l;

        printf("%d\n", num - match());
    }

    return 0;
}

阅读(2176) | 评论(0) | 转发(0) |
0

上一篇:Island of Logic

下一篇:Lguest总结

给主人留下些什么吧!~~