HDU 5727 - Necklace

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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章