結果
問題 | No.1647 Travel in Mitaru city 2 |
ユーザー |
|
提出日時 | 2021-08-21 08:07:56 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 2,411 bytes |
コンパイル時間 | 2,848 ms |
コンパイル使用メモリ | 202,324 KB |
最終ジャッジ日時 | 2025-01-24 01:12:47 |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 31 WA * 4 RE * 13 |
ソースコード
#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;}