#include <bits/stdc++.h>
#define pb push_back
#define INF 987654321
using namespace std;
typedef long long LL;
int n, m, x;
char str[3010][3010];
int col[3010][3010][3], re[3010][3010], ans;
vector<int> link[8000010], id;
int par[8000010];
bool used[8000010];
int dist[8000010], A[8000010], B[8000010];
vector<vector<int> > vc;
int findpar(int num)
{
if(num==par[num])return num;
return par[num]=findpar(par[num]);
}
void mergepar(int a, int b)
{
a=findpar(a);
b=findpar(b);
par[a]=par[b];
}
void bfs(int p)
{
queue<int> Q;
for(int i=0; i<vc[p].size(); i++){
if(!used[vc[p][i]]){
dist[vc[p][i]]=0;
Q.push(vc[p][i]);
}
else dist[vc[p][i]]=INF;
}
while(!Q.empty()){
int a=Q.front();
Q.pop();
for(int b: link[a]){
if(B[b]!=-1&&dist[B[b]]==INF){
dist[B[b]]=dist[a]+1;
Q.push(B[b]);
}
}
}
}
bool dfs(int a){
for(int b: link[a]){
if(B[b]==-1||dist[B[b]]==dist[a]+1&&dfs(B[b])){
used[a]=true;
A[a]=b;
B[b]=a;
return true;
}
}
return false;
}
int hopcroft_carp(int p)
{
int match=0;
while(1){
bfs(p);
int flow = 0;
for(int i=0; i<vc[p].size(); i++)
if(!used[vc[p][i]] && dfs(vc[p][i]))flow++;
if(!flow)break;
match+=flow;
}
return match/2;
}
int main()
{
scanf("%d%d", &n, &m);
for(int i=1; i<=n; i++){
scanf("%s", str[i]+1);
}
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
if(str[i][j]=='R'&&str[i][j+1]=='G'&&str[i][j+2]=='W'){
++x;
col[i][j][++re[i][j]]=x;
col[i][j+1][++re[i][j+1]]=x;
col[i][j+2][++re[i][j+2]]=x;
}
if(str[i][j]=='R'&&str[i+1][j]=='G'&&str[i+2][j]=='W'){
++x;
col[i][j][++re[i][j]]=x;
col[i+1][j][++re[i+1][j]]=x;
col[i+2][j][++re[i+2][j]]=x;
}
}
}
for(int i=1; i<=x; i++)par[i]=i;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
if(re[i][j]==2){
mergepar(col[i][j][1], col[i][j][2]);
link[col[i][j][1]].pb(col[i][j][2]);
link[col[i][j][2]].pb(col[i][j][1]);
}
}
}
for(int i=1; i<=x; i++){
id.pb(findpar(i));
}
sort(id.begin(), id.end());
id.erase(unique(id.begin(), id.end()), id.end());
int maxpar=1;
for(int i=1; i<=x; i++){
par[i]=lower_bound(id.begin(), id.end(), par[i])-id.begin();
maxpar=max(maxpar, par[i]+1);
}
vc.resize(maxpar);
for(int i=1; i<=x; i++){
vc[par[i]].pb(i);
}
fill(A+1, A+x+1, -1);
fill(B+1, B+x+1, -1);
for(int i=0; i<maxpar; i++){
if(vc[i].size()==1)ans++;
else ans+=vc[i].size()-hopcroft_carp(i);
}
printf("%d", ans);
}
Compilation message
dango_maker.cpp: In function 'void bfs(int)':
dango_maker.cpp:28:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<vc[p].size(); i++){
~^~~~~~~~~~~~~
dango_maker.cpp: In function 'bool dfs(int)':
dango_maker.cpp:48:43: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
if(B[b]==-1||dist[B[b]]==dist[a]+1&&dfs(B[b])){
~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~
dango_maker.cpp: In function 'int hopcroft_carp(int)':
dango_maker.cpp:63:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<vc[p].size(); i++)
~^~~~~~~~~~~~~
dango_maker.cpp: In function 'int main()':
dango_maker.cpp:72:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~
dango_maker.cpp:74:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", str[i]+1);
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
170 ms |
188180 KB |
Output is correct |
2 |
Correct |
170 ms |
188216 KB |
Output is correct |
3 |
Correct |
169 ms |
188272 KB |
Output is correct |
4 |
Correct |
170 ms |
188444 KB |
Output is correct |
5 |
Correct |
171 ms |
188180 KB |
Output is correct |
6 |
Correct |
169 ms |
188316 KB |
Output is correct |
7 |
Correct |
171 ms |
188216 KB |
Output is correct |
8 |
Correct |
175 ms |
188440 KB |
Output is correct |
9 |
Correct |
177 ms |
188256 KB |
Output is correct |
10 |
Correct |
171 ms |
188232 KB |
Output is correct |
11 |
Correct |
171 ms |
188364 KB |
Output is correct |
12 |
Correct |
170 ms |
188280 KB |
Output is correct |
13 |
Correct |
171 ms |
188316 KB |
Output is correct |
14 |
Correct |
170 ms |
188280 KB |
Output is correct |
15 |
Correct |
170 ms |
188280 KB |
Output is correct |
16 |
Correct |
170 ms |
188308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
170 ms |
188180 KB |
Output is correct |
2 |
Correct |
170 ms |
188216 KB |
Output is correct |
3 |
Correct |
169 ms |
188272 KB |
Output is correct |
4 |
Correct |
170 ms |
188444 KB |
Output is correct |
5 |
Correct |
171 ms |
188180 KB |
Output is correct |
6 |
Correct |
169 ms |
188316 KB |
Output is correct |
7 |
Correct |
171 ms |
188216 KB |
Output is correct |
8 |
Correct |
175 ms |
188440 KB |
Output is correct |
9 |
Correct |
177 ms |
188256 KB |
Output is correct |
10 |
Correct |
171 ms |
188232 KB |
Output is correct |
11 |
Correct |
171 ms |
188364 KB |
Output is correct |
12 |
Correct |
170 ms |
188280 KB |
Output is correct |
13 |
Correct |
171 ms |
188316 KB |
Output is correct |
14 |
Correct |
170 ms |
188280 KB |
Output is correct |
15 |
Correct |
170 ms |
188280 KB |
Output is correct |
16 |
Correct |
170 ms |
188308 KB |
Output is correct |
17 |
Correct |
168 ms |
188240 KB |
Output is correct |
18 |
Correct |
169 ms |
188280 KB |
Output is correct |
19 |
Correct |
178 ms |
188348 KB |
Output is correct |
20 |
Correct |
170 ms |
188308 KB |
Output is correct |
21 |
Correct |
173 ms |
188280 KB |
Output is correct |
22 |
Correct |
171 ms |
188380 KB |
Output is correct |
23 |
Correct |
194 ms |
188280 KB |
Output is correct |
24 |
Correct |
171 ms |
188368 KB |
Output is correct |
25 |
Correct |
194 ms |
188304 KB |
Output is correct |
26 |
Correct |
194 ms |
188316 KB |
Output is correct |
27 |
Correct |
176 ms |
188280 KB |
Output is correct |
28 |
Correct |
171 ms |
188508 KB |
Output is correct |
29 |
Correct |
191 ms |
188332 KB |
Output is correct |
30 |
Correct |
208 ms |
188280 KB |
Output is correct |
31 |
Correct |
201 ms |
188288 KB |
Output is correct |
32 |
Correct |
173 ms |
188280 KB |
Output is correct |
33 |
Correct |
178 ms |
188360 KB |
Output is correct |
34 |
Correct |
170 ms |
188452 KB |
Output is correct |
35 |
Correct |
172 ms |
188444 KB |
Output is correct |
36 |
Correct |
171 ms |
188464 KB |
Output is correct |
37 |
Correct |
171 ms |
188432 KB |
Output is correct |
38 |
Correct |
170 ms |
188408 KB |
Output is correct |
39 |
Correct |
169 ms |
188404 KB |
Output is correct |
40 |
Correct |
173 ms |
188408 KB |
Output is correct |
41 |
Correct |
170 ms |
188408 KB |
Output is correct |
42 |
Correct |
171 ms |
188344 KB |
Output is correct |
43 |
Correct |
186 ms |
188408 KB |
Output is correct |
44 |
Correct |
171 ms |
188332 KB |
Output is correct |
45 |
Correct |
170 ms |
188412 KB |
Output is correct |
46 |
Correct |
172 ms |
188340 KB |
Output is correct |
47 |
Correct |
171 ms |
188280 KB |
Output is correct |
48 |
Correct |
171 ms |
188332 KB |
Output is correct |
49 |
Correct |
170 ms |
188336 KB |
Output is correct |
50 |
Correct |
170 ms |
188280 KB |
Output is correct |
51 |
Correct |
172 ms |
188364 KB |
Output is correct |
52 |
Correct |
170 ms |
188280 KB |
Output is correct |
53 |
Correct |
171 ms |
188408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
170 ms |
188180 KB |
Output is correct |
2 |
Correct |
170 ms |
188216 KB |
Output is correct |
3 |
Correct |
169 ms |
188272 KB |
Output is correct |
4 |
Correct |
170 ms |
188444 KB |
Output is correct |
5 |
Correct |
171 ms |
188180 KB |
Output is correct |
6 |
Correct |
169 ms |
188316 KB |
Output is correct |
7 |
Correct |
171 ms |
188216 KB |
Output is correct |
8 |
Correct |
175 ms |
188440 KB |
Output is correct |
9 |
Correct |
177 ms |
188256 KB |
Output is correct |
10 |
Correct |
171 ms |
188232 KB |
Output is correct |
11 |
Correct |
171 ms |
188364 KB |
Output is correct |
12 |
Correct |
170 ms |
188280 KB |
Output is correct |
13 |
Correct |
171 ms |
188316 KB |
Output is correct |
14 |
Correct |
170 ms |
188280 KB |
Output is correct |
15 |
Correct |
170 ms |
188280 KB |
Output is correct |
16 |
Correct |
170 ms |
188308 KB |
Output is correct |
17 |
Correct |
168 ms |
188240 KB |
Output is correct |
18 |
Correct |
169 ms |
188280 KB |
Output is correct |
19 |
Correct |
178 ms |
188348 KB |
Output is correct |
20 |
Correct |
170 ms |
188308 KB |
Output is correct |
21 |
Correct |
173 ms |
188280 KB |
Output is correct |
22 |
Correct |
171 ms |
188380 KB |
Output is correct |
23 |
Correct |
194 ms |
188280 KB |
Output is correct |
24 |
Correct |
171 ms |
188368 KB |
Output is correct |
25 |
Correct |
194 ms |
188304 KB |
Output is correct |
26 |
Correct |
194 ms |
188316 KB |
Output is correct |
27 |
Correct |
176 ms |
188280 KB |
Output is correct |
28 |
Correct |
171 ms |
188508 KB |
Output is correct |
29 |
Correct |
191 ms |
188332 KB |
Output is correct |
30 |
Correct |
208 ms |
188280 KB |
Output is correct |
31 |
Correct |
201 ms |
188288 KB |
Output is correct |
32 |
Correct |
173 ms |
188280 KB |
Output is correct |
33 |
Correct |
178 ms |
188360 KB |
Output is correct |
34 |
Correct |
170 ms |
188452 KB |
Output is correct |
35 |
Correct |
172 ms |
188444 KB |
Output is correct |
36 |
Correct |
171 ms |
188464 KB |
Output is correct |
37 |
Correct |
171 ms |
188432 KB |
Output is correct |
38 |
Correct |
170 ms |
188408 KB |
Output is correct |
39 |
Correct |
169 ms |
188404 KB |
Output is correct |
40 |
Correct |
173 ms |
188408 KB |
Output is correct |
41 |
Correct |
170 ms |
188408 KB |
Output is correct |
42 |
Correct |
171 ms |
188344 KB |
Output is correct |
43 |
Correct |
186 ms |
188408 KB |
Output is correct |
44 |
Correct |
171 ms |
188332 KB |
Output is correct |
45 |
Correct |
170 ms |
188412 KB |
Output is correct |
46 |
Correct |
172 ms |
188340 KB |
Output is correct |
47 |
Correct |
171 ms |
188280 KB |
Output is correct |
48 |
Correct |
171 ms |
188332 KB |
Output is correct |
49 |
Correct |
170 ms |
188336 KB |
Output is correct |
50 |
Correct |
170 ms |
188280 KB |
Output is correct |
51 |
Correct |
172 ms |
188364 KB |
Output is correct |
52 |
Correct |
170 ms |
188280 KB |
Output is correct |
53 |
Correct |
171 ms |
188408 KB |
Output is correct |
54 |
Correct |
171 ms |
188276 KB |
Output is correct |
55 |
Correct |
182 ms |
199800 KB |
Output is correct |
56 |
Correct |
172 ms |
188808 KB |
Output is correct |
57 |
Correct |
215 ms |
210544 KB |
Output is correct |
58 |
Correct |
245 ms |
220932 KB |
Output is correct |
59 |
Runtime error |
320 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
60 |
Halted |
0 ms |
0 KB |
- |