結果

問題 No.1647 Travel in Mitaru city 2
ユーザー parenthesesparentheses
提出日時 2021-08-13 22:46:33
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 173 ms / 2,500 ms
コード長 6,473 bytes
コンパイル時間 2,425 ms
コンパイル使用メモリ 212,664 KB
実行使用メモリ 35,528 KB
最終ジャッジ日時 2024-04-21 04:55:56
合計ジャッジ時間 11,452 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
7,936 KB
testcase_01 AC 8 ms
7,936 KB
testcase_02 AC 5 ms
7,936 KB
testcase_03 AC 5 ms
8,064 KB
testcase_04 AC 6 ms
8,192 KB
testcase_05 AC 5 ms
7,936 KB
testcase_06 AC 5 ms
7,936 KB
testcase_07 AC 7 ms
8,320 KB
testcase_08 AC 150 ms
32,524 KB
testcase_09 AC 145 ms
29,328 KB
testcase_10 AC 156 ms
32,872 KB
testcase_11 AC 105 ms
23,552 KB
testcase_12 AC 122 ms
22,912 KB
testcase_13 AC 109 ms
24,704 KB
testcase_14 AC 152 ms
33,376 KB
testcase_15 AC 154 ms
33,168 KB
testcase_16 AC 141 ms
31,204 KB
testcase_17 AC 113 ms
25,472 KB
testcase_18 AC 125 ms
23,808 KB
testcase_19 AC 126 ms
32,000 KB
testcase_20 AC 132 ms
28,928 KB
testcase_21 AC 139 ms
32,896 KB
testcase_22 AC 154 ms
33,992 KB
testcase_23 AC 168 ms
34,264 KB
testcase_24 AC 135 ms
29,696 KB
testcase_25 AC 155 ms
31,576 KB
testcase_26 AC 160 ms
34,380 KB
testcase_27 AC 171 ms
34,656 KB
testcase_28 AC 168 ms
35,524 KB
testcase_29 AC 173 ms
35,520 KB
testcase_30 AC 165 ms
32,624 KB
testcase_31 AC 171 ms
35,528 KB
testcase_32 AC 156 ms
34,304 KB
testcase_33 AC 148 ms
30,172 KB
testcase_34 AC 165 ms
35,396 KB
testcase_35 AC 154 ms
31,360 KB
testcase_36 AC 170 ms
35,520 KB
testcase_37 AC 172 ms
35,520 KB
testcase_38 AC 142 ms
28,704 KB
testcase_39 AC 124 ms
24,704 KB
testcase_40 AC 143 ms
28,060 KB
testcase_41 AC 123 ms
25,088 KB
testcase_42 AC 150 ms
28,392 KB
testcase_43 AC 8 ms
7,936 KB
testcase_44 AC 5 ms
7,936 KB
testcase_45 AC 104 ms
18,432 KB
testcase_46 AC 122 ms
18,432 KB
testcase_47 AC 106 ms
18,560 KB
testcase_48 AC 110 ms
18,432 KB
testcase_49 AC 98 ms
18,432 KB
testcase_50 AC 104 ms
18,432 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
// --------------------------------------------------------
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; }
#define FOR(i,l,r) for (ll i = (l); i < (r); ++i)
#define RFOR(i,l,r) for (ll i = (r)-1; (l) <= i; --i)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define MIN(c) *min_element(ALL(c))
#define MAX(c) *max_element(ALL(c))
#define SUMLL(c) accumulate(ALL(c), 0LL)
#define COUNT(c,v) count(ALL(c),(v))
#define SZ(c) ((ll)(c).size())
#define BIT(b,i) (((b)>>(i)) & 1)
#define PCNT(b) __builtin_popcountll(b)
#define CIN(c) cin >> (c)
#define COUT(c) cout << (c) << '\n'
#define debug(x) cerr << "l." << __LINE__ << " : " << #x << " = " << (x) << '\n'
ll llceil(ll a, ll b) { return (a + b - 1) / b; }
ll bitlen(ll b) { if (b <= 0) { return 0; } return (64LL - __builtin_clzll(b)); }
string toupper(const string& S) { string T(S); REP(i,SZ(T)) T[i] = toupper(T[i]); return T; }
string tolower(const string& S) { string T(S); REP(i,SZ(T)) T[i] = tolower(T[i]); return T; }
template<class T> void cout_line(const vector<T>& ans, ll l, ll r) { for (ll i = l; i < r; i++) { if (i != l) { cout << " "; } cout << ans[i]; } cout << '\n'; }
template<class T> void debug_line(const vector<T>& ans, ll l, ll r, ll L = 0) { cerr << "l." << L << " :"; for (ll i = l; i < r; i++) { cerr << " " << ans[i]; } cerr << '\n'; }
using P = pair<ll,ll>;
using VP = vector<P>;
using VVP = vector<VP>;
using VS = vector<string>;
using VVS = vector<VS>;
using VLL = vector<ll>;
using VVLL = vector<VLL>;
using VVVLL = vector<VVLL>;
using VB = vector<bool>;
using VVB = vector<VB>;
using VVVB = vector<VVB>;
using VD = vector<double>;
using VVD = vector<VD>;
using VVVD = vector<VVD>;
using VLD = vector<ld>;
using VVLD = vector<VLD>;
using VVVLD = vector<VVLD>;
static const double EPS = 1e-10;
static const double PI  = acos(-1.0);
static const ll MOD = 1000000007;
// static const ll MOD = 998244353;
static const ll INF = (1LL << 62) - 1;  // 4611686018427387904 - 1
// --------------------------------------------------------
// #include <atcoder/all>
// using namespace atcoder;


