#include<bits/stdc++.h>
using namespace std;
// Edge structure: handles node pointed to, capacity
// and the index in v[to] to its reverse edge
struct Edge{
int to, rev;
int cap; //
Edge(int t, int c, int r, bool irev):to{t},rev{r},cap{c}
{}
};
struct Flow{
int n, s, t; // n: # nodes, s: source, t: sink
vector<vector<Edge>> v; // extended adj list
vector<int> lvl; // indicates the lvl for each node. used for the lvl graph
// assures that each edge is completely visited only once. keeps sort of backup
// of the index of the last visited edge
// there is no need to revisit an edge, hence, these can be skipped
vector<int> lastPos;
// constructor
Flow(int N, int src, int snk){
this->n = N;
this->s = src;
this->t = snk;
v = vector<vector<Edge>>(n, vector<Edge>());
}
// adding edges: endpoints and capacities
// in case of bidirecitonal edges, use this function twice, one for each direction
// using the full capacity on both
void addEdge(int a, int b, int c){
v[a].push_back({b,c,(int)v[b].size(),false});
v[b].push_back({a,0,((int)v[a].size()) - 1,true});
}
// "builds" the lvl graph. simple bfs indicating the lvl for each node
void getLvls(){
queue<int> q; q.push(s);
lvl.assign(n,-1); lvl[s] = 0;
while(q.size()){
int x = q.front(); q.pop();
for(auto e : v[x]){
if(lvl[e.to] == -1 && e.cap){
// lvl graph only reaches new nodes and possible edges (cap > 0)
q.push(e.to);
lvl[e.to] = lvl[x] + 1;
}
}
}
}
// finds an augmenting path and updates edges
int dfs(int i, int curr = INT_MAX){
if(i == t){
return curr; // reached sink, returning collected flow
}else{
for(; lastPos[i] < v[i].size(); ++lastPos[i]){
auto &e = v[i][lastPos[i]];
if(e.cap && lvl[i] + 1 == lvl[e.to]){
// possible edges (cap > 0) and lvl-graph structure (consecutive lvls traversal)
int maybe_flow = dfs(e.to, min(curr, e.cap));
if(maybe_flow == 0) continue;
e.cap -= maybe_flow; // updating edge
v[e.to][e.rev].cap += maybe_flow; // and its reverse
return maybe_flow;
}
}
}
return 0;
}
// main funct
int run(){
int flow = 0; // initial flow
while(true){
getLvls(); // builds lvl graph
// if the sink cant be reached, the max flow has been found
if(lvl[t]==-1) break;
// get a blocking flow for the current lvl graph
int blocking = -1;
lastPos.assign(n,0);
while((blocking = dfs(s))) flow += blocking;
}
return flow;
}
};
const int SRC = 0;
const int SINK = 1;
const int OFFSET = 2;
const int MAXN = 3001;
string s[MAXN];
int idv[MAXN][MAXN];
vector<pair<int,int>> es;
int main(){
memset(idv, -1, sizeof idv);
cin.tie(NULL)->sync_with_stdio(false);
int n, m; cin >> n >> m;
for(int i = 0; i < n; ++i){
cin >> s[i];
s[i].shrink_to_fit();
}
int curr_idv = 0;
for(int i = 0; i < n; ++i){
for(int j = 0; j < m; ++j){
// ver
if(i + 2 < n){
if((s[i][j] == 'R') && (s[i+1][j] == 'G') && (s[i+2][j] == 'W')){
idv[i][j] = curr_idv;
idv[i+1][j] = curr_idv;
idv[i+2][j] = curr_idv;
curr_idv++;
}
}
}
}
int curr_idh = 0;
for(int i = 0; i < n; ++i){
for(int j = 0; j < m; ++j){
if(j + 2 < m){
if((s[i][j] == 'R') && (s[i][j+1] == 'G') && (s[i][j+2] == 'W')){
if(idv[i][j] != -1) es.push_back({curr_idh, idv[i][j]});
if(idv[i][j+1] != -1) es.push_back({curr_idh, idv[i][j+1]});
if(idv[i][j+2] != -1) es.push_back({curr_idh, idv[i][j+2]});
curr_idh++;
}
}
}
}
// ESQ: VERTICAIS
// DIR: HORIZONTAIS
Flow f(2 + curr_idh + curr_idv, SRC, SINK);
for(int i = 0; i < curr_idv; ++i){
f.addEdge(SRC, i + OFFSET, 1);
}
for(int i = 0; i < curr_idh; ++i){
f.addEdge(i + curr_idv + OFFSET, SINK, 1);
}
while(es.size()){
auto [a, b] = es.back();
es.pop_back();
f.addEdge(b + OFFSET, curr_idv + a + OFFSET, 1);
}
int x = f.run();
// cout << x << " " << curr_idh << " " << curr_idv<< endl;
cout << curr_idh + curr_idv - x << "\n";
}
Compilation message
dango_maker.cpp: In member function 'int Flow::dfs(int, int)':
dango_maker.cpp:61:24: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} and 'std::vector<Edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for(; lastPos[i] < v[i].size(); ++lastPos[i]){
dango_maker.cpp: In function 'int main()':
dango_maker.cpp:155:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
155 | auto [a, b] = es.back();
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
35540 KB |
Output is correct |
2 |
Correct |
18 ms |
35540 KB |
Output is correct |
3 |
Correct |
15 ms |
35588 KB |
Output is correct |
4 |
Correct |
15 ms |
35572 KB |
Output is correct |
5 |
Correct |
15 ms |
35540 KB |
Output is correct |
6 |
Correct |
17 ms |
35540 KB |
Output is correct |
7 |
Correct |
16 ms |
35540 KB |
Output is correct |
8 |
Correct |
16 ms |
35540 KB |
Output is correct |
9 |
Correct |
14 ms |
35648 KB |
Output is correct |
10 |
Correct |
15 ms |
35556 KB |
Output is correct |
11 |
Correct |
14 ms |
35592 KB |
Output is correct |
12 |
Correct |
17 ms |
35540 KB |
Output is correct |
13 |
Correct |
16 ms |
35620 KB |
Output is correct |
14 |
Correct |
15 ms |
35604 KB |
Output is correct |
15 |
Correct |
17 ms |
35540 KB |
Output is correct |
16 |
Correct |
15 ms |
35596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
35540 KB |
Output is correct |
2 |
Correct |
18 ms |
35540 KB |
Output is correct |
3 |
Correct |
15 ms |
35588 KB |
Output is correct |
4 |
Correct |
15 ms |
35572 KB |
Output is correct |
5 |
Correct |
15 ms |
35540 KB |
Output is correct |
6 |
Correct |
17 ms |
35540 KB |
Output is correct |
7 |
Correct |
16 ms |
35540 KB |
Output is correct |
8 |
Correct |
16 ms |
35540 KB |
Output is correct |
9 |
Correct |
14 ms |
35648 KB |
Output is correct |
10 |
Correct |
15 ms |
35556 KB |
Output is correct |
11 |
Correct |
14 ms |
35592 KB |
Output is correct |
12 |
Correct |
17 ms |
35540 KB |
Output is correct |
13 |
Correct |
16 ms |
35620 KB |
Output is correct |
14 |
Correct |
15 ms |
35604 KB |
Output is correct |
15 |
Correct |
17 ms |
35540 KB |
Output is correct |
16 |
Correct |
15 ms |
35596 KB |
Output is correct |
17 |
Correct |
14 ms |
35540 KB |
Output is correct |
18 |
Correct |
16 ms |
35632 KB |
Output is correct |
19 |
Correct |
15 ms |
35668 KB |
Output is correct |
20 |
Correct |
16 ms |
35592 KB |
Output is correct |
21 |
Correct |
16 ms |
35600 KB |
Output is correct |
22 |
Correct |
15 ms |
35668 KB |
Output is correct |
23 |
Correct |
16 ms |
35668 KB |
Output is correct |
24 |
Correct |
15 ms |
35624 KB |
Output is correct |
25 |
Correct |
15 ms |
35540 KB |
Output is correct |
26 |
Correct |
16 ms |
35540 KB |
Output is correct |
27 |
Correct |
15 ms |
35592 KB |
Output is correct |
28 |
Correct |
17 ms |
35668 KB |
Output is correct |
29 |
Correct |
15 ms |
35608 KB |
Output is correct |
30 |
Correct |
15 ms |
35560 KB |
Output is correct |
31 |
Correct |
17 ms |
35668 KB |
Output is correct |
32 |
Correct |
15 ms |
35588 KB |
Output is correct |
33 |
Correct |
15 ms |
35668 KB |
Output is correct |
34 |
Correct |
16 ms |
35568 KB |
Output is correct |
35 |
Correct |
14 ms |
35656 KB |
Output is correct |
36 |
Correct |
14 ms |
35668 KB |
Output is correct |
37 |
Correct |
15 ms |
35592 KB |
Output is correct |
38 |
Correct |
16 ms |
35556 KB |
Output is correct |
39 |
Correct |
15 ms |
35624 KB |
Output is correct |
40 |
Correct |
15 ms |
35668 KB |
Output is correct |
41 |
Correct |
16 ms |
35668 KB |
Output is correct |
42 |
Correct |
15 ms |
35612 KB |
Output is correct |
43 |
Correct |
16 ms |
35668 KB |
Output is correct |
44 |
Correct |
15 ms |
35668 KB |
Output is correct |
45 |
Correct |
15 ms |
35668 KB |
Output is correct |
46 |
Correct |
15 ms |
35668 KB |
Output is correct |
47 |
Correct |
15 ms |
35668 KB |
Output is correct |
48 |
Correct |
15 ms |
35608 KB |
Output is correct |
49 |
Correct |
15 ms |
35668 KB |
Output is correct |
50 |
Correct |
15 ms |
35668 KB |
Output is correct |
51 |
Correct |
16 ms |
35584 KB |
Output is correct |
52 |
Correct |
17 ms |
35644 KB |
Output is correct |
53 |
Correct |
15 ms |
35668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
35540 KB |
Output is correct |
2 |
Correct |
18 ms |
35540 KB |
Output is correct |
3 |
Correct |
15 ms |
35588 KB |
Output is correct |
4 |
Correct |
15 ms |
35572 KB |
Output is correct |
5 |
Correct |
15 ms |
35540 KB |
Output is correct |
6 |
Correct |
17 ms |
35540 KB |
Output is correct |
7 |
Correct |
16 ms |
35540 KB |
Output is correct |
8 |
Correct |
16 ms |
35540 KB |
Output is correct |
9 |
Correct |
14 ms |
35648 KB |
Output is correct |
10 |
Correct |
15 ms |
35556 KB |
Output is correct |
11 |
Correct |
14 ms |
35592 KB |
Output is correct |
12 |
Correct |
17 ms |
35540 KB |
Output is correct |
13 |
Correct |
16 ms |
35620 KB |
Output is correct |
14 |
Correct |
15 ms |
35604 KB |
Output is correct |
15 |
Correct |
17 ms |
35540 KB |
Output is correct |
16 |
Correct |
15 ms |
35596 KB |
Output is correct |
17 |
Correct |
14 ms |
35540 KB |
Output is correct |
18 |
Correct |
16 ms |
35632 KB |
Output is correct |
19 |
Correct |
15 ms |
35668 KB |
Output is correct |
20 |
Correct |
16 ms |
35592 KB |
Output is correct |
21 |
Correct |
16 ms |
35600 KB |
Output is correct |
22 |
Correct |
15 ms |
35668 KB |
Output is correct |
23 |
Correct |
16 ms |
35668 KB |
Output is correct |
24 |
Correct |
15 ms |
35624 KB |
Output is correct |
25 |
Correct |
15 ms |
35540 KB |
Output is correct |
26 |
Correct |
16 ms |
35540 KB |
Output is correct |
27 |
Correct |
15 ms |
35592 KB |
Output is correct |
28 |
Correct |
17 ms |
35668 KB |
Output is correct |
29 |
Correct |
15 ms |
35608 KB |
Output is correct |
30 |
Correct |
15 ms |
35560 KB |
Output is correct |
31 |
Correct |
17 ms |
35668 KB |
Output is correct |
32 |
Correct |
15 ms |
35588 KB |
Output is correct |
33 |
Correct |
15 ms |
35668 KB |
Output is correct |
34 |
Correct |
16 ms |
35568 KB |
Output is correct |
35 |
Correct |
14 ms |
35656 KB |
Output is correct |
36 |
Correct |
14 ms |
35668 KB |
Output is correct |
37 |
Correct |
15 ms |
35592 KB |
Output is correct |
38 |
Correct |
16 ms |
35556 KB |
Output is correct |
39 |
Correct |
15 ms |
35624 KB |
Output is correct |
40 |
Correct |
15 ms |
35668 KB |
Output is correct |
41 |
Correct |
16 ms |
35668 KB |
Output is correct |
42 |
Correct |
15 ms |
35612 KB |
Output is correct |
43 |
Correct |
16 ms |
35668 KB |
Output is correct |
44 |
Correct |
15 ms |
35668 KB |
Output is correct |
45 |
Correct |
15 ms |
35668 KB |
Output is correct |
46 |
Correct |
15 ms |
35668 KB |
Output is correct |
47 |
Correct |
15 ms |
35668 KB |
Output is correct |
48 |
Correct |
15 ms |
35608 KB |
Output is correct |
49 |
Correct |
15 ms |
35668 KB |
Output is correct |
50 |
Correct |
15 ms |
35668 KB |
Output is correct |
51 |
Correct |
16 ms |
35584 KB |
Output is correct |
52 |
Correct |
17 ms |
35644 KB |
Output is correct |
53 |
Correct |
15 ms |
35668 KB |
Output is correct |
54 |
Correct |
15 ms |
35584 KB |
Output is correct |
55 |
Correct |
15 ms |
35668 KB |
Output is correct |
56 |
Correct |
15 ms |
35924 KB |
Output is correct |
57 |
Correct |
17 ms |
35868 KB |
Output is correct |
58 |
Correct |
44 ms |
43264 KB |
Output is correct |
59 |
Correct |
299 ms |
98564 KB |
Output is correct |
60 |
Correct |
296 ms |
98420 KB |
Output is correct |
61 |
Correct |
291 ms |
98260 KB |
Output is correct |
62 |
Correct |
15 ms |
35668 KB |
Output is correct |
63 |
Correct |
289 ms |
96380 KB |
Output is correct |
64 |
Runtime error |
262 ms |
262144 KB |
Execution killed with signal 9 |
65 |
Halted |
0 ms |
0 KB |
- |