結果

問題 No.1647 Travel in Mitaru city 2
ユーザー みしあ
提出日時 2021-10-22 18:22:52
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,903 bytes
コンパイル時間 4,877 ms
コンパイル使用メモリ 269,396 KB
最終ジャッジ日時 2025-01-25 02:54:43
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2 WA * 1
other AC * 2 WA * 46
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma region Macros
#include <bits/stdc++.h>
#if defined(LOCAL) || defined(ONLINE_JUDGE) || defined(_DEBUG)
#include <atcoder/all>
#endif
using namespace std;
#define REP(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define REPR(i, n) for(int i=(n); i>=0; --i)
#define FOR(i, n, m) for(int i=(m), i##_len=(n); i<i##_len; ++i)
#define EACH(i, v) for(const auto& i : v)
#define ALL(x) (x).begin(),(x).end()
#define ALLR(x) (x).rbegin(),(x).rend()
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
template<class T>using vec = vector<T>;
template<class T, class U>using umap = unordered_map<T, U>;
template<class T>using uset = unordered_set<T>;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
//using T = tuple<ll, ll, ll>;
using vl = vec<ll>;
#define fi first
#define se second
#define el endl
constexpr ll INF = numeric_limits<ll>::max()/2-1;
#pragma endregion

#pragma region IOMacros
template<class T>
istream &operator>>(istream &stream, vec<T>& o){REP(i, o.size())stream >> o[i];return stream;}
template<class T>
ostream &operator<<(ostream &stream, vec<T>& objs){REP(i, objs.size())stream << objs[i] << " ";stream << el;return stream;}

#define I(T, ...) ;T __VA_ARGS__;__i(__VA_ARGS__);
void __i() {}
template<class T, class... Ts> void __i(T&& o, Ts&&... args){cin >> o;__i(forward<Ts>(args)...);}

#ifdef LOCAL
void O() {cerr << el;}
template<class T, class... Ts> void O(T&& o, Ts&&... args){cerr << o << " ";O(forward<Ts>(args)...);}
#else
template<class T, class... Ts> void O(T&& o, Ts&&... args){};
#endif
#pragma endregion

void Main();

int main(){
    std::cin.tie(nullptr);
    std::cout << std::fixed << std::setprecision(15);
    Main();
    return 0;
}

umap<ll, vl> HP, WP;
umap<ll, ll> ind;
uset<ll> seen;
ll pos(ll h, ll w){
    return h*(ll)2e5+w;
}

bool solve(ll i, ll h, ll w){
    ll p = pos(h, w);
    if(i == 0){
        cout << 4 << el;
        return true;
    }
    if(seen.count(p)) return false;
    seen.insert(p);
    if((i&1)){
        EACH(v, HP[h]){
            if(solve(i-1, h, v)){
                cout << ind[p] << (i==4?"":" ");
                return true;
            }
        }
    }else{
        EACH(v, WP[w]){
            if(solve(i-1, v, w)){
                cout << ind[p] << (i==4?"":" ");
                return true;
            }
        }
    }
    seen.erase(p);
    return false;
}

void Main(){
    I(ll, H, W, N);
    vl X(N), Y(N);
    REP(i, N){
        I(ll, h, w);
        HP[h].push_back(w);
        WP[w].push_back(h);
        ind[pos(h, w)] = i+1;
        X[i] = h;
        Y[i] = w;
    }
    bool flag = false;
    REP(i, N){
        if(solve(4, X[i], Y[i])){
            flag = true;
            break;
        }
    }
    if(!flag){
        cout << -1;
    }
    cout << el;
}
0