結果

問題 No.1647 Travel in Mitaru city 2
ユーザー 👑 Nachia
提出日時 2021-08-13 23:13:28
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 91 ms / 2,500 ms
コード長 1,906 bytes
コンパイル時間 3,670 ms
コンパイル使用メモリ 121,844 KB
最終ジャッジ日時 2025-01-23 21:11:23
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 48
権限があれば一括ダウンロードができます

ソースコード

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