HDU 5727 - Necklace

来源:互联网 发布:手机淘宝如何管理店铺 编辑:程序博客网 时间:2024/04/30 22:13
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