結果

問題 No.1647 Travel in Mitaru city 2
ユーザー KudeKude
提出日時 2021-08-13 22:31:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 69 ms / 2,500 ms
コード長 1,745 bytes
コンパイル時間 4,417 ms
コンパイル使用メモリ 259,628 KB
実行使用メモリ 25,868 KB
最終ジャッジ日時 2024-04-21 04:54:33
合計ジャッジ時間 9,788 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 3 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 63 ms
23,648 KB
testcase_09 AC 54 ms
21,068 KB
testcase_10 AC 61 ms
23,656 KB
testcase_11 AC 46 ms
17,056 KB
testcase_12 AC 49 ms
15,692 KB
testcase_13 AC 48 ms
17,660 KB
testcase_14 AC 66 ms
24,208 KB
testcase_15 AC 60 ms
23,884 KB
testcase_16 AC 57 ms
22,772 KB
testcase_17 AC 44 ms
18,240 KB
testcase_18 AC 47 ms
16,800 KB
testcase_19 AC 61 ms
23,416 KB
testcase_20 AC 52 ms
21,000 KB
testcase_21 AC 58 ms
24,432 KB
testcase_22 AC 67 ms
24,736 KB
testcase_23 AC 66 ms
24,840 KB
testcase_24 AC 55 ms
21,756 KB
testcase_25 AC 58 ms
22,792 KB
testcase_26 AC 60 ms
24,840 KB
testcase_27 AC 65 ms
24,936 KB
testcase_28 AC 65 ms
25,736 KB
testcase_29 AC 65 ms
25,864 KB
testcase_30 AC 60 ms
23,688 KB
testcase_31 AC 69 ms
25,736 KB
testcase_32 AC 63 ms
25,352 KB
testcase_33 AC 55 ms
22,028 KB
testcase_34 AC 63 ms
25,868 KB
testcase_35 AC 56 ms
22,668 KB
testcase_36 AC 65 ms
25,736 KB
testcase_37 AC 69 ms
25,864 KB
testcase_38 AC 58 ms
20,480 KB
testcase_39 AC 51 ms
17,816 KB
testcase_40 AC 62 ms
20,232 KB
testcase_41 AC 55 ms
17,956 KB
testcase_42 AC 67 ms
20,224 KB
testcase_43 AC 2 ms
5,376 KB
testcase_44 AC 5 ms
8,328 KB
testcase_45 AC 42 ms
12,812 KB
testcase_46 AC 61 ms
12,416 KB
testcase_47 AC 41 ms
12,748 KB
testcase_48 AC 51 ms
12,784 KB
testcase_49 AC 43 ms
12,508 KB
testcase_50 AC 42 ms
12,672 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;
#define rep(i,n)for (int i = 0; i < int(n); ++i)
#define rrep(i,n)for (int i = int(n)-1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template<class T> void chmax(T& a, const T& b) {a = max(a, b);}
template<class T> void chmin(T& a, const T& b) {a = min(a, b);}
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int h, w, n;
    cin >> h >> w >> n;
    vector<vector<P>> to(h + w);
    rep(i, n) {
        int x, y;
        cin >> x >> y;
        x--, y--;
        to[x].emplace_back(h + y, i);
        to[h + y].emplace_back(x, i);
    }
    vector<char> visited(h + w);
    vector<P> path;
    vector<char> used(n);
    auto dfs = [&](auto&& self, int u, int p=-1) -> void {
        if (visited[u]) {
            VI ans;
            ans.push_back(p);
            while(path.back().first != u) {
                ans.push_back(path.back().second);
                path.pop_back();
            }
            if (u >= h) {
                rotate(ans.begin(), ans.begin() + 1, ans.end());
            }
            int sz = ans.size();
            cout << sz << '\n';
            rep(i, sz) cout << ans[i] + 1 << " \n"[i + 1 == sz];
            exit(0);
        }
        visited[u] = true;
        path.emplace_back(u, p);
        for(auto [v, eid]: to[u]) if (!used[eid]) {
            used[eid] = true;
            self(self, v, eid);
        }
        path.pop_back();
    };

    rep(i, h) if (!visited[i]) dfs(dfs, i);
    cout << -1 << endl;
}
0