HDU 5727 - Necklace

54 篇文章 0 订阅
Problem Description
SJX has 2*N magic gems. N of them have Yin energy inside while others have Yang energy. SJX wants to make a necklace with these magic gems for his beloved BHB. To avoid making the necklace too Yin or too Yang, he must place these magic gems Yin after Yang and Yang after Yin, which means two adjacent gems must have different kind of energy. But he finds that some gems with Yang energy will become somber adjacent with some of the Yin gems and impact the value of the neckless. After trying multiple times, he finds out M rules of the gems. He wants to have a most valuable neckless which means the somber gems must be as less as possible. So he wonders how many gems with Yang energy will become somber if he make the necklace in the best way.


Input
Multiple test cases.

For each test case, the first line contains two integers N(0≤N≤9),M(0≤M≤N∗N), descripted as above.

Then M lines followed, every line contains two integers X,Y, indicates that magic gem X with Yang energy will become somber adjacent with the magic gem Y with Yin energy.


Output
One line per case, an integer indicates that how many gem will become somber at least.


Sample Input

2 1
1 1
3 4
1 1
1 2
1 3
2 1



Sample Output

1
1


题意:有一串项链分为阴阳两种珠子,规定里面有些珠子靠在一起时会让阳珠子褪色【误】。给出一个 n 和 m,n 表示有 n 个珠子,m 表示 m 对珠子,对于每个 m 输入两个数,表示这两个位置上的珠子靠在一起会褪色,求出将这些珠子全排列,褪色最少情况的褪色珠子数量。

将每个全排列枚举,每次答案都取到目前为止最小的那个值。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int n, m;
bool vis[20];
int maps[20][20], f[20][20];
int pos[20], mark[20];

bool DFS(int u)
{
    for (int i = 1; i <= 2*n; ++i)
    {
        if (f[u][i] != 0 && !vis[i])
        {
            vis[i] = true;
            if (mark[i] == -1 || DFS(mark[i]))
            {
                mark[i] = u;
                return true;
            }
        }
    }
    return false;
}

void solve()
{
    for (int i = 1; i <= n; ++i)
        pos[i] = i;
    int ans = n;
    do
    {
        memset(f, 0, sizeof(f));
        memset(mark, -1, sizeof(mark));
        int u, v;
        for (int i = 1; i <= n; ++i)
        {
            for (int j = 1; j <= n; ++j)
            {
                if (i == 1)
                {
                    u = pos[1];
                    v = pos[n];
                }
                else
                {
                    u = pos[i];
                    v = pos[i-1];
                }
                if (maps[u][j] != 0 || maps[v][j] != 0)
                    continue;
                f[i][j] = 1;
            }
        }
        int sum = 0;
        for (int i = 1; i <= n; ++i)
        {
            memset(vis, false, sizeof(vis));
            if (DFS(i))
                sum++;
        }
        ans = min(ans, n-sum);
    }while (next_permutation(pos+2, pos+n+1));
    printf("%d\n", ans);
}

int main()
{
    while (scanf("%d%d", &n, &m) != EOF)
    {
        if (n == 0)
        {
            printf("0\n");
            continue;
        }
        memset(maps, 0, sizeof(maps));

        int u, v;
        for (int i = 0; i < m; ++i)
        {
            scanf("%d%d", &u, &v);
            maps[v][u] = 1;
        }
        solve();
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值