結果

問題 No.1647 Travel in Mitaru city 2
ユーザー 👑 NachiaNachia
提出日時 2024-04-21 16:50:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 86 ms / 2,500 ms
コード長 2,205 bytes
コンパイル時間 1,070 ms
コンパイル使用メモリ 87,424 KB
実行使用メモリ 15,280 KB
最終ジャッジ日時 2024-04-21 16:50:12
合計ジャッジ時間 8,946 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 73 ms
14,752 KB
testcase_09 AC 69 ms
13,288 KB
testcase_10 AC 81 ms
14,688 KB
testcase_11 AC 66 ms
14,464 KB
testcase_12 AC 74 ms
13,776 KB
testcase_13 AC 78 ms
14,592 KB
testcase_14 AC 80 ms
14,768 KB
testcase_15 AC 80 ms
14,816 KB
testcase_16 AC 74 ms
14,480 KB
testcase_17 AC 77 ms
14,240 KB
testcase_18 AC 67 ms
14,248 KB
testcase_19 AC 65 ms
13,888 KB
testcase_20 AC 80 ms
14,612 KB
testcase_21 AC 67 ms
14,076 KB
testcase_22 AC 83 ms
15,280 KB
testcase_23 AC 85 ms
15,252 KB
testcase_24 AC 71 ms
14,380 KB
testcase_25 AC 60 ms
14,068 KB
testcase_26 AC 78 ms
15,252 KB
testcase_27 AC 79 ms
15,224 KB
testcase_28 AC 84 ms
15,124 KB
testcase_29 AC 79 ms
15,124 KB
testcase_30 AC 65 ms
13,732 KB
testcase_31 AC 77 ms
15,124 KB
testcase_32 AC 71 ms
14,360 KB
testcase_33 AC 66 ms
14,184 KB
testcase_34 AC 86 ms
15,000 KB
testcase_35 AC 67 ms
13,856 KB
testcase_36 AC 85 ms
15,128 KB
testcase_37 AC 83 ms
15,128 KB
testcase_38 AC 77 ms
14,988 KB
testcase_39 AC 70 ms
14,624 KB
testcase_40 AC 74 ms
14,996 KB
testcase_41 AC 61 ms
14,296 KB
testcase_42 AC 79 ms
14,992 KB
testcase_43 AC 2 ms
6,940 KB
testcase_44 AC 6 ms
10,080 KB
testcase_45 AC 61 ms
14,432 KB
testcase_46 AC 80 ms
14,500 KB
testcase_47 AC 57 ms
14,452 KB
testcase_48 AC 72 ms
14,492 KB
testcase_49 AC 58 ms
14,452 KB
testcase_50 AC 58 ms
14,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>

using namespace std;
#define rep(i,n) for(int i=0; i<(int)(n); i++)

int main(){
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int H,W,N;
    struct Edge{ int to; int i; };
    vector<vector<Edge>> E;
    cin >> H >> W >> N;
    E.resize(H+W);
    rep(i,N){
        int y,x; cin >> y >> x; y--; x--;
        E[y].push_back({H+x,i});
        E[H+x].push_back({y,i});
    }
    vector<int> D(H+W,-1);
    vector<int> P(H+W,-1);
    vector<int> Pi(H+W,-1);
    vector<int> ans;
    for(int s=0; s<=H; s++) if(D[s] == -1){
        P[s] = -2;
        D[s] = 0;
        vector<int> I;
        I.push_back(s);
        rep(i,I.size()){
            int p = I[i];
            for(auto e : E[p]){
                if(D[e.to] == -1){
                    D[e.to] = D[p] + 1;
                    P[e.to] = p;
                    Pi[e.to] = e.i;
                    I.push_back(e.to);
                }
                if(D[p] != 0) if(D[e.to] == D[p] + 1) if(P[e.to] != p){
                    vector<int> ans1 = {e.i};
                    int x1 = p;
                    vector<int> ans2 = {Pi[e.to]};
                    int x2 = P[e.to];
                    while(x1 != x2){
                        ans1.push_back(Pi[x1]);
                        ans2.push_back(Pi[x2]);
                        x1 = P[x1];
                        x2 = P[x2];
                    }
                    if(D[x1] % 2 == 1){
                        ans1.push_back(ans2.back());
                        ans2.pop_back();
                    }
                    while(ans1.size()){
                        ans.push_back(ans1.back());
                        ans1.pop_back();
                    }
                    for(auto a : ans2) ans.push_back(a);
                    cout << ans.size() << "\n";
                    rep(i,ans.size()){
                        if(i) cout << " ";
                        cout << (ans[i]+1);
                    }
                    cout << "\n";
                    return 0;
                }
            }
        }
        for(int p : I) D[p] = -2;
    }
    cout << "-1\n";
    return 0;
}
0