2016多校1 1005 二分图最大匹配
来源:互联网 发布:国产汽车发动机知乎 编辑:程序博客网 时间:2024/06/16 15:52
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
8!暴力枚举阴的部分建图后,转化为二分图最大匹配问题。匈牙利算法跑一遍即可。
之所以是8!而不是9!是因为环的对称性,可以直接set第一个是1。
代码:
#include <bits/stdc++.h>using namespace std;int a[15];int n, m;int Max;bool vis[15];bool vis2[15];int linker[15];bool mp[15][15];vector<int>g[15];struct match{ int x, y;};//map <match, int>mp;bool operator<(match const&a, match const&b){ if(a.x!=b.x)return a.x<b.x; else return a.y<b.y;}bool dfs2(int u){ for(int v=0;v<g[u].size();v++) { int tmp=g[u][v]; if(!vis2[tmp]) { vis2[tmp]=true; if(linker[tmp]==-1||dfs2(linker[tmp])) { linker[tmp]=u; return true; } } } return false;}int hungary(){ for(int i=1;i<=n;i++)g[i].clear(); int x1, x2, y1, y2; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { x1=i;x2=i; y1=a[j]; if(j==n)y2=a[1]; else y2=a[j+1]; if(!mp[x1][y1]&&!mp[x2][y2])g[i].push_back(j); } } //printf("pic is:\n"); /*for(int i=1;i<=n;i++) { for(int j=0;j<g[i].size();j++) printf("%d ", g[i][j]); printf("\n"); }*/ int res=0; memset(linker, -1, sizeof(linker)); for(int u=1;u<=n;u++) { memset(vis2, false, sizeof(vis2)); if(dfs2(u))res++; } return res;}void dfs(int flor){ if(flor==n+1) { int res=hungary(); Max=max(Max, res); //for(int i=1;i<=n;i++) // printf("%d%c", a[i],i==n?'\n':' '); return; } for(int i=2;i<=n;i++) { if(!vis[i]) { vis[i]=true; a[flor]=i; dfs(flor+1); vis[i]=false; } } return;}int main(){ while(~scanf("%d%d", &n, &m)) { //mp.clear(); memset(mp, 0, sizeof(mp)); int x, y; for(int i=1;i<=m;i++) { scanf("%d%d", &x, &y); //match tmp; //tmp.x=x;tmp.y=y; mp[x][y]=true; } Max=0; memset(vis, false, sizeof(vis)); a[1]=1; dfs(2); printf("%d\n",n-Max); } return 0;}
- 2016多校1 1005 二分图最大匹配
- 二分图最大匹配
- 二分图最大匹配 。
- 二分图最大匹配
- 二分图最大匹配
- 二分图最大匹配
- 二分图最大匹配
- 二分图 最大匹配
- 二分图最大匹配
- 二分图最大匹配
- 二分图最大匹配
- 二分图最大匹配
- 二分图最大匹配
- 二分图最大匹配
- 二分图最大匹配
- 二分图最大匹配
- 二分图最大匹配
- 二分图 最大匹配
- 未名湖边的烦恼-蓝桥杯算法训练题-递推/递归
- 动画1
- 常用的Linux组合命令备忘
- 初识 Oracle启动的7个服务
- js子窗口与父窗口的调用
- 2016多校1 1005 二分图最大匹配
- poj 2337 Catenyms 欧拉回路+dfs 解题报告
- C++中嵌入python程序——参数传递
- VPN篇(5.2)-07. IPsec VPN 子网重叠 ❀ 飞塔 (Fortinet) 防火墙
- 雨
- foxmail不能登录网易企业邮箱
- 文章标题
- IBM SPSS Modeler算法系列-----决策树CHAID算法
- 动画队列