結果

問題 No.1647 Travel in Mitaru city 2
ユーザー SSRSSSRS
提出日時 2021-08-13 21:38:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 119 ms / 2,500 ms
コード長 1,600 bytes
コンパイル時間 2,077 ms
コンパイル使用メモリ 173,568 KB
実行使用メモリ 24,320 KB
最終ジャッジ日時 2024-04-21 04:47:06
合計ジャッジ時間 9,900 ms
ジャッジサーバーID
(参考情報)
judge1 / 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 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 102 ms
22,656 KB
testcase_09 AC 95 ms
20,608 KB
testcase_10 AC 100 ms
22,400 KB
testcase_11 AC 93 ms
20,224 KB
testcase_12 AC 101 ms
19,968 KB
testcase_13 AC 102 ms
21,632 KB
testcase_14 AC 108 ms
22,912 KB
testcase_15 AC 110 ms
22,656 KB
testcase_16 AC 100 ms
21,888 KB
testcase_17 AC 99 ms
20,864 KB
testcase_18 AC 110 ms
22,528 KB
testcase_19 AC 108 ms
23,168 KB
testcase_20 AC 105 ms
21,120 KB
testcase_21 AC 108 ms
23,424 KB
testcase_22 AC 112 ms
23,296 KB
testcase_23 AC 111 ms
23,680 KB
testcase_24 AC 107 ms
21,120 KB
testcase_25 AC 101 ms
22,016 KB
testcase_26 AC 106 ms
23,552 KB
testcase_27 AC 105 ms
23,680 KB
testcase_28 AC 110 ms
24,192 KB
testcase_29 AC 112 ms
24,192 KB
testcase_30 AC 106 ms
22,400 KB
testcase_31 AC 112 ms
24,320 KB
testcase_32 AC 113 ms
23,936 KB
testcase_33 AC 107 ms
21,504 KB
testcase_34 AC 112 ms
24,320 KB
testcase_35 AC 112 ms
24,320 KB
testcase_36 AC 115 ms
24,320 KB
testcase_37 AC 119 ms
24,192 KB
testcase_38 AC 105 ms
20,608 KB
testcase_39 AC 101 ms
18,688 KB
testcase_40 AC 106 ms
20,480 KB
testcase_41 AC 101 ms
18,688 KB
testcase_42 AC 107 ms
20,608 KB
testcase_43 AC 2 ms
5,376 KB
testcase_44 AC 8 ms
10,368 KB
testcase_45 AC 93 ms
15,104 KB
testcase_46 AC 102 ms
15,360 KB
testcase_47 AC 94 ms
15,232 KB
testcase_48 AC 96 ms
15,360 KB
testcase_49 AC 94 ms
15,360 KB
testcase_50 AC 95 ms
15,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
void dfs(vector<vector<pair<int, int>>> &E, vector<int> &d, vector<int> &p, vector<int> &p2, int v){
  for (auto P : E[v]){
    int w = P.first;
    int id = P.second;
    if (d[w] == -1){
      d[w] = d[v] + 1;
      p[w] = v;
      p2[w] = id;
      dfs(E, d, p, p2, w);
    }
  }
}
int main(){
  int H, W, N;
  cin >> H >> W >> N;
  vector<int> x(N), y(N);
  for (int i = 0; i < N; i++){
    cin >> x[i] >> y[i];
    x[i]--;
    y[i]--;
  }
  vector<vector<pair<int, int>>> E(H + W);
  for (int i = 0; i < N; i++){
    E[x[i]].push_back(make_pair(H + y[i], i));
    E[H + y[i]].push_back(make_pair(x[i], i));
  }
  vector<int> d(H + W, -1);
  vector<int> p(H + W, -1);
  vector<int> p2(H + W, -1);
  for (int i = 0; i < H + W; i++){
    if (d[i] == -1){
      d[i] = 0;
      dfs(E, d, p, p2, i);
    }
  }
  vector<int> T;
  for (int i = 0; i < H + W; i++){
    for (auto P : E[i]){
      int w = P.first;
      int id = P.second;
      if (i != p[w] && w != p[i]){
        int a = i, b = w;
        if (d[b] < d[a]){
          swap(a, b);
        }
        T.push_back(id);
        for (int v = b; v != a; v = p[v]){
          T.push_back(p2[v]);
        }
        break;
      }
    }
    if (!T.empty()){
      break;
    }
  }
  if (T.empty()){
    cout << -1 << endl;
  } else {
    if (y[T[0]] != y[T[1]]){
      rotate(T.begin(), T.begin() + 1, T.end());
    }
    int X = T.size();
    cout << X << endl;
    for (int i = 0; i < X; i++){
      cout << T[i] + 1;
      if (i < X - 1){
        cout << ' ';
      }
    }
    cout << endl;
  }
}
0