POJ2771【二分图-最大独立集】

Description

Frank N. Stein is a very conservative high-school teacher. He wants to take some of his students on an excursion, but he is afraid that some of them might become couples. While you can never exclude this possibility, he has made some rules that he thinks indicates a low probability two persons will become a couple:

  • Their height differs by more than 40 cm.
  • They are of the same sex.
  • Their preferred music style is different.
  • Their favourite sport is the same (they are likely to be fans of different teams and that would result in fighting).

So, for any two persons that he brings on the excursion, they must satisfy at least one of the requirements above. Help him find the maximum number of persons he can take, given their vital information.

Input

The first line of the input consists of an integer T ≤ 100 giving the number of test cases. The first line of each test case consists of an integer N ≤ 500 giving the number of pupils. Next there will be one line for each pupil consisting of four space-separated data items:

an integer h giving the height in cm;
a character 'F' for female or 'M' for male;
a string describing the preferred music style;
a string with the name of the favourite sport. 

No string in the input will contain more than 100 characters, nor will any string contain any whitespace.

Output

For each test case in the input there should be one line with an integer giving the maximum number of eligible pupils.

Sample Input

2
4
35 M classicism programming
0 M baroque skiing
43 M baroque chess
30 F baroque soccer
8
27 M romance programming
194 F baroque programming
67 M baroque ping-pong
51 M classicism programming
80 M classicism Paintball
35 M baroque ping-pong
39 F romance ping-pong
110 M romance Paintball

Sample Output

3
7


最大独立集

设G=(v,e)是n阶图,如果G的顶点集合中U中任何两个顶点都不邻接,则称它为独立集。
最大独立集:在一个独立集中顶点的最大个数称为图G的独立数
设最大匹配边集是M,那么最大独立集个数|U|=|总点数|-|最大匹配数|

老师要带学生们出去远行,但是这个保守的老师不希望男女生们在旅行途中恋爱,所以他要使得谈恋爱的可能性尽量小,并且带出去最多的学生。

根据条件,将恋爱的人连边。
求出最大独立集。

#include
#include
#include
#include
#include
#include
using namespace std;
const int MAX = 555;
int mapp[MAX][MAX];
int vis[MAX];
int match[MAX];
int n, i, j;
struct node
{
    int height;
    int id;
    string sex;
    string music, sport;
}t;
bool dfs(int u)
{
    for (int v = 1; v <= n; v++)
        if (mapp[u][v] && !vis[v])
        {
            vis[v] = 1;
            if (match[v] == -1 || dfs(match[v]))
            {
                match[v] = u;
                return true;
            }
        }
    return false;
}
vectorf, m;
int main()
{
    int T;
    scanf("%d", &T);
    while (T--)
    {
        f.clear();
        m.clear();
        memset(mapp, 0, sizeof(mapp));
        scanf("%d", &n);
        for (i = 1; i <= n; i++)
        {
            scanf("%d", &t.height);
            cin >>t.sex>> t.music >> t.sport;
            t.id = i;
            if (t.sex[0] == 'F')
            {
                f.push_back(t);
            }
            else
            {
                m.push_back(t);
            }
        }
        for (i = 0; i < f.size(); i++)
        {
            for (j = 0; j < m.size(); j++)
            {
                if (abs(m[j].height - f[i].height )> 40)
                    continue;
                if ((f[i].music != m[j].music || f[i].sport == m[j].sport))
                    continue;
                mapp[f[i].id][m[j].id] = 1;//把能搞在一起的建边
            }
        }
        int sum = 0;
        memset(match, -1, sizeof(match));
        for (int i = 1; i <= n; i++)
        {
            memset(vis, 0, sizeof(vis));
            if (dfs(i))
                sum++;
        }
        printf("%d\n", n - sum);// 最大独立集个数 | U |= | 总点数 | -| 最大匹配数 |
    }
}

你可能感兴趣的:(图论,解题报告)