結果

問題 No.1647 Travel in Mitaru city 2
ユーザー kimiyukikimiyuki
提出日時 2021-08-21 08:24:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 116 ms / 2,500 ms
コード長 2,467 bytes
コンパイル時間 2,609 ms
コンパイル使用メモリ 203,900 KB
実行使用メモリ 21,636 KB
最終ジャッジ日時 2024-04-22 18:04:35
合計ジャッジ時間 8,786 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 66 ms
19,740 KB
testcase_09 AC 63 ms
17,924 KB
testcase_10 AC 116 ms
19,768 KB
testcase_11 AC 50 ms
12,928 KB
testcase_12 AC 71 ms
18,424 KB
testcase_13 AC 71 ms
19,024 KB
testcase_14 AC 83 ms
20,324 KB
testcase_15 AC 73 ms
20,004 KB
testcase_16 AC 61 ms
19,088 KB
testcase_17 AC 61 ms
18,672 KB
testcase_18 AC 58 ms
17,276 KB
testcase_19 AC 64 ms
19,972 KB
testcase_20 AC 73 ms
19,284 KB
testcase_21 AC 69 ms
20,096 KB
testcase_22 AC 89 ms
20,732 KB
testcase_23 AC 80 ms
20,992 KB
testcase_24 AC 68 ms
16,460 KB
testcase_25 AC 64 ms
14,720 KB
testcase_26 AC 81 ms
20,864 KB
testcase_27 AC 85 ms
20,992 KB
testcase_28 AC 87 ms
21,632 KB
testcase_29 AC 86 ms
21,628 KB
testcase_30 AC 80 ms
19,580 KB
testcase_31 AC 88 ms
21,632 KB
testcase_32 AC 69 ms
17,920 KB
testcase_33 AC 57 ms
17,444 KB
testcase_34 AC 79 ms
21,632 KB
testcase_35 AC 72 ms
19,452 KB
testcase_36 AC 78 ms
21,636 KB
testcase_37 AC 80 ms
21,632 KB
testcase_38 AC 74 ms
17,536 KB
testcase_39 AC 61 ms
16,212 KB
testcase_40 AC 62 ms
17,280 KB
testcase_41 AC 55 ms
16,128 KB
testcase_42 AC 60 ms
17,536 KB
testcase_43 AC 2 ms
5,376 KB
testcase_44 AC 6 ms
7,936 KB
testcase_45 AC 51 ms
12,544 KB
testcase_46 AC 59 ms
12,672 KB
testcase_47 AC 46 ms
12,544 KB
testcase_48 AC 69 ms
12,544 KB
testcase_49 AC 58 ms
12,544 KB
testcase_50 AC 50 ms
12,544 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++(i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++(i))
#define REP_R(i, n) for (int i = (int)(n)-1; (i) >= 0; --(i))
#define REP3R(i, m, n) for (int i = (int)(n)-1; (i) >= (int)(m); --(i))
#define ALL(x) ::std::begin(x), ::std::end(x)
using namespace std;

std::vector<int> solve(int h, int w, int n, const std::vector<int> &x, const std::vector<int> &y) {
    vector<vector<int>> row(h);
    vector<vector<int>> col(w);
    REP (i, n) {
        row[y[i]].push_back(i);
        col[x[i]].push_back(i);
    }

    vector<bool> used(n);
    vector<int> ans;
    auto go = [&](auto &&go, int i, int parent) -> bool {
        if (used[i]) {
            auto it = find(ALL(ans), i);
            if (it != ans.end() and (ans.end() - it) % 2 == 0) {
                int k = it - ans.begin();
                ans.erase(ans.begin(), it);
                if (k % 2 == 1) {
                    rotate(ans.begin(), ans.begin() + 1, ans.end());
                }
                return true;
            }
            return false;
        }
        ans.push_back(i);
        used[i] = true;
        if (ans.size() % 2 == 0) {
            for (int j : row[y[i]]) {
                if (j != i and j != parent) {
                    if (go(go, j, i)) {
                        return true;
                    }
                }
            }
        } else {
            for (int j : col[x[i]]) {
                if (j != i and j != parent) {
                    if (go(go, j, i)) {
                        return true;
                    }
                }
            }
        }
        ans.pop_back();
        return false;
    };
    REP (i, n) {
        if (not used[i]) {
            if (go(go, i, -1)) {
                return ans;
            }
        }
    }
    return ans;
}

// generated by oj-template v4.8.0 (https://github.com/online-judge-tools/template-generator)
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int H, W;
    int N;
    std::cin >> H >> W >> N;
    std::vector<int> x(N), y(N);
    REP (i, N) { std::cin >> y[i] >> x[i]; -- x[i]; -- y[i]; }
    vector<int> ans = solve(H, W, N, x, y);
    if (ans.empty()) {
        std::cout << -1 << '\n';
    } else {
        std::cout << (int)ans.size() << '\n';
        REP (i, (int)ans.size()) { std::cout << ans[i] + 1 << ' '; }
        std::cout << '\n';
    }
    return 0;
}
0