結果

問題 No.1647 Travel in Mitaru city 2
ユーザー 蜜蜂蜜蜂
提出日時 2021-07-27 15:17:49
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 273 ms / 2,500 ms
コード長 2,090 bytes
コンパイル時間 4,344 ms
コンパイル使用メモリ 236,024 KB
実行使用メモリ 36,664 KB
最終ジャッジ日時 2023-08-03 05:31:22
合計ジャッジ時間 22,606 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,156 KB
testcase_01 AC 4 ms
6,088 KB
testcase_02 AC 4 ms
6,224 KB
testcase_03 AC 4 ms
6,156 KB
testcase_04 AC 5 ms
6,864 KB
testcase_05 AC 3 ms
6,160 KB
testcase_06 AC 4 ms
6,156 KB
testcase_07 AC 7 ms
7,028 KB
testcase_08 AC 226 ms
34,388 KB
testcase_09 AC 221 ms
32,456 KB
testcase_10 AC 219 ms
34,596 KB
testcase_11 AC 169 ms
29,588 KB
testcase_12 AC 200 ms
29,816 KB
testcase_13 AC 174 ms
30,520 KB
testcase_14 AC 240 ms
35,360 KB
testcase_15 AC 241 ms
34,936 KB
testcase_16 AC 216 ms
33,240 KB
testcase_17 AC 165 ms
30,412 KB
testcase_18 AC 177 ms
30,764 KB
testcase_19 AC 210 ms
34,844 KB
testcase_20 AC 214 ms
33,268 KB
testcase_21 AC 191 ms
35,312 KB
testcase_22 AC 220 ms
35,816 KB
testcase_23 AC 258 ms
35,968 KB
testcase_24 AC 213 ms
33,852 KB
testcase_25 AC 225 ms
34,836 KB
testcase_26 AC 261 ms
35,964 KB
testcase_27 AC 251 ms
36,132 KB
testcase_28 AC 242 ms
36,664 KB
testcase_29 AC 245 ms
36,632 KB
testcase_30 AC 220 ms
35,308 KB
testcase_31 AC 267 ms
36,588 KB
testcase_32 AC 207 ms
36,224 KB
testcase_33 AC 221 ms
34,056 KB
testcase_34 AC 273 ms
36,592 KB
testcase_35 AC 270 ms
34,824 KB
testcase_36 AC 272 ms
36,612 KB
testcase_37 AC 247 ms
36,584 KB
testcase_38 AC 218 ms
32,812 KB
testcase_39 AC 192 ms
31,052 KB
testcase_40 AC 249 ms
32,576 KB
testcase_41 AC 188 ms
31,460 KB
testcase_42 AC 194 ms
32,692 KB
testcase_43 AC 3 ms
6,180 KB
testcase_44 AC 5 ms
10,792 KB
testcase_45 AC 175 ms
28,340 KB
testcase_46 AC 184 ms
28,236 KB
testcase_47 AC 176 ms
28,328 KB
testcase_48 AC 180 ms
28,304 KB
testcase_49 AC 191 ms
28,392 KB
testcase_50 AC 176 ms
28,340 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//g++ 1.cpp -std=c++14 -O2 -I .
#include <bits/stdc++.h>
using namespace std;

#include <atcoder/all>
using namespace atcoder;

using ll = long long;
using ld = long double;

using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vld = vector<ld>;
using vvld = vector<vld>;

#define fi first
#define se second
#define pb push_back
#define pq_big(T) priority_queue<T,vector<T>,less<T>>
#define pq_small(T) priority_queue<T,vector<T>,greater<T>>
#define all(a) a.begin(),a.end()
#define rep(i,start,end) for(ll i=start;i<(ll)(end);i++)
#define per(i,start,end) for(ll i=start;i>=(ll)(end);i--)

vi seen(410000,0);
vi fin(410000,0);
vi hist;
int pos=-1;

void dfs(vvi &g,int now,int p){
  seen[now]=1;
  hist.pb(now);

  for(int next:g[now]){
    if(next==p){
      continue;
    }
    if(fin[next]==1){
      continue;
    }
    if(seen[next]==1&&fin[next]==0){
      pos=next;
      return;
    }

    dfs(g,next,now);

    if(pos!=-1){
      return;
    }
  }

  hist.pop_back();
  fin[now]=1;
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int h,w,n;
  cin>>h>>w>>n;

  vvi g(h+w);
  vector<pair<int,int>> place(n);
  map<pair<int,int>,int> inv;

  rep(i,0,n){
    int x,y;
    cin>>x>>y;
    x--;y--;
    place[i].fi=x,place[i].se=y;
    g[x].pb(h+y);
    g[h+y].pb(x);
    inv[{x,h+y}]=i;
    inv[{h+y,x}]=i;
  }

  rep(i,0,h+w){
    if(seen[i]==1)continue;
    if(g[i].size()==0)continue;

    dfs(g,i,-1);

    if(pos!=-1){
      break;
    }
  }

  if(pos==-1){
    cout<<"-1\n";
    return 0;
  }

  vi cycle;
  while(!hist.empty()){
    int t=hist[hist.size()-1];
    cycle.pb(t);
    hist.pop_back();
    if(t==pos){
      break;
    }
  }

  vi ans;
  rep(i,0,cycle.size()){
    int now=cycle[i],next=cycle[(i+1)%cycle.size()];
    ans.pb(inv[{now,next}]);
  }

  cout<<ans.size()<<"\n";

  if(place[ans[0]].se==place[ans[1]].se){
    rep(i,0,ans.size()){
      cout<<ans[i]+1<<" ";
    }
    cout<<"\n";
  }
  else{
    rep(i,1,ans.size()+1){
      cout<<ans[i%ans.size()]+1<<" ";
    }
    cout<<"\n";
  }
}
0