結果

問題 No.1647 Travel in Mitaru city 2
ユーザー milanis48663220milanis48663220
提出日時 2021-08-13 22:30:37
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 333 ms / 2,500 ms
コード長 2,358 bytes
コンパイル時間 1,449 ms
コンパイル使用メモリ 128,988 KB
実行使用メモリ 32,640 KB
最終ジャッジ日時 2024-04-21 04:53:38
合計ジャッジ時間 16,347 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
8,196 KB
testcase_01 AC 5 ms
8,316 KB
testcase_02 AC 4 ms
8,136 KB
testcase_03 AC 4 ms
8,284 KB
testcase_04 AC 6 ms
8,720 KB
testcase_05 AC 3 ms
8,188 KB
testcase_06 AC 5 ms
8,644 KB
testcase_07 AC 7 ms
9,028 KB
testcase_08 AC 297 ms
30,496 KB
testcase_09 AC 268 ms
29,044 KB
testcase_10 AC 298 ms
30,896 KB
testcase_11 AC 207 ms
26,592 KB
testcase_12 AC 232 ms
27,112 KB
testcase_13 AC 258 ms
27,676 KB
testcase_14 AC 303 ms
31,476 KB
testcase_15 AC 305 ms
31,172 KB
testcase_16 AC 273 ms
29,672 KB
testcase_17 AC 246 ms
27,260 KB
testcase_18 AC 255 ms
27,716 KB
testcase_19 AC 267 ms
30,788 KB
testcase_20 AC 262 ms
29,892 KB
testcase_21 AC 253 ms
31,296 KB
testcase_22 AC 322 ms
31,796 KB
testcase_23 AC 278 ms
31,908 KB
testcase_24 AC 244 ms
30,036 KB
testcase_25 AC 257 ms
30,772 KB
testcase_26 AC 284 ms
31,776 KB
testcase_27 AC 288 ms
31,896 KB
testcase_28 AC 322 ms
32,640 KB
testcase_29 AC 333 ms
32,640 KB
testcase_30 AC 309 ms
31,316 KB
testcase_31 AC 313 ms
32,516 KB
testcase_32 AC 280 ms
31,920 KB
testcase_33 AC 282 ms
30,416 KB
testcase_34 AC 296 ms
32,384 KB
testcase_35 AC 286 ms
30,692 KB
testcase_36 AC 316 ms
32,384 KB
testcase_37 AC 306 ms
32,384 KB
testcase_38 AC 291 ms
29,508 KB
testcase_39 AC 248 ms
28,104 KB
testcase_40 AC 259 ms
29,252 KB
testcase_41 AC 252 ms
28,388 KB
testcase_42 AC 249 ms
29,384 KB
testcase_43 AC 4 ms
8,064 KB
testcase_44 AC 4 ms
8,388 KB
testcase_45 AC 222 ms
26,052 KB
testcase_46 AC 241 ms
26,304 KB
testcase_47 AC 210 ms
26,148 KB
testcase_48 AC 240 ms
26,132 KB
testcase_49 AC 230 ms
26,164 KB
testcase_50 AC 201 ms
26,184 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

vector<int> g[200000];
bool used[200000];
int pre[200000];

vector<int> ans;
void make_loop(int v, int end){
    while(v != end){
        ans.push_back(v);
        v = pre[v];
    }
    ans.push_back(end);
}

bool dfs(int v){
    used[v] = true;
    for(int to: g[v]){
        if(!used[to]){
            pre[to] = v;
            if(dfs(to)) return true;
        }else if(pre[v] != to){
            make_loop(v, to);
            return true;
        }
    }
    return false;
}

using P = pair<int, int>;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int h, w; cin >> h >> w;
    int n; cin >> n;
    map<P, int> idx;
    vector<int> x(n), y(n);
    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]);
        idx[P(x[i], h+y[i])] = i;
        idx[P(h+y[i], x[i])] = i;
    }

    for(int i = 0; i < h+w; i++){
        if(!used[i]){
            if(dfs(i)){
                int m = ans.size();
                cout << m << endl;
                vector<int> v(m);
                for(int j = 0; j < m; j++){
                    int k = (j+1)%m;
                    v[j] = idx[P(ans[j], ans[k])];
                }
                if(x[v[0]] != x[v[1]]){
                    for(int j = 0; j < m; j++) cout << v[j]+1 << ' ';
                    cout << endl;
                }else{
                    for(int j = 0; j < m; j++) cout << v[(j+1)%m]+1 << ' ';
                    cout << endl;
                }
                // for(int j: v) cout << x[j] << ' ' << y[j] << endl;
                return 0;
            }
        }
    }
    cout << -1 << endl;
}
0