結果
問題 | No.1647 Travel in Mitaru city 2 |
ユーザー |
![]() |
提出日時 | 2021-08-13 22:45:48 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 156 ms / 2,500 ms |
コード長 | 1,902 bytes |
コンパイル時間 | 2,735 ms |
コンパイル使用メモリ | 255,748 KB |
実行使用メモリ | 34,520 KB |
最終ジャッジ日時 | 2024-10-13 03:21:36 |
合計ジャッジ時間 | 11,354 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 48 |
ソースコード
#include <bits/stdc++.h>using namespace std;using graph = vector<vector<int>>;vector<int> cycle;void dfs(const graph &to, vector<bool> &seen, int v, int p){seen.at(v) = true;for (auto &&c : to.at(v)){if (not cycle.empty()){break;}if (c == p){continue;}if (seen.at(c)){cycle.push_back(c);break;}dfs(to, seen, c, v);}if (not cycle.empty()){cycle.push_back(v);}}int main(){int h, w, n;cin >> h >> w >> n;graph to(h + w + n);for (int i = 0; i < n; i++){int x, y;cin >> x >> y;x--;y--;to.at(x).push_back(h + w + i);to.at(h + w + i).push_back(x);to.at(y + h).push_back(h + w + i);to.at(h + w + i).push_back(y + h);}vector<bool> seen(h + w + n);for (int i = 0; i < h + w + n; i++){if (seen.at(i)){continue;}dfs(to, seen, i, -1);}if (cycle.empty()){cout << -1 << endl;return 0;}vector<int> ans;int len = cycle.size(), shift = -1;for (int i = 0; i < len; i++){if (i and cycle.at(i) == cycle.at(0)){break;}if (cycle.at(i) >= h + w){ans.push_back(cycle.at(i) - h - w);}else if (shift == -1){shift = (ans.size() + (cycle.at(i) >= h)) % 2;}}rotate(ans.begin(), ans.begin() + shift, ans.end());int k = ans.size();cout << k << endl;for (int i = 0; i < k; i++){cout << ans.at(i) + 1;if (i < k - 1){cout << ' ';}else{cout << endl;}}return 0;}