#include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define ll long long
#define f first
#define s second
#define FOR(i,a,b) for (int i = a; i<b; i++)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) for (int i = n-1; i>=0; i--)
#define SZ(x) (int)(x.size())
#define ALL(x) x.begin(),x.end()
#define MX(a,b) a = max(a,(__typeof__(a))(b))
#define MN(a,b) a = min(a,(__typeof__(a))(b))
#define pb push_back
#ifdef BALBIT
#define IOS()
#define bug(x) cerr<<__LINE__<<' '<<#x<<": "<<x<<endl
#else
#define IOS() ios::sync_with_stdio(0),cin.tie(0)
#define endl '\n'
#define bug(x)
#endif
const ll mod = 1e9+7;
const int maxn = 3e3+5;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int iinf = 0x3f3f3f3f;
struct Dinic{
struct Edge{
int to, rev; ll cap; ll flow = 0;
Edge(int to, int rev, ll cap) : to(to),rev(rev),cap(cap){
}
};
int n , S, T;
vector<vector<Edge> > g;
vector<int> level;
vector<int> ptr;
Dinic(int n, int S, int T) : n(n), S(S), T(T){
g.resize(n); level.resize(n); ptr.resize(n);
}
void add(int v, int u, ll cap){
g[v].pb({u,SZ(g[u]), cap});
g[u].pb({v,SZ(g[v])-1, 0});
}
bool bfs(){
fill(ALL(level), -1);
level[S]=0;
queue<int> q ({S});
while (level[T]==-1 && !q.empty() ){
int v = q.front(); q.pop();
for (Edge &e : g[v]){
if(e.cap-e.flow > 0 && level[e.to] == -1){
level[e.to] = level[v]+1;
q.push(e.to);
}
}
}
return level[T]!=-1;
}
ll dfs(int v, ll amt){
if(amt == 0 || v== T) return amt;
for(; ptr[v] < SZ(g[v]); ptr[v]++){
Edge &e = g[v][ptr[v]];
if (level[e.to]!=level[v]+1) continue;
ll mo = dfs(e.to, min(amt,e.cap-e.flow));
if (mo !=0 ) {
e.flow += mo;
g[e.to][e.rev].flow -= mo;
return mo;
}
}
return 0;
}
ll mf(){
ll re = 0;
while (bfs()){
fill(ALL(ptr),0);
while(ll amt = dfs(S,INF)) {
re += amt;
}
}
return re;
}
};
char grd[maxn][maxn];
bool up[maxn][maxn]; // Can this g go up?
bool rt[maxn][maxn]; // Can this g go right?
int idup[maxn][maxn];
int idrt[maxn][maxn];
int id(int r, int c){
}
signed main(){
IOS();
int n, m; cin>>n>>m;
REP(i,n) REP(j,m) cin>>grd[i][j];
int nup = 0, nrt = 0;
REP(i,n)REP(j,m){
if(grd[i][j]=='G'){
up[i][j] = (i && i!=n-1 && grd[i-1][j] == 'R' && grd[i+1][j]=='W' );
rt[i][j] = (j && j!=m-1 && grd[i][j-1] == 'R' && grd[i][j+1]=='W' );
idup[i][j] = nup; idrt[i][j] = nrt;
nup += up[i][j];
nrt += rt[i][j];
}
}
int N = nup + nrt + 5; int S = N-1, T = N-2;
Dinic dd (N,S,T);
REP(i,n) REP(j,m){
if(up[i][j]){
// cout<<i<<' '<<j<<' '<<idup[i][j]<<endl;
dd.add(S,idup[i][j],1);
if(rt[i][j] ) {
dd.add(idup[i][j], idrt[i][j] + nup, iinf);
}
if(i && rt[i-1][j+1]){
dd.add(idup[i][j], idrt[i-1][j+1] + nup, iinf);
}
if(j && rt[i+1][j-1]){
dd.add(idup[i][j], idrt[i+1][j-1] + nup, iinf);
}
}
if(rt[i][j]){
// cout<<"rt: "<<i<<' '<<j<<' '<<idrt[i][j]<<endl;
dd.add(idrt[i][j] + nup, T, 1);
}
}
// bug(nup + nrt);
cout<<nup + nrt - dd.mf() << endl;
}
Compilation message
dango_maker.cpp: In function 'int id(int, int)':
dango_maker.cpp:101:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
508 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
380 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
508 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
380 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
504 KB |
Output is correct |
20 |
Correct |
2 ms |
504 KB |
Output is correct |
21 |
Correct |
2 ms |
504 KB |
Output is correct |
22 |
Correct |
2 ms |
516 KB |
Output is correct |
23 |
Correct |
2 ms |
504 KB |
Output is correct |
24 |
Correct |
2 ms |
504 KB |
Output is correct |
25 |
Correct |
2 ms |
504 KB |
Output is correct |
26 |
Correct |
3 ms |
376 KB |
Output is correct |
27 |
Correct |
2 ms |
504 KB |
Output is correct |
28 |
Correct |
2 ms |
504 KB |
Output is correct |
29 |
Correct |
2 ms |
504 KB |
Output is correct |
30 |
Correct |
2 ms |
376 KB |
Output is correct |
31 |
Correct |
2 ms |
504 KB |
Output is correct |
32 |
Correct |
2 ms |
504 KB |
Output is correct |
33 |
Correct |
2 ms |
504 KB |
Output is correct |
34 |
Correct |
2 ms |
504 KB |
Output is correct |
35 |
Correct |
2 ms |
504 KB |
Output is correct |
36 |
Correct |
2 ms |
504 KB |
Output is correct |
37 |
Correct |
2 ms |
504 KB |
Output is correct |
38 |
Correct |
3 ms |
504 KB |
Output is correct |
39 |
Correct |
2 ms |
504 KB |
Output is correct |
40 |
Correct |
2 ms |
504 KB |
Output is correct |
41 |
Correct |
2 ms |
504 KB |
Output is correct |
42 |
Correct |
2 ms |
504 KB |
Output is correct |
43 |
Correct |
2 ms |
504 KB |
Output is correct |
44 |
Correct |
2 ms |
504 KB |
Output is correct |
45 |
Correct |
2 ms |
504 KB |
Output is correct |
46 |
Correct |
2 ms |
504 KB |
Output is correct |
47 |
Correct |
2 ms |
504 KB |
Output is correct |
48 |
Correct |
2 ms |
504 KB |
Output is correct |
49 |
Correct |
2 ms |
504 KB |
Output is correct |
50 |
Correct |
2 ms |
504 KB |
Output is correct |
51 |
Correct |
2 ms |
504 KB |
Output is correct |
52 |
Correct |
2 ms |
504 KB |
Output is correct |
53 |
Correct |
14 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
508 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
380 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
504 KB |
Output is correct |
20 |
Correct |
2 ms |
504 KB |
Output is correct |
21 |
Correct |
2 ms |
504 KB |
Output is correct |
22 |
Correct |
2 ms |
516 KB |
Output is correct |
23 |
Correct |
2 ms |
504 KB |
Output is correct |
24 |
Correct |
2 ms |
504 KB |
Output is correct |
25 |
Correct |
2 ms |
504 KB |
Output is correct |
26 |
Correct |
3 ms |
376 KB |
Output is correct |
27 |
Correct |
2 ms |
504 KB |
Output is correct |
28 |
Correct |
2 ms |
504 KB |
Output is correct |
29 |
Correct |
2 ms |
504 KB |
Output is correct |
30 |
Correct |
2 ms |
376 KB |
Output is correct |
31 |
Correct |
2 ms |
504 KB |
Output is correct |
32 |
Correct |
2 ms |
504 KB |
Output is correct |
33 |
Correct |
2 ms |
504 KB |
Output is correct |
34 |
Correct |
2 ms |
504 KB |
Output is correct |
35 |
Correct |
2 ms |
504 KB |
Output is correct |
36 |
Correct |
2 ms |
504 KB |
Output is correct |
37 |
Correct |
2 ms |
504 KB |
Output is correct |
38 |
Correct |
3 ms |
504 KB |
Output is correct |
39 |
Correct |
2 ms |
504 KB |
Output is correct |
40 |
Correct |
2 ms |
504 KB |
Output is correct |
41 |
Correct |
2 ms |
504 KB |
Output is correct |
42 |
Correct |
2 ms |
504 KB |
Output is correct |
43 |
Correct |
2 ms |
504 KB |
Output is correct |
44 |
Correct |
2 ms |
504 KB |
Output is correct |
45 |
Correct |
2 ms |
504 KB |
Output is correct |
46 |
Correct |
2 ms |
504 KB |
Output is correct |
47 |
Correct |
2 ms |
504 KB |
Output is correct |
48 |
Correct |
2 ms |
504 KB |
Output is correct |
49 |
Correct |
2 ms |
504 KB |
Output is correct |
50 |
Correct |
2 ms |
504 KB |
Output is correct |
51 |
Correct |
2 ms |
504 KB |
Output is correct |
52 |
Correct |
2 ms |
504 KB |
Output is correct |
53 |
Correct |
14 ms |
504 KB |
Output is correct |
54 |
Correct |
2 ms |
376 KB |
Output is correct |
55 |
Correct |
25 ms |
24696 KB |
Output is correct |
56 |
Correct |
3 ms |
816 KB |
Output is correct |
57 |
Correct |
30 ms |
34168 KB |
Output is correct |
58 |
Correct |
71 ms |
34144 KB |
Output is correct |
59 |
Correct |
551 ms |
173164 KB |
Output is correct |
60 |
Correct |
556 ms |
173148 KB |
Output is correct |
61 |
Correct |
554 ms |
173128 KB |
Output is correct |
62 |
Correct |
2 ms |
504 KB |
Output is correct |
63 |
Correct |
563 ms |
170796 KB |
Output is correct |
64 |
Runtime error |
434 ms |
262144 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
65 |
Halted |
0 ms |
0 KB |
- |