HDU 1498 50 years, 50 colors(最小顶点覆盖)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1498

思路:和HDU2119有点像,枚举每种颜色的气球就行

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
const int inf = 0x3f3f3f3f;//1061109567
typedef long long ll;
const int maxn = 40000;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
#include<iostream>
int map1[110][110];
int map2[110][110];
int parter[110];
int visit[110];
int n;
int find1(int x)
{
    for (int i = 1; i <= n; i++)
    {
        if (map2[x][i] && visit[i] == 0)//如果有边 且 未被搜索过
        {
            visit[i] = 1;
            if (parter[i] == 0 || find1(parter[i]))//如果该男孩没有搭档或者能够给该男孩另找到一搭档
            {
                parter[i] = x;
                return 1;
            }
        }
    }
    return 0;
}
int solve(int x)
{
    memset(map2, 0, sizeof(map2));
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            if (map1[i][j] == x)
                map2[i][j] = 1;
        }
    }
    int sum = 0;
    memset(parter, 0, sizeof(parter));
    for (int i = 1; i <= n; i++)
    {
        memset(visit, 0, sizeof(visit));
        if (find1(i))
            sum++;
    }
    return sum;
}
int main()
{
    int k;
    int b[110];
    while (scanf("%d%d", &n, &k) ,n+k)
    {
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                scanf("%d", &map1[i][j]);
            }
        }
        int m = 0;
        for (int i = 1; i <= 50; i++)
        {
            if (solve(i) > k)
                b[m++] = i;
        }
        if (m == 0)
        {
            printf("-1\n");
        }
        else
        {
            for (int i = 0; i < m; i++)
            {
                if (i == 0)
                {
                    printf("%d", b[i]);
                }
                else
                    printf(" %d", b[i]);
            }
            printf("\n");
        }
    }
    return 0;
}


你可能感兴趣的:(HDU 1498 50 years, 50 colors(最小顶点覆盖))