#include<bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define ff first
#define ss second
#define ll long long
#define pll pair<ll, ll>
#define pb push_back
const int MAXLS = 5000000;
int dq[3010][3010];
int a[3021][3021];
int kto[MAXLS];
int bd[MAXLS];
int cnt[3];
int pos[MAXLS];
vector<int> q[MAXLS];
vector<int> ord;
int fup[MAXLS], tin[MAXLS], timer = 1;
int dp[MAXLS][2];
void dfs1(int v, int p = -1){
pos[v] = 1;
ord.pb(v);
int cnt = 0;
fup[v] = tin[v] = timer++;
int dcnt = 0;
for(int to: q[v]){
if(to == p){
continue;
}
if(pos[to]){
fup[v] = min(fup[v],tin[to]);
}
else{
dfs1(to, v);
fup[v] = min(fup[v], fup[to]);
if(fup[to] > tin[v]){
bd[v] ++;
bd[to] ++;
}
cnt++;
}
}
}
vector<int> dord;
int bdcnt = 0;
void dfs2(int v){
pos[v] = 1;
bdcnt ++;
//cout<<v<<' '<<bd[v]<<'\n';
for(int to: q[v]){
if(!bd[to] and bd[v] == 2) dord.pb(to);
if(pos[to] or !bd[to]) continue;
dfs2(to);
}
}
void dfs3(int v){
pos[v] = 1;
dp[v][0] = 0;
dp[v][1] = 1;
for(int to: q[v]){
if(pos[to] or bd[to]) continue;
dfs3(to);
dp[v][1] += dp[to][0];
dp[v][0] += dp[to][1];
}
dp[v][1] = max(dp[v][1], dp[v][0]);
}
void add(int i, int j, int x){
if(dq[i][j] != -1) q[dq[i][j]].pb(x), q[x].pb(dq[i][j]);
dq[i][j] = x;
}
void solve(){
int n, m;
cin>>n>>m;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
char c;
cin>>c;
a[i][j] = c;
dq[i][j] = -1;
}
}
int ls = 0;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
if(j >= 3 and a[i][j] == 'W' and a[i][j-1] == 'G' and a[i][j - 2] == 'R'){
//cout<<i<<' '<<j<<' '<<ls+1<<" h\n";
ls ++;
add(i, j, ls);
add(i, j-1, ls);
add(i, j-2, ls);
kto[ls] = 1;
}
if(i >= 3 and a[i][j] == 'W' and a[i-1][j] == 'G' and a[i-2][j] == 'R'){
//cout<<i<<' '<<j<<' '<<ls+1<<" v\n";
ls ++;
add(i, j, ls);
add(i-1, j, ls);
add(i-2, j, ls);
kto[ls] = 2;
}
}
}
if(ls > MAXLS) cout<<1/0;
//cout<<ls<<'\n';
for(int i=1; i<=ls; i++){
//bd[i] = 1;
//cout<<i<<'\n';
//for(int v: q[i]) cout<<v<<' ';
//for(int v: q[i]) if(i < v) cout<<i<<' '<<v<<'\n';
//cout<<'\n';
}
ll ans = 0;
//cout<<ls<<'\n';
for(int i=1; i<=ls; i++){
if(pos[i]) continue;
ord.clear();
dfs1(i);
for(int v: ord){
pos[v] = 0;
if(bd[v] != q[v].size() and bd[v] != 0) bd[v] = 2;
else if(bd[v] != q[v].size()) bd[v] = 1;
else bd[v] = 0;
}
int cntb = 0;
//cout<<cntb<<'\n';
for(int v:ord){
if(bd[v] and !pos[v]){
dord.clear();
bdcnt = 0;
dfs2(v);
//cout<<bdcnt<<'\n';
//for(int u: dord) cout<<u<<'\n';
if(dord.size() > 1 and ((bdcnt / 2 % 2 == 0 and kto[dord[0]] != kto[dord[1]] or
(bdcnt / 2 % 2 == 1 and kto[dord[0]] != kto[dord[1]])))){
q[dord[0]].pb(dord[1]);
q[dord[1]].pb(dord[0]);
//cout<<"ASDASD";
}
}
}
for(int v: ord){
//cout<<v<<' '<<bd[v]<<'\n';
if(bd[v]) cntb++;
}
ans += cntb / 2;
//cout<<'\n';
for(int v: ord){
if(pos[v] or bd[v]) continue;
cnt[1] = cnt[2] = 0;
dfs3(v);
ans += max(dp[v][1], dp[v][0]);
//cout<<v<<' '<<max(cnt[1], cnt[2])<<'\n';
}
}
cout<<ans<<'\n';
}
int main(){
ios_base::sync_with_stdio(NULL), cin.tie(NULL), cout.tie(NULL);
solve();
}
Compilation message
dango_maker.cpp: In function 'void dfs1(int, int)':
dango_maker.cpp:28:9: warning: unused variable 'dcnt' [-Wunused-variable]
28 | int dcnt = 0;
| ^~~~
dango_maker.cpp: In function 'void solve()':
dango_maker.cpp:113:27: warning: division by zero [-Wdiv-by-zero]
113 | if(ls > MAXLS) cout<<1/0;
| ~^~
dango_maker.cpp:130:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
130 | if(bd[v] != q[v].size() and bd[v] != 0) bd[v] = 2;
| ~~~~~~^~~~~~~~~~~~~~
dango_maker.cpp:131:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
131 | else if(bd[v] != q[v].size()) bd[v] = 1;
| ~~~~~~^~~~~~~~~~~~~~
dango_maker.cpp:143:61: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
143 | if(dord.size() > 1 and ((bdcnt / 2 % 2 == 0 and kto[dord[0]] != kto[dord[1]] or
| ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
117844 KB |
Output is correct |
2 |
Correct |
45 ms |
117848 KB |
Output is correct |
3 |
Correct |
46 ms |
117784 KB |
Output is correct |
4 |
Correct |
45 ms |
117848 KB |
Output is correct |
5 |
Correct |
46 ms |
117852 KB |
Output is correct |
6 |
Correct |
46 ms |
117840 KB |
Output is correct |
7 |
Correct |
45 ms |
117840 KB |
Output is correct |
8 |
Correct |
49 ms |
117848 KB |
Output is correct |
9 |
Correct |
46 ms |
117844 KB |
Output is correct |
10 |
Correct |
45 ms |
117840 KB |
Output is correct |
11 |
Correct |
46 ms |
117840 KB |
Output is correct |
12 |
Correct |
45 ms |
117876 KB |
Output is correct |
13 |
Correct |
45 ms |
117720 KB |
Output is correct |
14 |
Correct |
47 ms |
117932 KB |
Output is correct |
15 |
Correct |
53 ms |
117900 KB |
Output is correct |
16 |
Correct |
53 ms |
117840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
117844 KB |
Output is correct |
2 |
Correct |
45 ms |
117848 KB |
Output is correct |
3 |
Correct |
46 ms |
117784 KB |
Output is correct |
4 |
Correct |
45 ms |
117848 KB |
Output is correct |
5 |
Correct |
46 ms |
117852 KB |
Output is correct |
6 |
Correct |
46 ms |
117840 KB |
Output is correct |
7 |
Correct |
45 ms |
117840 KB |
Output is correct |
8 |
Correct |
49 ms |
117848 KB |
Output is correct |
9 |
Correct |
46 ms |
117844 KB |
Output is correct |
10 |
Correct |
45 ms |
117840 KB |
Output is correct |
11 |
Correct |
46 ms |
117840 KB |
Output is correct |
12 |
Correct |
45 ms |
117876 KB |
Output is correct |
13 |
Correct |
45 ms |
117720 KB |
Output is correct |
14 |
Correct |
47 ms |
117932 KB |
Output is correct |
15 |
Correct |
53 ms |
117900 KB |
Output is correct |
16 |
Correct |
53 ms |
117840 KB |
Output is correct |
17 |
Correct |
45 ms |
117800 KB |
Output is correct |
18 |
Correct |
46 ms |
117840 KB |
Output is correct |
19 |
Correct |
47 ms |
117844 KB |
Output is correct |
20 |
Correct |
44 ms |
117852 KB |
Output is correct |
21 |
Correct |
52 ms |
117844 KB |
Output is correct |
22 |
Correct |
47 ms |
117844 KB |
Output is correct |
23 |
Correct |
46 ms |
117940 KB |
Output is correct |
24 |
Correct |
46 ms |
117836 KB |
Output is correct |
25 |
Correct |
46 ms |
117836 KB |
Output is correct |
26 |
Correct |
45 ms |
117840 KB |
Output is correct |
27 |
Correct |
47 ms |
117952 KB |
Output is correct |
28 |
Correct |
49 ms |
117840 KB |
Output is correct |
29 |
Correct |
46 ms |
117944 KB |
Output is correct |
30 |
Correct |
47 ms |
117844 KB |
Output is correct |
31 |
Correct |
55 ms |
117976 KB |
Output is correct |
32 |
Correct |
51 ms |
117844 KB |
Output is correct |
33 |
Correct |
51 ms |
117908 KB |
Output is correct |
34 |
Correct |
47 ms |
117840 KB |
Output is correct |
35 |
Correct |
51 ms |
117920 KB |
Output is correct |
36 |
Correct |
45 ms |
117980 KB |
Output is correct |
37 |
Correct |
48 ms |
117840 KB |
Output is correct |
38 |
Correct |
47 ms |
117844 KB |
Output is correct |
39 |
Correct |
51 ms |
117840 KB |
Output is correct |
40 |
Correct |
45 ms |
117848 KB |
Output is correct |
41 |
Correct |
51 ms |
117844 KB |
Output is correct |
42 |
Correct |
46 ms |
117840 KB |
Output is correct |
43 |
Correct |
57 ms |
117840 KB |
Output is correct |
44 |
Correct |
49 ms |
117840 KB |
Output is correct |
45 |
Correct |
47 ms |
117852 KB |
Output is correct |
46 |
Correct |
49 ms |
117804 KB |
Output is correct |
47 |
Correct |
46 ms |
117844 KB |
Output is correct |
48 |
Correct |
47 ms |
117840 KB |
Output is correct |
49 |
Correct |
44 ms |
117844 KB |
Output is correct |
50 |
Correct |
59 ms |
117844 KB |
Output is correct |
51 |
Correct |
53 ms |
117880 KB |
Output is correct |
52 |
Correct |
56 ms |
117944 KB |
Output is correct |
53 |
Correct |
56 ms |
117800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
117844 KB |
Output is correct |
2 |
Correct |
45 ms |
117848 KB |
Output is correct |
3 |
Correct |
46 ms |
117784 KB |
Output is correct |
4 |
Correct |
45 ms |
117848 KB |
Output is correct |
5 |
Correct |
46 ms |
117852 KB |
Output is correct |
6 |
Correct |
46 ms |
117840 KB |
Output is correct |
7 |
Correct |
45 ms |
117840 KB |
Output is correct |
8 |
Correct |
49 ms |
117848 KB |
Output is correct |
9 |
Correct |
46 ms |
117844 KB |
Output is correct |
10 |
Correct |
45 ms |
117840 KB |
Output is correct |
11 |
Correct |
46 ms |
117840 KB |
Output is correct |
12 |
Correct |
45 ms |
117876 KB |
Output is correct |
13 |
Correct |
45 ms |
117720 KB |
Output is correct |
14 |
Correct |
47 ms |
117932 KB |
Output is correct |
15 |
Correct |
53 ms |
117900 KB |
Output is correct |
16 |
Correct |
53 ms |
117840 KB |
Output is correct |
17 |
Correct |
45 ms |
117800 KB |
Output is correct |
18 |
Correct |
46 ms |
117840 KB |
Output is correct |
19 |
Correct |
47 ms |
117844 KB |
Output is correct |
20 |
Correct |
44 ms |
117852 KB |
Output is correct |
21 |
Correct |
52 ms |
117844 KB |
Output is correct |
22 |
Correct |
47 ms |
117844 KB |
Output is correct |
23 |
Correct |
46 ms |
117940 KB |
Output is correct |
24 |
Correct |
46 ms |
117836 KB |
Output is correct |
25 |
Correct |
46 ms |
117836 KB |
Output is correct |
26 |
Correct |
45 ms |
117840 KB |
Output is correct |
27 |
Correct |
47 ms |
117952 KB |
Output is correct |
28 |
Correct |
49 ms |
117840 KB |
Output is correct |
29 |
Correct |
46 ms |
117944 KB |
Output is correct |
30 |
Correct |
47 ms |
117844 KB |
Output is correct |
31 |
Correct |
55 ms |
117976 KB |
Output is correct |
32 |
Correct |
51 ms |
117844 KB |
Output is correct |
33 |
Correct |
51 ms |
117908 KB |
Output is correct |
34 |
Correct |
47 ms |
117840 KB |
Output is correct |
35 |
Correct |
51 ms |
117920 KB |
Output is correct |
36 |
Correct |
45 ms |
117980 KB |
Output is correct |
37 |
Correct |
48 ms |
117840 KB |
Output is correct |
38 |
Correct |
47 ms |
117844 KB |
Output is correct |
39 |
Correct |
51 ms |
117840 KB |
Output is correct |
40 |
Correct |
45 ms |
117848 KB |
Output is correct |
41 |
Correct |
51 ms |
117844 KB |
Output is correct |
42 |
Correct |
46 ms |
117840 KB |
Output is correct |
43 |
Correct |
57 ms |
117840 KB |
Output is correct |
44 |
Correct |
49 ms |
117840 KB |
Output is correct |
45 |
Correct |
47 ms |
117852 KB |
Output is correct |
46 |
Correct |
49 ms |
117804 KB |
Output is correct |
47 |
Correct |
46 ms |
117844 KB |
Output is correct |
48 |
Correct |
47 ms |
117840 KB |
Output is correct |
49 |
Correct |
44 ms |
117844 KB |
Output is correct |
50 |
Correct |
59 ms |
117844 KB |
Output is correct |
51 |
Correct |
53 ms |
117880 KB |
Output is correct |
52 |
Correct |
56 ms |
117944 KB |
Output is correct |
53 |
Correct |
56 ms |
117800 KB |
Output is correct |
54 |
Correct |
45 ms |
117924 KB |
Output is correct |
55 |
Correct |
52 ms |
141988 KB |
Output is correct |
56 |
Correct |
45 ms |
118104 KB |
Output is correct |
57 |
Correct |
66 ms |
134120 KB |
Output is correct |
58 |
Correct |
68 ms |
136592 KB |
Output is correct |
59 |
Correct |
247 ms |
213328 KB |
Output is correct |
60 |
Correct |
237 ms |
213328 KB |
Output is correct |
61 |
Correct |
241 ms |
213340 KB |
Output is correct |
62 |
Correct |
47 ms |
117844 KB |
Output is correct |
63 |
Correct |
255 ms |
212820 KB |
Output is correct |
64 |
Runtime error |
287 ms |
262144 KB |
Execution killed with signal 9 |
65 |
Halted |
0 ms |
0 KB |
- |