結果

問題 No.1647 Travel in Mitaru city 2
ユーザー 👑 NachiaNachia
提出日時 2021-08-13 23:13:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 59 ms / 2,500 ms
コード長 1,906 bytes
コンパイル時間 900 ms
コンパイル使用メモリ 87,644 KB
実行使用メモリ 15,284 KB
最終ジャッジ日時 2024-04-27 05:08:37
合計ジャッジ時間 5,738 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 50 ms
14,628 KB
testcase_09 AC 44 ms
13,424 KB
testcase_10 AC 51 ms
14,824 KB
testcase_11 AC 46 ms
14,596 KB
testcase_12 AC 44 ms
13,908 KB
testcase_13 AC 51 ms
14,472 KB
testcase_14 AC 52 ms
14,900 KB
testcase_15 AC 51 ms
14,816 KB
testcase_16 AC 51 ms
14,484 KB
testcase_17 AC 47 ms
14,248 KB
testcase_18 AC 46 ms
14,008 KB
testcase_19 AC 43 ms
13,912 KB
testcase_20 AC 50 ms
14,616 KB
testcase_21 AC 44 ms
14,336 KB
testcase_22 AC 54 ms
15,284 KB
testcase_23 AC 55 ms
15,256 KB
testcase_24 AC 45 ms
14,356 KB
testcase_25 AC 43 ms
13,956 KB
testcase_26 AC 52 ms
15,132 KB
testcase_27 AC 54 ms
15,224 KB
testcase_28 AC 58 ms
15,128 KB
testcase_29 AC 53 ms
15,128 KB
testcase_30 AC 44 ms
13,876 KB
testcase_31 AC 59 ms
15,132 KB
testcase_32 AC 49 ms
14,360 KB
testcase_33 AC 47 ms
14,360 KB
testcase_34 AC 56 ms
15,132 KB
testcase_35 AC 42 ms
13,912 KB
testcase_36 AC 53 ms
15,132 KB
testcase_37 AC 52 ms
15,132 KB
testcase_38 AC 52 ms
14,988 KB
testcase_39 AC 47 ms
14,636 KB
testcase_40 AC 50 ms
15,000 KB
testcase_41 AC 43 ms
14,308 KB
testcase_42 AC 49 ms
14,992 KB
testcase_43 AC 2 ms
6,940 KB
testcase_44 AC 6 ms
10,252 KB
testcase_45 AC 38 ms
14,504 KB
testcase_46 AC 49 ms
14,332 KB
testcase_47 AC 38 ms
14,532 KB
testcase_48 AC 43 ms
14,576 KB
testcase_49 AC 38 ms
14,504 KB
testcase_50 AC 40 ms
14,396 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <atcoder/modint>
#include <atcoder/fenwicktree>
using namespace std;
using i64 = int64_t;
using u64 = uint64_t;
#define rep(i,n) for(int i=0; i<(n); i++)


struct Edge{ int to; int i; };

int H,W,N;
vector<vector<Edge>> E;

int main() {
  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;
}


struct ios_do_not_sync{
  ios_do_not_sync(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
  }
} ios_do_not_sync_instance;


0