1 条题解
-
0
C :
#include <stdio.h> #include <string.h> #define MAXN 1000 #define _clr(x) memset(x,0xff,sizeof(int)*MAXN) int f[10][10] = { { 2, 1, 7 }, { 0 }, { 0 }, { 2, 1, 7 }, { 1, 1 }, { 0 }, { 1, 5 }, { 1, 1 }, { 9, 0, 1, 2, 3, 4, 5, 6, 7, 9 }, { 5, 1, 3, 4, 5, 7 } }; int g[MAXN][MAXN], match[MAXN],mark[MAXN],n; //邻接矩阵、 int hungary(int x) { int i; for(i=0;i<n;i++) { if(g[x][i]&&!mark[i]) { mark[i]=1;//标记这一点走过了 if(match[i]==-1||hungary(match[i])) { match[i]=x;//匹配的真正含义 return 1; } } } return 0; } int main() { int i, j, k, a[MAXN]; while (scanf("%d", &n) != EOF) { for (i = 0; i < n; i++) scanf("%d", &a[i]); memset(g, 0, sizeof(g)); for (i = 0; i < n; i++) { if (a[i] == 1 || a[i] == 2 || a[i] == 5) continue;//剪了个枝 for (j = i + 1; j < n; j++) { if (a[i] == a[j]) continue; for (k = 1; k <= f[a[i]][0]; k++) if (a[j] == f[a[i]][k]) { g[i][j] = 1; break; } } }//整个这个循环就是初始化邻接矩阵、f数组也只在这里用一次 int sum=0; memset(match,-1,sizeof(match)); for(i=0;i<n;i++) { memset(mark,0,sizeof(mark)); sum+=hungary(i); } printf("%d\n", n - sum); } return 0; }
C++ :
#include <stdio.h> #include <string.h> #define MAXN 1000 #define _clr(x) memset(x,0xff,sizeof(int)*MAXN) int f[10][10] = { { 2, 1, 7 }, { 0 }, { 0 }, { 2, 1, 7 }, { 1, 1 }, { 0 }, { 1, 5 }, { 1, 1 }, { 9, 0, 1, 2, 3, 4, 5, 6, 7, 9 }, { 5, 1, 3, 4, 5, 7 } }; int g[MAXN][MAXN], match1[MAXN], match2[MAXN]; int hungary(int m, int n, int mat[][MAXN], int *match1, int *match2) { int s[MAXN], t[MAXN], p, q, ret = 0, i, j, k; for (_clr(match1),_clr(match2), i = 0; i < m; ret += (match1[i++] >= 0)) for (_clr(t),s[p = q = 0] = i; p <= q && match1[i] < 0; p++) for (k = s[p], j = 0; j < n && match1[i] < 0; j++) if (mat[k][j] && t[j] < 0) { s[++q] = match2[j]; t[j] = k; if (s[q] < 0) for (p = j; p >= 0; j = p) { match2[j] = k = t[j]; p = match1[k]; match1[k] = j; } } return ret; } int main() { int n, i, j, k, a[MAXN]; while (scanf("%d", &n) != EOF) { for (i = 0; i < n; i++) scanf("%d", &a[i]); memset(g, 0, sizeof(g)); for (i = 0; i < n; i++) { if (a[i] == 1 || a[i] == 2 || a[i] == 5) continue; for (j = i + 1; j < n; j++) { if (a[i] == a[j]) continue; for (k = 1; k <= f[a[i]][0]; k++) if (a[j] == f[a[i]][k]) { g[i][j] = 1; break; } } } printf("%d\n", n - hungary(n, n, g, match1, match2)); } return 0; }
- 1
信息
- ID
- 2366
- 时间
- 1000ms
- 内存
- 32MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者