結果

問題 No.1647 Travel in Mitaru city 2
ユーザー parenthesesparentheses
提出日時 2021-08-13 22:41:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 6,434 bytes
コンパイル時間 2,908 ms
コンパイル使用メモリ 214,240 KB
実行使用メモリ 35,652 KB
最終ジャッジ日時 2024-04-14 19:09:39
合計ジャッジ時間 14,510 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
8,112 KB
testcase_01 AC 8 ms
7,976 KB
testcase_02 AC 4 ms
8,120 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 4 ms
7,836 KB
testcase_06 AC 5 ms
7,840 KB
testcase_07 WA -
testcase_08 AC 201 ms
32,388 KB
testcase_09 AC 180 ms
29,452 KB
testcase_10 AC 203 ms
32,864 KB
testcase_11 AC 142 ms
23,740 KB
testcase_12 AC 161 ms
22,928 KB
testcase_13 AC 154 ms
24,892 KB
testcase_14 AC 213 ms
33,640 KB
testcase_15 AC 200 ms
33,164 KB
testcase_16 AC 192 ms
31,332 KB
testcase_17 AC 147 ms
25,476 KB
testcase_18 AC 156 ms
23,688 KB
testcase_19 AC 171 ms
31,944 KB
testcase_20 AC 175 ms
29,004 KB
testcase_21 AC 190 ms
32,788 KB
testcase_22 AC 209 ms
33,996 KB
testcase_23 AC 215 ms
34,140 KB
testcase_24 AC 180 ms
29,480 KB
testcase_25 AC 193 ms
31,448 KB
testcase_26 AC 221 ms
34,256 KB
testcase_27 AC 213 ms
34,528 KB
testcase_28 AC 210 ms
35,604 KB
testcase_29 AC 210 ms
35,392 KB
testcase_30 AC 203 ms
32,496 KB
testcase_31 AC 217 ms
35,652 KB
testcase_32 AC 184 ms
34,152 KB
testcase_33 AC 191 ms
30,092 KB
testcase_34 AC 216 ms
35,524 KB
testcase_35 AC 191 ms
31,368 KB
testcase_36 AC 215 ms
35,396 KB
testcase_37 AC 220 ms
35,396 KB
testcase_38 AC 200 ms
28,444 KB
testcase_39 AC 171 ms
24,716 KB
testcase_40 AC 191 ms
28,064 KB
testcase_41 AC 181 ms
25,396 KB
testcase_42 AC 196 ms
28,268 KB
testcase_43 AC 8 ms
7,956 KB
testcase_44 AC 4 ms
7,776 KB
testcase_45 AC 142 ms
18,476 KB
testcase_46 AC 170 ms
18,276 KB
testcase_47 AC 148 ms
18,456 KB
testcase_48 AC 159 ms
18,416 KB
testcase_49 WA -
testcase_50 WA -
権限があれば一括ダウンロードができます

ソースコード

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);
    REP(u,L) {
        ll x = C[u];
        ll y = C[(u+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