結果
問題 | No.1123 Afforestation |
ユーザー | e869120 |
提出日時 | 2020-07-03 00:15:26 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 683 ms / 2,500 ms |
コード長 | 3,863 bytes |
コンパイル時間 | 1,138 ms |
コンパイル使用メモリ | 86,960 KB |
実行使用メモリ | 124,672 KB |
最終ジャッジ日時 | 2024-09-16 17:04:52 |
合計ジャッジ時間 | 14,556 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 3 ms
5,376 KB |
testcase_04 | AC | 12 ms
9,344 KB |
testcase_05 | AC | 90 ms
38,272 KB |
testcase_06 | AC | 531 ms
124,032 KB |
testcase_07 | AC | 2 ms
5,376 KB |
testcase_08 | AC | 2 ms
5,376 KB |
testcase_09 | AC | 2 ms
5,376 KB |
testcase_10 | AC | 3 ms
5,376 KB |
testcase_11 | AC | 7 ms
6,400 KB |
testcase_12 | AC | 51 ms
23,040 KB |
testcase_13 | AC | 660 ms
124,160 KB |
testcase_14 | AC | 2 ms
5,376 KB |
testcase_15 | AC | 2 ms
5,376 KB |
testcase_16 | AC | 2 ms
5,376 KB |
testcase_17 | AC | 2 ms
5,376 KB |
testcase_18 | AC | 4 ms
5,376 KB |
testcase_19 | AC | 15 ms
9,472 KB |
testcase_20 | AC | 146 ms
38,016 KB |
testcase_21 | AC | 668 ms
124,288 KB |
testcase_22 | AC | 660 ms
124,160 KB |
testcase_23 | AC | 683 ms
124,416 KB |
testcase_24 | AC | 659 ms
124,544 KB |
testcase_25 | AC | 642 ms
124,288 KB |
testcase_26 | AC | 3 ms
5,376 KB |
testcase_27 | AC | 2 ms
5,376 KB |
testcase_28 | AC | 76 ms
8,448 KB |
testcase_29 | AC | 2 ms
5,376 KB |
testcase_30 | AC | 2 ms
5,376 KB |
testcase_31 | AC | 76 ms
8,448 KB |
testcase_32 | AC | 2 ms
5,376 KB |
testcase_33 | AC | 2 ms
5,376 KB |
testcase_34 | AC | 3 ms
5,376 KB |
testcase_35 | AC | 4 ms
5,376 KB |
testcase_36 | AC | 3 ms
5,376 KB |
testcase_37 | AC | 3 ms
5,376 KB |
testcase_38 | AC | 2 ms
5,376 KB |
testcase_39 | AC | 5 ms
5,376 KB |
testcase_40 | AC | 5 ms
5,376 KB |
testcase_41 | AC | 4 ms
5,376 KB |
testcase_42 | AC | 9 ms
6,400 KB |
testcase_43 | AC | 20 ms
9,472 KB |
testcase_44 | AC | 38 ms
12,544 KB |
testcase_45 | AC | 37 ms
12,672 KB |
testcase_46 | AC | 41 ms
13,824 KB |
testcase_47 | AC | 41 ms
13,824 KB |
testcase_48 | AC | 3 ms
5,376 KB |
testcase_49 | AC | 2 ms
5,376 KB |
testcase_50 | AC | 4 ms
5,376 KB |
testcase_51 | AC | 4 ms
5,376 KB |
testcase_52 | AC | 3 ms
5,376 KB |
testcase_53 | AC | 2 ms
5,376 KB |
testcase_54 | AC | 2 ms
5,376 KB |
testcase_55 | AC | 4 ms
5,376 KB |
testcase_56 | AC | 4 ms
5,376 KB |
testcase_57 | AC | 4 ms
5,376 KB |
testcase_58 | AC | 8 ms
6,400 KB |
testcase_59 | AC | 18 ms
9,472 KB |
testcase_60 | AC | 34 ms
12,800 KB |
testcase_61 | AC | 35 ms
12,800 KB |
testcase_62 | AC | 42 ms
13,824 KB |
testcase_63 | AC | 40 ms
13,824 KB |
testcase_64 | AC | 2 ms
5,376 KB |
testcase_65 | AC | 2 ms
5,376 KB |
testcase_66 | AC | 3 ms
5,376 KB |
testcase_67 | AC | 3 ms
5,376 KB |
testcase_68 | AC | 4 ms
5,376 KB |
testcase_69 | AC | 5 ms
5,632 KB |
testcase_70 | AC | 9 ms
6,400 KB |
testcase_71 | AC | 17 ms
9,600 KB |
testcase_72 | AC | 1 ms
5,376 KB |
testcase_73 | AC | 1 ms
5,376 KB |
testcase_74 | AC | 2 ms
5,376 KB |
testcase_75 | AC | 2 ms
5,376 KB |
testcase_76 | AC | 1 ms
5,376 KB |
testcase_77 | AC | 2 ms
5,376 KB |
testcase_78 | AC | 1 ms
5,376 KB |
testcase_79 | AC | 1 ms
5,376 KB |
testcase_80 | AC | 1 ms
5,376 KB |
testcase_81 | AC | 2 ms
5,376 KB |
testcase_82 | AC | 2 ms
5,376 KB |
testcase_83 | AC | 2 ms
5,376 KB |
testcase_84 | AC | 328 ms
124,544 KB |
testcase_85 | AC | 450 ms
124,672 KB |
testcase_86 | AC | 2 ms
5,376 KB |
testcase_87 | AC | 2 ms
5,376 KB |
testcase_88 | AC | 328 ms
99,968 KB |
testcase_89 | AC | 315 ms
100,224 KB |
testcase_90 | AC | 2 ms
5,376 KB |
testcase_91 | AC | 1 ms
5,376 KB |
testcase_92 | AC | 2 ms
5,376 KB |
ソースコード
#include <iostream> #include <vector> #include <cassert> #include <algorithm> using namespace std; #pragma warning (disable: 4996) struct Node { int to, cap, rev; }; class MaxFlow { public: vector<Node> G[5009]; bool used[5009]; void add_edge(int u, int v, int c, int d) { G[u].push_back(Node{ v, c - d, (int)G[v].size() }); G[v].push_back(Node{ u, d, (int)G[u].size() - 1 }); } int dfs(int pos, int to, int f) { if (pos == to) return f; used[pos] = true; for (int i = 0; i < G[pos].size(); i++) { if (used[G[pos][i].to] == true || G[pos][i].cap == 0) continue; int F = dfs(G[pos][i].to, to, min(f, G[pos][i].cap)); if (F == 0) continue; G[pos][i].cap -= F; G[G[pos][i].to][G[pos][i].rev].cap += F; return F; } return 0; } int max_flow(int u, int v) { int f = 0; while (true) { for (int i = 0; i < 5009; i++) used[i] = false; int g = dfs(u, v, 100000007); f += g; if (g == 0) break; } return f; } }; // 入力 int H, A[2509]; int W, B[2509]; int K, X[19], Y[19]; // その他 int Answer[2509][2509]; bool used[2509][2509]; bool forced[2509][2509]; MaxFlow G; int main() { // 入力 cin >> H >> W; for (int i = 1; i <= H; i++) cin >> A[i]; for (int i = 1; i <= W; i++) cin >> B[i]; cin >> K; for (int i = 1; i <= K; i++) cin >> X[i] >> Y[i]; // 制約チェック assert(1 <= H && H <= 2000); assert(1 <= W && W <= 2000); assert(0 <= K && K <= 16); for (int i = 1; i <= H; i++) assert(0 <= A[i] && A[i] <= W); for (int i = 1; i <= W; i++) assert(0 <= B[i] && B[i] <= H); for (int i = 1; i <= K; i++) assert(1 <= X[i] && X[i] <= H); for (int i = 1; i <= K; i++) assert(1 <= Y[i] && Y[i] <= W); for (int i = 1; i <= K; i++) { for (int j = i + 1; j <= K; j++) assert(X[i] != X[j] || Y[i] != Y[j]); } // 貪欲法 vector<pair<int, int>> CA, CB; for (int i = 1; i <= K; i++) forced[X[i]][Y[i]] = true; for (int i = 1; i <= H; i++) CA.push_back(make_pair(A[i], i)); for (int i = 1; i <= W; i++) CB.push_back(make_pair(B[i], i)); sort(CA.begin(), CA.end()); reverse(CA.begin(), CA.end()); sort(CB.begin(), CB.end()); reverse(CB.begin(), CB.end()); for (int i = 0; i < H; i++) { for (int j = 0; j < CA[i].first; j++) used[CA[i].second][CB[j].second] = true; for (int j = 0; j < CA[i].first; j++) CB[j].first -= 1; sort(CB.begin(), CB.end()); reverse(CB.begin(), CB.end()); } if (CB[0].first >= 1) { printf(":(\n"); return 0; } // 判定 int SA = 0; for (int i = 1; i <= H; i++) SA += A[i]; int SB = 0; for (int i = 1; i <= W; i++) SB += B[i]; if (SA != SB) { printf(":(\n"); return 0; } // 家の部分を消去 int cnts = 0; for (int i = 1; i <= K; i++) { if (used[X[i]][Y[i]] == false) continue; used[X[i]][Y[i]] = false; cnts++; } // 辺を貼る for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) { if (forced[i][j] == true) continue; G.add_edge(i, H + j, 1, (int)used[i][j]); } } for (int i = 1; i <= H; i++) { int cnts = 0; for (int j = 1; j <= W; j++) cnts += (int)used[i][j]; G.add_edge(H + W + 1, i, A[i], cnts); } for (int i = 1; i <= W; i++) { int cnts = 0; for (int j = 1; j <= H; j++) cnts += used[j][i]; G.add_edge(H + i, H + W + 2, B[i], cnts); } // フローを流す int Ret = G.max_flow(H + W + 1, H + W + 2); if (Ret != cnts) { printf(":(\n"); return 0; } // 復元する for (int i = 1; i <= H; i++) { for (int j = 0; j < G.G[i].size(); j++) { int pos = G.G[i][j].to; if (pos <= H || pos >= H + W + 1) continue; if (G.G[i][j].cap == 0) Answer[i][pos - H] = 1; } } for (int i = 1; i <= K; i++) Answer[X[i]][Y[i]] = 2; // 出力 printf("Yay!\n"); for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) { if (Answer[i][j] == 0) printf("."); if (Answer[i][j] == 1) printf("o"); if (Answer[i][j] == 2) printf("x"); } printf("\n"); } return 0; }