#include <bits/stdc++.h>
using namespace std;
class BipartiteMatcher{
int n, m;
vector<int> l;
vector<int> r;
vector<vector<int> > graph;
vector<bool> viz;
bool pairup(int nod){
if(viz[nod] == true){
return false;
}
viz[nod] = true;
for(auto it:graph[nod]){
if(r[it] == -1){
l[nod] = it;
r[it] = nod;
return true;
}
}
for(auto it:graph[nod]){
if(pairup(r[it])){
l[nod] = it;
r[it] = nod;
return true;
}
}
return false;
}
public:
BipartiteMatcher(int n, int m) {
this->n = n;
this->m = m;
this->l = vector<int>(n, -1);
this->r = vector<int>(m, -1);
this->graph = vector<vector<int> >(n, vector<int>());
this->viz = vector<bool>(n, false);
}
void add_edge(int x, int y){
graph[x].push_back(y);
}
int solve(){
bool ok = true;
while(ok){
ok = false;
for(int i = 0;i < n;i++){
viz[i] = false;
}
for(int i = 0;i < n;i++){
if(l[i] == -1 && pairup(i)){
ok = true;
}
}
}
int cuplaj = 0;
for(int i = 0;i < n;i++){
cuplaj += (l[i] != -1);
}
return cuplaj;
}
};
int main(){
int n, m;
cin >> n >> m;
vector<string> v(n,"");
vector<vector<int> > group(n, vector<int>(m, -1));
for(int i = 0;i < n;i++){
cin >> v[i];
}
int cnt_horizontal = 0;
int cnt_vertical = 0;
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
if(j + 2 < m && v[i][j] == 'R' && v[i][j + 1] == 'G' && v[i][j + 2] == 'W'){
group[i][j] = cnt_horizontal;
group[i][j + 1] = cnt_horizontal;
group[i][j + 2] = cnt_horizontal;
cnt_horizontal++;
}
if(i + 2 < n && v[i][j] == 'R' && v[i + 1][j] == 'G' && v[i + 2][j] == 'W'){
cnt_vertical++;
}
}
}
BipartiteMatcher matcher(cnt_horizontal, cnt_vertical);
int last_vertical = 0;
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
if(i + 2 < n && v[i][j] == 'R' && v[i + 1][j] == 'G' && v[i + 2][j] == 'W'){
if(group[i][j] != -1){
matcher.add_edge(group[i][j], last_vertical);
}
if(group[i + 1][j] != -1){
matcher.add_edge(group[i + 1][j], last_vertical);
}
if(group[i + 2][j] != -1){
matcher.add_edge(group[i + 2][j], last_vertical);
}
last_vertical++;
}
}
}
printf("%d", cnt_horizontal + cnt_vertical - matcher.solve());
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
204 KB |
Output is correct |
23 |
Correct |
0 ms |
204 KB |
Output is correct |
24 |
Correct |
0 ms |
204 KB |
Output is correct |
25 |
Correct |
0 ms |
204 KB |
Output is correct |
26 |
Correct |
0 ms |
204 KB |
Output is correct |
27 |
Correct |
0 ms |
204 KB |
Output is correct |
28 |
Correct |
0 ms |
204 KB |
Output is correct |
29 |
Correct |
0 ms |
204 KB |
Output is correct |
30 |
Correct |
0 ms |
204 KB |
Output is correct |
31 |
Correct |
0 ms |
204 KB |
Output is correct |
32 |
Correct |
0 ms |
204 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
0 ms |
204 KB |
Output is correct |
35 |
Correct |
0 ms |
204 KB |
Output is correct |
36 |
Correct |
0 ms |
204 KB |
Output is correct |
37 |
Correct |
0 ms |
204 KB |
Output is correct |
38 |
Correct |
0 ms |
204 KB |
Output is correct |
39 |
Correct |
0 ms |
204 KB |
Output is correct |
40 |
Correct |
0 ms |
204 KB |
Output is correct |
41 |
Correct |
0 ms |
204 KB |
Output is correct |
42 |
Correct |
0 ms |
204 KB |
Output is correct |
43 |
Correct |
0 ms |
204 KB |
Output is correct |
44 |
Correct |
0 ms |
204 KB |
Output is correct |
45 |
Correct |
1 ms |
204 KB |
Output is correct |
46 |
Correct |
0 ms |
204 KB |
Output is correct |
47 |
Correct |
1 ms |
204 KB |
Output is correct |
48 |
Correct |
1 ms |
204 KB |
Output is correct |
49 |
Correct |
0 ms |
204 KB |
Output is correct |
50 |
Correct |
0 ms |
204 KB |
Output is correct |
51 |
Correct |
0 ms |
204 KB |
Output is correct |
52 |
Correct |
0 ms |
204 KB |
Output is correct |
53 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
204 KB |
Output is correct |
23 |
Correct |
0 ms |
204 KB |
Output is correct |
24 |
Correct |
0 ms |
204 KB |
Output is correct |
25 |
Correct |
0 ms |
204 KB |
Output is correct |
26 |
Correct |
0 ms |
204 KB |
Output is correct |
27 |
Correct |
0 ms |
204 KB |
Output is correct |
28 |
Correct |
0 ms |
204 KB |
Output is correct |
29 |
Correct |
0 ms |
204 KB |
Output is correct |
30 |
Correct |
0 ms |
204 KB |
Output is correct |
31 |
Correct |
0 ms |
204 KB |
Output is correct |
32 |
Correct |
0 ms |
204 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
0 ms |
204 KB |
Output is correct |
35 |
Correct |
0 ms |
204 KB |
Output is correct |
36 |
Correct |
0 ms |
204 KB |
Output is correct |
37 |
Correct |
0 ms |
204 KB |
Output is correct |
38 |
Correct |
0 ms |
204 KB |
Output is correct |
39 |
Correct |
0 ms |
204 KB |
Output is correct |
40 |
Correct |
0 ms |
204 KB |
Output is correct |
41 |
Correct |
0 ms |
204 KB |
Output is correct |
42 |
Correct |
0 ms |
204 KB |
Output is correct |
43 |
Correct |
0 ms |
204 KB |
Output is correct |
44 |
Correct |
0 ms |
204 KB |
Output is correct |
45 |
Correct |
1 ms |
204 KB |
Output is correct |
46 |
Correct |
0 ms |
204 KB |
Output is correct |
47 |
Correct |
1 ms |
204 KB |
Output is correct |
48 |
Correct |
1 ms |
204 KB |
Output is correct |
49 |
Correct |
0 ms |
204 KB |
Output is correct |
50 |
Correct |
0 ms |
204 KB |
Output is correct |
51 |
Correct |
0 ms |
204 KB |
Output is correct |
52 |
Correct |
0 ms |
204 KB |
Output is correct |
53 |
Correct |
0 ms |
204 KB |
Output is correct |
54 |
Correct |
0 ms |
204 KB |
Output is correct |
55 |
Correct |
1 ms |
460 KB |
Output is correct |
56 |
Correct |
1 ms |
332 KB |
Output is correct |
57 |
Correct |
1 ms |
460 KB |
Output is correct |
58 |
Correct |
38 ms |
7812 KB |
Output is correct |
59 |
Correct |
333 ms |
60752 KB |
Output is correct |
60 |
Correct |
348 ms |
60768 KB |
Output is correct |
61 |
Correct |
341 ms |
60716 KB |
Output is correct |
62 |
Correct |
0 ms |
332 KB |
Output is correct |
63 |
Correct |
321 ms |
59144 KB |
Output is correct |
64 |
Correct |
740 ms |
244156 KB |
Output is correct |
65 |
Correct |
788 ms |
243984 KB |
Output is correct |
66 |
Correct |
856 ms |
243744 KB |
Output is correct |
67 |
Correct |
227 ms |
67708 KB |
Output is correct |
68 |
Correct |
233 ms |
67364 KB |
Output is correct |
69 |
Correct |
232 ms |
66568 KB |
Output is correct |
70 |
Correct |
80 ms |
28020 KB |
Output is correct |
71 |
Correct |
83 ms |
27924 KB |
Output is correct |
72 |
Correct |
86 ms |
27944 KB |
Output is correct |
73 |
Correct |
85 ms |
27948 KB |
Output is correct |
74 |
Correct |
93 ms |
28160 KB |
Output is correct |
75 |
Correct |
86 ms |
28028 KB |
Output is correct |
76 |
Correct |
86 ms |
28020 KB |
Output is correct |
77 |
Correct |
86 ms |
27996 KB |
Output is correct |
78 |
Correct |
87 ms |
27912 KB |
Output is correct |
79 |
Correct |
95 ms |
27956 KB |
Output is correct |
80 |
Correct |
115 ms |
27996 KB |
Output is correct |
81 |
Correct |
88 ms |
27972 KB |
Output is correct |
82 |
Correct |
89 ms |
27972 KB |
Output is correct |
83 |
Correct |
97 ms |
27996 KB |
Output is correct |
84 |
Correct |
89 ms |
27960 KB |
Output is correct |
85 |
Correct |
88 ms |
27928 KB |
Output is correct |
86 |
Correct |
86 ms |
27944 KB |
Output is correct |
87 |
Correct |
96 ms |
27940 KB |
Output is correct |
88 |
Correct |
88 ms |
27888 KB |
Output is correct |
89 |
Correct |
88 ms |
27972 KB |
Output is correct |
90 |
Correct |
90 ms |
27840 KB |
Output is correct |