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
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
- HDU 5727 Necklace
- 【HDU】5727 Necklace
- HDU 5727 Necklace
- HDU 5727 - Necklace
- Necklace HDU
- Necklace HDU
- HDU 5727 Necklace (二分图匹配hungary)
- HDU 5727 Necklace(暴力+匈牙利)
- HDU 5727 Necklace(二分匹配)
- HDU 5727 Necklace (二分图匹配)
- HDU 5727 Necklace(二分匹配-hungary)
- HDU 5727 Necklace(枚举+二分图)
- HDU 5727 Necklace(枚举+二分图匹配)
- HDU 5727 Necklace (二分图匹配)
- HDU 5727 Necklace(二分图匹配)
- hdu 5727 Necklace 全排列+二分图
- Hdu 5727 Necklace(二分图匹配)
- hdu 3474 Necklace
- 玩游戏赚钱真实收入5000+
- pixhawk-console调试之ardupilot
- 方阵中的最大乘积
- ZooKeeper学习笔记-6---ZkClient使用
- Ubuntu 16.04无线网卡RTL8723BE频繁掉线及信号不足解决办法
- HDU 5727 - Necklace
- Spark1.5堆内存分配
- 【我的Android进阶之旅】解决SVN Cleanup错误: Failed to run the WC DB work queue associated with
- Linux kernel printk的用法
- ibatis中动态查询返回字段返回用resultClass="java.util.HashMap" Java.sql.SQLException: 列名无效的问题
- ES6笔记:正则的扩展
- ROC曲线-阈值评价标准
- 获取信息,组装数据,后台接收流程
- oracle存储过程和自定义函数