結果

問題 No.1647 Travel in Mitaru city 2
ユーザー kimiyukikimiyuki
提出日時 2021-08-21 08:07:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,411 bytes
コンパイル時間 2,465 ms
コンパイル使用メモリ 205,812 KB
実行使用メモリ 21,760 KB
最終ジャッジ日時 2024-04-22 17:49:34
合計ジャッジ時間 12,429 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 WA -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 WA -
testcase_17 RE -
testcase_18 AC 57 ms
17,408 KB
testcase_19 AC 59 ms
19,840 KB
testcase_20 AC 60 ms
19,200 KB
testcase_21 AC 60 ms
20,096 KB
testcase_22 AC 66 ms
20,864 KB
testcase_23 AC 66 ms
20,992 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 64 ms
20,864 KB
testcase_27 AC 66 ms
20,996 KB
testcase_28 AC 70 ms
21,760 KB
testcase_29 AC 74 ms
21,632 KB
testcase_30 AC 64 ms
19,460 KB
testcase_31 AC 67 ms
21,760 KB
testcase_32 AC 60 ms
17,920 KB
testcase_33 AC 57 ms
17,476 KB
testcase_34 AC 76 ms
21,632 KB
testcase_35 AC 63 ms
19,332 KB
testcase_36 AC 89 ms
21,628 KB
testcase_37 AC 93 ms
21,632 KB
testcase_38 AC 77 ms
17,664 KB
testcase_39 AC 69 ms
16,384 KB
testcase_40 AC 74 ms
17,408 KB
testcase_41 AC 67 ms
16,000 KB
testcase_42 AC 76 ms
17,536 KB
testcase_43 AC 2 ms
5,376 KB
testcase_44 AC 6 ms
7,808 KB
testcase_45 AC 52 ms
12,664 KB
testcase_46 AC 75 ms
12,672 KB
testcase_47 AC 55 ms
12,656 KB
testcase_48 AC 62 ms
12,544 KB
testcase_49 AC 48 ms
12,576 KB
testcase_50 AC 49 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()) {
                int k = it - ans.begin();
                ans.erase(ans.begin(), it);
                if (k % 2 == 1) {
                    rotate(ans.begin(), ans.begin() + 1, ans.end());
                }
                return true;
            }
        }
        ans.push_back(i);
        used[i] = true;
        if (ans.size() % 2 == 0) {
            for (int j : col[x[i]]) {
                if (j != i and j != parent) {
                    if (go(go, j, i)) {
                        return true;
                    }
                }
            }
        } else {
            for (int j : row[y[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 >> x[i] >> y[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