結果

問題 No.1647 Travel in Mitaru city 2
ユーザー Kude
提出日時 2021-08-13 22:31:10
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 112 ms / 2,500 ms
コード長 1,745 bytes
コンパイル時間 4,184 ms
コンパイル使用メモリ 256,920 KB
最終ジャッジ日時 2025-01-23 20:10:28
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 48
権限があれば一括ダウンロードができます

ソースコード

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