Skip to content

Commit a0d7238

Browse files
committed
[Platinum IV] Title: Cards, Time: 440 ms, Memory: 3228 KB -BaekjoonHub
1 parent 8e42c55 commit a0d7238

2 files changed

Lines changed: 123 additions & 0 deletions

File tree

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include<iostream>
2+
#include<vector>
3+
#include<cstring>
4+
using namespace std;
5+
6+
const int N = 501;
7+
int arr[N];
8+
vector<int> edges[N];
9+
int n, m, t;
10+
int v[N];
11+
int match[N];
12+
13+
int get_gcd(int a, int b) {
14+
while (b) {
15+
int t = a % b;
16+
a = b;
17+
b = t;
18+
}
19+
return a;
20+
}
21+
22+
bool dfs(int cn) {
23+
for (int nn : edges[cn]) {
24+
if (!match[nn]) {
25+
match[nn] = cn;
26+
return true;
27+
}
28+
}
29+
30+
for (int nn : edges[cn]) {
31+
if (v[nn] == t) continue;
32+
v[nn] = t;
33+
34+
if (dfs(match[nn])) {
35+
match[nn] = cn;
36+
return true;
37+
}
38+
}
39+
40+
return false;
41+
}
42+
43+
int main() {
44+
ios::sync_with_stdio(0);
45+
cin.tie(0);
46+
cout.tie(0);
47+
48+
while (1) {
49+
cin >> n >> m;
50+
if (!n && !m) break;
51+
52+
for (int i = 1; i <= n; ++i) {
53+
cin >> arr[i];
54+
edges[i].clear();
55+
}
56+
57+
for (int i = 1; i <= m; ++i) {
58+
int a; cin >> a;
59+
60+
for (int j = 1; j <= n; ++j) {
61+
int g = get_gcd(arr[j], a);
62+
if (g == 1) continue;
63+
64+
edges[j].push_back(i);
65+
}
66+
}
67+
68+
int ans = 0;
69+
memset(match, 0, sizeof(match));
70+
for (int i = 1; i <= n; ++i, ++t) {
71+
if (dfs(i)) ++ans;
72+
}
73+
cout << ans << "\n";
74+
}
75+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# [Platinum IV] Cards - 4966
2+
3+
[문제 링크](https://www.acmicpc.net/problem/4966)
4+
5+
### 성능 요약
6+
7+
메모리: 3228 KB, 시간: 440 ms
8+
9+
### 분류
10+
11+
수학, 정수론, 유클리드 호제법, 이분 매칭
12+
13+
### 제출 일자
14+
15+
2026년 1월 30일 11:34:51
16+
17+
### 문제 설명
18+
19+
<p>There are many blue cards and red cards on the table. For each card, an integer number greater than 1 is printed on its face. The same number may be printed on several cards.</p>
20+
21+
<p>A blue card and a red card can be paired when both of the numbers printed on them have a common divisor greater than 1. There may be more than one red card that can be paired with one blue card. Also, there may be more than one blue card that can be paired with one red card. When a blue card and a red card are chosen and paired, these two cards are removed from the whole cards on the table.</p>
22+
23+
<p style="text-align: center;"><img alt="" src="https://www.acmicpc.net/upload/images3/E1.png" style="height:283px; width:394px"></p>
24+
25+
<p style="text-align: center;">Figure E-1: Four blue cards and three red cards</p>
26+
27+
<p>For example, in Figure E-1, there are four blue cards and three red cards. Numbers 2, 6, 6 and 15 are printed on the faces of the four blue cards, and 2, 3 and 35 are printed on those of the three red cards. Here, you can make pairs of blue cards and red cards as follows. First, the blue card with number 2 on it and the red card with 2 are paired and removed. Second, one of the two blue cards with 6 and the red card with 3 are paired and removed. Finally, the blue card with 15 and the red card with 35 are paired and removed. Thus the number of removed pairs is three.</p>
28+
29+
<p>Note that the total number of the pairs depends on the way of choosing cards to be paired. The blue card with 15 and the red card with 3 might be paired and removed at the beginning. In this case, there are only one more pair that can be removed and the total number of the removed pairs is two.</p>
30+
31+
<p>Your job is to find the largest number of pairs that can be removed from the given set of cards on the table.</p>
32+
33+
### 입력
34+
35+
<p>The input is a sequence of datasets. The number of the datasets is less than or equal to 100. Each dataset is formatted as follows.</p>
36+
37+
<pre>m n
38+
b1 ... bk ... bm
39+
r1 ... rk ... rn </pre>
40+
41+
<p>The integers m and n are the number of blue cards and that of red cards, respectively. You may assume 1 ≤ m ≤ 500 and 1≤ n ≤ 500. bk (1 ≤ k ≤ m) and rk (1 ≤ k ≤ n) are numbers printed on the blue cards and the red cards respectively, that are integers greater than or equal to 2 and less than 10000000 (=10<sup>7</sup>). The input integers are separated by a space or a newline. Each of bm and rn is followed by a newline. There are no other characters in the dataset.</p>
42+
43+
<p>The end of the input is indicated by a line containing two zeros separated by a space.</p>
44+
45+
### 출력
46+
47+
<p>For each dataset, output a line containing an integer that indicates the maximum of the number of the pairs.</p>
48+

0 commit comments

Comments
 (0)