// 無向グラフの閉路を一つ見つける
//   ※自己ループ・多重辺は想定外
//   - 閉路が存在しない場合,サイズ 0 の vector を返す
//   - 閉路が存在する場合,最初に見つけた閉路の頂点配列を返す
//     e.g.) C = {u1, u2, u3} のとき u1 -> u2 -> u3 -> u1 となっている
VLL find_cycle_undirected(const VVLL& G) {
    const ll N = (ll)G.size();
    VLL C;
    VB visited(N,false);   // 訪問済フラグ(閉路検出なので 1 回の探索で十分)
    VB visiting(N,false);  // 探索中の経路に含まれるか (deque に入っているか)
    deque<ll> d;
    bool is_cycle = false;
    REP(s,N) if (!visited[s]) {
        auto rec = [&](auto self, ll u, ll p) -> void {
            if (visited[u]) return;
            for (ll v : G[u]) if (v != p) {
                if (visiting[v]) {
                    // 閉路に無関係な頂点群を削除
                    while (v != d.front()) { d.pop_front(); }

                    // 閉路を構成する頂点群を回収
                    while (!d.empty()){ C.push_back(d.front()); d.pop_front(); }

                    is_cycle = true;
                    return;
                }
                visiting[v] = true; d.push_back(v);
                self(self, v, u);
                if (is_cycle) return;
                visiting[v] = false; d.pop_back();
                visited[v] = true;
            }
        };
        visiting[s] = true; d.push_back(s);
        rec(rec, s, -1);
        if (is_cycle) return C;
        visiting[s] = false; d.pop_back();
        visited[s] = true;
    }
    return C;  // 閉路が存在しなかったので空
}


int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    ll H, W, N; cin >> H >> W >> N;

    ll M = 1e5;
    VVLL G(2*M);
    map<P,ll> mp;
    REP(i,N) {
        ll h, w; cin >> h >> w;
        h--; w--;
        ll u = h, v = M + w;
        G[u].push_back(v);
        G[v].push_back(u);
        mp[P(h,w)] = i+1;
    }

    VLL C = find_cycle_undirected(G);
    if (SZ(C) == 0) { COUT(-1); return 0; }

    ll L = SZ(C);
    VLL ans(L);
    ll s = (C[0] < M ? 0 : 1);
    REP(u,L) {
        ll x = C[(u+s)%L];
        ll y = C[(u+s+1)%L];
        ll h = -1, w = -1;
        if (M <= x) {
            h = y; w = x - M;
        } else {
            h = x; w = y - M;
        }
        ans[u] = mp[P(h,w)];
    }
    COUT(L);
    cout_line(ans,0,L);

    return 0;
}


// ----- 考察フレームワーク -----
//
// ■ 考察の準備
//   □ 制約
//     - N
//   □ 許容される計算量の例
//     - O()
//   □ 直感
//     - 
//   □ 愚直解
//     - 
//
// ■ 考察メモ
//    ・
//    ・
//    ・
//    
//
// ■ 解法案
//   (1) 
//   (2) 
//   (3) 
//
// -----------------------------
// ■ 考察に詰まった時のチェックリスト
//   - 考察ミス
//     - [ ] 誤読や見落としはないか
//     - [ ] サンプルを正しく理解したか
//     - [ ] 仮定に思い込みはないか
//   - 考察不足
//     - [ ] 愚直解を考えたか
//     - [ ] 数式を用いた考察をしたか
//     - [ ] 探索の利用を検討したか
//   - 以上に問題がない場合
//     - [ ] 典型考察ガチャ
//     - [ ] 解法ガチャ (二分探索・DP・ダブリングなど)
//     - [ ] 実験・テストケース作成
//
// ■ 典型考察 (条件の言い換え)
//   - 逆から考える (+ 中央/両側)
//   - 主客転倒する
//   - 独立に考える (x,y・bit)
//   - プロットする (二次元データなど)
//   - 余事象を考える
//   - 同一視する (DP の状態など)
//   - 単純化する (≒貰える仮定は貰う)
//   - 相対化する (変化量に着目する)
//   - 単調性を考える (二分探索・尺取り)
//   - 周期性を考える (mod など)
//   - 不変量を考える (ゲーム・貪欲法)
//   - パリティを考える (グリッドなど)
0