結果

問題 No.1647 Travel in Mitaru city 2
ユーザー nawawannawawan
提出日時 2021-08-14 00:43:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 247 ms / 2,500 ms
コード長 2,226 bytes
コンパイル時間 2,466 ms
コンパイル使用メモリ 208,976 KB
実行使用メモリ 42,240 KB
最終ジャッジ日時 2024-04-21 05:00:00
合計ジャッジ時間 14,356 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 5 ms
5,376 KB
testcase_08 AC 212 ms
38,144 KB
testcase_09 AC 199 ms
34,044 KB
testcase_10 AC 233 ms
38,424 KB
testcase_11 AC 165 ms
27,904 KB
testcase_12 AC 175 ms
26,096 KB
testcase_13 AC 177 ms
28,920 KB
testcase_14 AC 247 ms
39,424 KB
testcase_15 AC 243 ms
38,912 KB
testcase_16 AC 197 ms
36,604 KB
testcase_17 AC 162 ms
29,824 KB
testcase_18 AC 168 ms
28,032 KB
testcase_19 AC 190 ms
39,168 KB
testcase_20 AC 193 ms
34,836 KB
testcase_21 AC 210 ms
40,264 KB
testcase_22 AC 242 ms
40,028 KB
testcase_23 AC 238 ms
40,428 KB
testcase_24 AC 185 ms
35,632 KB
testcase_25 AC 227 ms
37,360 KB
testcase_26 AC 224 ms
40,412 KB
testcase_27 AC 225 ms
40,832 KB
testcase_28 AC 212 ms
42,240 KB
testcase_29 AC 223 ms
42,200 KB
testcase_30 AC 203 ms
38,912 KB
testcase_31 AC 225 ms
42,152 KB
testcase_32 AC 191 ms
41,984 KB
testcase_33 AC 182 ms
36,412 KB
testcase_34 AC 221 ms
42,112 KB
testcase_35 AC 192 ms
36,992 KB
testcase_36 AC 208 ms
42,240 KB
testcase_37 AC 217 ms
42,112 KB
testcase_38 AC 190 ms
32,896 KB
testcase_39 AC 163 ms
28,572 KB
testcase_40 AC 183 ms
32,256 KB
testcase_41 AC 169 ms
28,824 KB
testcase_42 AC 176 ms
32,696 KB
testcase_43 AC 2 ms
5,376 KB
testcase_44 AC 7 ms
9,564 KB
testcase_45 AC 150 ms
20,524 KB
testcase_46 AC 167 ms
20,528 KB
testcase_47 AC 151 ms
20,308 KB
testcase_48 AC 166 ms
20,608 KB
testcase_49 AC 188 ms
20,536 KB
testcase_50 AC 186 ms
20,444 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int main(){
    int H, W, N;
    cin >> H >> W >> N;
    vector<int> x(N), y(N);
    vector<vector<int>> G(H + W);
    map<pair<int, int>, int> m;
    for(int i = 0; i < N; i++) {
        cin >> x[i] >> y[i];
        x[i]--;
        y[i]--;
        G[x[i]].push_back(H + y[i]);
        G[H + y[i]].push_back(x[i]);
        m[{x[i], H + y[i]}] = i;
    }
    vector<int> used(H + W);
    vector<int> par(H + W, -1);
    bool f = false;
    auto dfs = [&](auto dfs, int now, int num, int p) -> void{
        used[now] = num;
        if(f) true;
        for(int to : G[now]){
            if(f) return;
            if(p == to) continue;
            if(used[to] == -1) continue;
            if(used[to] > 0 && num - used[to] >= 3){
                par[to] = now;
                f = true;
                int t = now;
                vector<int> ans;
                ans.push_back(m[{min(now, to), max(now, to)}]);
                while(par[t] != now){
                    ans.push_back(m[{min(t, par[t]), max(t, par[t])}]);
                    t = par[t];
                }
                reverse(ans.begin(), ans.end());
                cout << ans.size() << endl;
                int ofs = 0;
                int n = ans.size();
                for(int i = 0; i < n; i++){
                    if(i % 2 == 0){
                        if(y[ans[i]] != y[ans[(i + 1) % n]]){
                            ofs = 1;
                            break;
                        }
                    }
                    else{
                        if(x[ans[i]] != x[ans[(i + 1) % n]]) {
                            ofs = 1;
                            break;
                        }
                    }
                }
                for(int i = 0; i < n; i++){
                    if(i == n - 1) cout << ans[(i + ofs) % n] + 1 << endl;
                    else cout << ans[(i + ofs) % n] + 1 << ' ';
                }
                return;
            }
            par[to] = now;
            dfs(dfs, to, num + 1, now);
        }
        used[now] = -1;
    };
    for(int i = 0; i < H; i++){
        if(used[i] == 0) dfs(dfs, i, 1, -1);
    }
    if(!f) cout << -1 << endl;

}
0