답안 #682086

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
682086 2023-01-15T16:12:28 Z ethening Dango Maker (JOI18_dango_maker) C++17
33 / 100
277 ms 262144 KB
#include "bits/stdc++.h"
using namespace std;
using ll = long long;

// RGW
int n, m;
string s[3005];
int a[3005][3005], b[3005][3005];
// vector<int> va[3000005];
// int ma[3000005], mb[3000005];
// bool visitb[3000005];
int cnt, cnt2;

stack<int> clrS;

template<class T>
struct dinic {
  static constexpr T INF = numeric_limits<T>::max();
  struct edge {
    int v, r; T c;
    edge(int _v, int _r, T _c) : v{_v}, r{_r}, c{_c} {}
  };
  int n, s, t;
  vector<vector<edge>> G;
  vector<int> d, arc;
  dinic(int _n, int _s, int _t) : n{_n}, s{_s}, t{_t}, G(_n) {}

  void add_edge(int u, int v, T c) {
    G[u].emplace_back(v, (int)G[v].size(), c);
    G[v].emplace_back(u, (int)G[u].size() - 1, 0);
  }
  int bfs() {
    d.assign(n, 0), arc.assign(n, 0);
    queue<int> q;
    d[s] = 1, q.emplace(s);
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      for (auto [v, r, c] : G[u]) {
        if (!d[v] && c) {
          d[v] = d[u] + 1;
          if (v == t) return 1;
          q.emplace(v);
        }
      }
    }
    return d[t];
  }
  T dfs(int u, T f) {
    if (u == t) return f;
    T sum = 0;
    for (int& i = arc[u]; i < (int)G[u].size(); ++i) {
      auto& [v, r, c] = G[u][i];
      if (d[v] == d[u] + 1 && c) {
        T res = dfs(v, min(f, c));
        if (res) {
          c -= res, G[v][r].c += res;
          sum += res, f -= res;
          if (!f) return sum;
        } else {
          d[v] = 0;
        }
      }
    }
    return sum;
  }
  T max_flow() {
    T sum = 0;
    while (bfs()) {
      while (T res = dfs(s, INF)) {
        sum += res;
      }
    }
    return sum;
  }
};


// bool dfs(int cur) {
// 	for (int nxt : va[cur]) {
// 		if (!visitb[nxt]) {
// 			visitb[nxt] = true;
// 			clrS.push(nxt);
// 			if (mb[nxt] == 0 || dfs(mb[nxt])) {
// 				ma[cur] = nxt;
// 				mb[nxt] = cur;
// 				return true;
// 			}
// 		}
// 	}
// 	return false;
// }


// int bipartite_matching() {
// 	memset(ma, -1, sizeof(ma));
// 	memset(mb, -1, sizeof(mb));
// 	int ret = 0;
// 	for (int i = 1; i <= cnt; i++) {
// 		while (!clrS.empty()) {
// 			visitb[clrS.top()] = false;
// 			clrS.pop();
// 		}
// 		if (dfs(i)) ++ret;
// 	}
// 	return ret;
// }

int main() {
	cin.tie(0)->sync_with_stdio(0);
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		cin >> s[i];
	}
	cnt = cnt2 = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (s[i][j] != 'R') continue;
			if (j + 2 < m) {
				if (s[i][j + 1] == 'G' && s[i][j + 2] == 'W') {
					++cnt;
					a[i][j] = cnt;
					a[i][j + 1] = cnt;
					a[i][j + 2] = cnt;
				}
			}
			if (i + 2 < n) {
				if (s[i + 1][j] == 'G' && s[i + 2][j] == 'W') {
					++cnt2;
					b[i][j]= cnt2;
					b[i + 1][j]= cnt2;
					b[i + 2][j]= cnt2;
				}
			}
		}
	}

	dinic<int> D(cnt + cnt2 + 2, 0, 1);
	for (int i = 1; i <= cnt; i++) {
		D.add_edge(0, i + 1, 1);
	}
	for (int i = 1; i <= cnt2; i++) {
		D.add_edge(cnt + i + 1, 1, 1);
	}

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (a[i][j] != 0 && b[i][j] != 0) {
				D.add_edge(a[i][j] + 1, cnt + b[i][j] + 1, D.INF);
				// va[a[i][j]].push_back(b[i][j]);
			}
		}
	}

	cout << cnt + cnt2 - D.max_flow() << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 416 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 416 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 416 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 1 ms 420 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 1 ms 416 KB Output is correct
28 Correct 1 ms 424 KB Output is correct
29 Correct 0 ms 468 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 0 ms 340 KB Output is correct
32 Correct 0 ms 340 KB Output is correct
33 Correct 1 ms 420 KB Output is correct
34 Correct 1 ms 468 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 0 ms 468 KB Output is correct
37 Correct 1 ms 468 KB Output is correct
38 Correct 0 ms 468 KB Output is correct
39 Correct 1 ms 468 KB Output is correct
40 Correct 1 ms 468 KB Output is correct
41 Correct 1 ms 468 KB Output is correct
42 Correct 1 ms 468 KB Output is correct
43 Correct 1 ms 468 KB Output is correct
44 Correct 1 ms 468 KB Output is correct
45 Correct 1 ms 468 KB Output is correct
46 Correct 1 ms 468 KB Output is correct
47 Correct 1 ms 468 KB Output is correct
48 Correct 1 ms 468 KB Output is correct
49 Correct 1 ms 468 KB Output is correct
50 Correct 1 ms 468 KB Output is correct
51 Correct 1 ms 468 KB Output is correct
52 Correct 1 ms 468 KB Output is correct
53 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 416 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 416 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 1 ms 420 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 1 ms 416 KB Output is correct
28 Correct 1 ms 424 KB Output is correct
29 Correct 0 ms 468 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 0 ms 340 KB Output is correct
32 Correct 0 ms 340 KB Output is correct
33 Correct 1 ms 420 KB Output is correct
34 Correct 1 ms 468 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 0 ms 468 KB Output is correct
37 Correct 1 ms 468 KB Output is correct
38 Correct 0 ms 468 KB Output is correct
39 Correct 1 ms 468 KB Output is correct
40 Correct 1 ms 468 KB Output is correct
41 Correct 1 ms 468 KB Output is correct
42 Correct 1 ms 468 KB Output is correct
43 Correct 1 ms 468 KB Output is correct
44 Correct 1 ms 468 KB Output is correct
45 Correct 1 ms 468 KB Output is correct
46 Correct 1 ms 468 KB Output is correct
47 Correct 1 ms 468 KB Output is correct
48 Correct 1 ms 468 KB Output is correct
49 Correct 1 ms 468 KB Output is correct
50 Correct 1 ms 468 KB Output is correct
51 Correct 1 ms 468 KB Output is correct
52 Correct 1 ms 468 KB Output is correct
53 Correct 1 ms 468 KB Output is correct
54 Correct 1 ms 340 KB Output is correct
55 Correct 3 ms 1748 KB Output is correct
56 Correct 1 ms 724 KB Output is correct
57 Correct 7 ms 13396 KB Output is correct
58 Correct 32 ms 23748 KB Output is correct
59 Correct 252 ms 134000 KB Output is correct
60 Correct 277 ms 133976 KB Output is correct
61 Correct 260 ms 133832 KB Output is correct
62 Correct 1 ms 472 KB Output is correct
63 Correct 261 ms 131856 KB Output is correct
64 Runtime error 161 ms 262144 KB Execution killed with signal 9
65 Halted 0 ms 0 KB -