結果

問題 No.1647 Travel in Mitaru city 2
ユーザー sash0sash0
提出日時 2021-08-28 22:00:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,018 bytes
コンパイル時間 3,712 ms
コンパイル使用メモリ 239,980 KB
実行使用メモリ 29,564 KB
最終ジャッジ日時 2024-05-01 16:55:18
合計ジャッジ時間 16,023 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 AC 2 ms
5,376 KB
testcase_44 AC 6 ms
7,936 KB
testcase_45 WA -
testcase_46 AC 70 ms
18,000 KB
testcase_47 WA -
testcase_48 AC 64 ms
18,008 KB
testcase_49 WA -
testcase_50 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

// clang-format off
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define mp make_pair
#define fst first
#define snd second
#define forn(i,n) for (int i = 0; i < int(n); i++)
#define forn1(i,n) for (int i = 1; i <= int(n); i++)
#define popcnt __builtin_popcountll
#define ffs __builtin_ffsll
#define ctz __builtin_ctzll
#define clz __builtin_clz
#define clzll __builtin_clzll
#define all(a) (a).begin(), (a).end()

using namespace std;
using namespace __gnu_pbds;

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int,int>;
using pli = pair<ll,int>;
using pil = pair<int,ll>;
using pll = pair<ll,ll>;
template <typename T> using vec = vector<T>;
using vi = vec<int>;
using vl = vec<ll>;
template <typename T> using que = queue<T>;
template <typename T> using deq = deque<T>;
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename K, typename V> using ordered_map = tree<K, V, less<K>, rb_tree_tag, tree_order_statistics_node_update>;

template <typename T> T id(T b) {return b;};
template <typename T> void chmax(T &x, T y) {if (x < y) x = y;}
template <typename T> void chmin(T &x, T y) {if (x > y) x = y;}
template <typename S, typename K> bool contains(S &s, K k) { return s.find(k) != s.end(); }
template <typename T> bool getf(T flag, size_t i) { return (flag>>i) & 1; }
template <typename T> T setf(T flag, size_t i) { return flag | (T(1)<<i); }
template <typename T> T unsetf(T flag, size_t i) { return flag & ~(T(1)<<i); }
void fastio() { ios_base::sync_with_stdio(false); cin.tie(nullptr); }
constexpr ll TEN(int n) { if (n == 0) return 1LL; else return 10LL*TEN(n-1); }
// clang-format on

int main() {
    fastio();

    int h, w, n;
    cin >> h >> w >> n;

    vec<vec<pii>> r(h), c(w);
    vec<pii> xy;
    forn(i, n) {
        int x, y;
        cin >> x >> y;
        x--, y--;
        r[x].emplace_back(y, i);
        c[y].emplace_back(x, i);
        xy.emplace_back(x, y);
    }

    for (auto& r : r) {
        sort(all(r));
    }

    for (auto& c : c) {
        sort(all(c));
    }

    vec<vi> g(n);

    forn(i, h) {
        for (int j = 0; j < int(r[i].size()) - 1; j++) {
            g[r[i][j].snd].push_back(r[i][j + 1].snd);
            g[r[i][j + 1].snd].push_back(r[i][j].snd);
        }
    }

    forn(i, w) {
        for (int j = 0; j < int(c[i].size()) - 1; j++) {
            g[c[i][j].snd].push_back(c[i][j + 1].snd);
            g[c[i][j + 1].snd].push_back(c[i][j].snd);
        }
    }

    vec<bool> in_path(n);
    vec<bool> vis(n);
    vi path;
    function<bool(int, int)> dfs = [&](int u, int p) {
        if (in_path[u]) {
            path.push_back(u);
            return true;
        }
        vis[u] = true;
        in_path[u] = true;
        path.push_back(u);
        for (auto v : g[u]) {
            if (v != p) {
                if (dfs(v, u)) return true;
            }
        }
        in_path[u] = false;
        path.pop_back();

        return false;
    };

    forn(i, n) {
        if (!vis[i])
            if (dfs(i, -1)) break;
    }

    if (path.empty()) {
        cout << "-1\n";
        return 0;
    }

    reverse(all(path));

    while (path.back() != path.front()) {
        path.pop_back();
    }

    auto [x, y] = xy[path[0]];
    vi ans;
    ans.push_back(path[0]);
    bool k = false;
    for (int i = 1; i < int(path.size()) - 1; i++) {
        auto [px, py] = xy[path[i - 1]];
        auto [nx, ny] = xy[path[i + 1]];
        if ((k and ny != py) or (!k and nx != px)) {
            k = not k;
            ans.push_back(path[i]);
        }

        x = nx;
        y = ny;
    }

    if (xy[ans[0]].snd != xy[ans[1]].snd) {
        auto v = ans.front();
        ans.erase(ans.begin());
        ans.push_back(v);
    }

    cout << path.size() - 1 << "\n";
    for (auto x : ans) {
        cout << x + 1 << " ";
    }
    cout << "\n";

    return 0;
}
0