-
Notifications
You must be signed in to change notification settings - Fork 34
/
1149-Factors-and-Multiples.cpp
124 lines (79 loc) · 1.3 KB
/
1149-Factors-and-Multiples.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
#include <stdio.h>
#include <stack>
#include <string.h>
using namespace std;
int inp1[110];
int inp2[110];
int n;
int m;
int a[250][250];
int sor;
int tar;
int flow;
int parent[250];
int dfs()
{
int x;
int y;
int vis[250];
memset(vis, 0 , sizeof vis);
stack <int> q;
q.push(sor);
while(!q.empty()) {
x = q.top();
q.pop();
for (int i = 0; i <= n + m + 2; i++) {
if(a[x][i] > 0 and vis[i] == 0) {
parent[i] = x;
q.push(i);
vis[i] = 1;
}
}
}
return vis[tar] == 1;
}
int main()
{
int t;
int ans;
int k;
scanf("%d", &t);
for (int cs = 1; cs <= t; cs++) {
ans = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", inp1 + i);
}
scanf("%d", &m);
for (int i = 0; i < m; i++) {
scanf("%d", inp2 + i);
}
sor = n + m + 1;
tar = n + m + 2;
memset(a, 0, sizeof a);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if(inp2[j] % inp1[i] == 0) {
a[i][j + n] = 1;
}
}
}
for (int i = 0; i < n; i++) {
a[sor][i] = 1;
}
for(int i = 0; i < m; i++) {
a[i + n][tar] = 1;
}
while(dfs()) {
k = tar;
while(k != sor) {
a[parent[k]][k] -= 1;
a[k][parent[k]] += 1;
k = parent[k];
}
ans += 1;
}
printf("Case %d: %d\n", cs, ans);
}
}