結果

問題 No.1647 Travel in Mitaru city 2
ユーザー 蜜蜂蜜蜂
提出日時 2021-07-27 15:17:49
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 289 ms / 2,500 ms
コード長 2,090 bytes
コンパイル時間 3,911 ms
コンパイル使用メモリ 232,180 KB
実行使用メモリ 36,956 KB
最終ジャッジ日時 2024-04-21 04:45:41
合計ジャッジ時間 17,921 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,528 KB
testcase_01 AC 4 ms
6,400 KB
testcase_02 AC 5 ms
6,656 KB
testcase_03 AC 5 ms
6,528 KB
testcase_04 AC 7 ms
6,940 KB
testcase_05 AC 5 ms
6,528 KB
testcase_06 AC 5 ms
6,656 KB
testcase_07 AC 8 ms
7,040 KB
testcase_08 AC 253 ms
34,636 KB
testcase_09 AC 220 ms
32,708 KB
testcase_10 AC 249 ms
34,812 KB
testcase_11 AC 183 ms
29,696 KB
testcase_12 AC 197 ms
30,036 KB
testcase_13 AC 191 ms
30,736 KB
testcase_14 AC 258 ms
35,504 KB
testcase_15 AC 257 ms
35,152 KB
testcase_16 AC 217 ms
33,404 KB
testcase_17 AC 198 ms
30,464 KB
testcase_18 AC 214 ms
30,920 KB
testcase_19 AC 223 ms
35,152 KB
testcase_20 AC 223 ms
33,744 KB
testcase_21 AC 232 ms
35,532 KB
testcase_22 AC 267 ms
36,056 KB
testcase_23 AC 243 ms
36,204 KB
testcase_24 AC 224 ms
34,000 KB
testcase_25 AC 240 ms
35,056 KB
testcase_26 AC 258 ms
36,196 KB
testcase_27 AC 256 ms
36,216 KB
testcase_28 AC 272 ms
36,952 KB
testcase_29 AC 287 ms
36,824 KB
testcase_30 AC 254 ms
35,588 KB
testcase_31 AC 282 ms
36,956 KB
testcase_32 AC 248 ms
36,340 KB
testcase_33 AC 238 ms
34,384 KB
testcase_34 AC 287 ms
36,956 KB
testcase_35 AC 274 ms
34,944 KB
testcase_36 AC 289 ms
36,828 KB
testcase_37 AC 282 ms
36,828 KB
testcase_38 AC 247 ms
33,024 KB
testcase_39 AC 210 ms
31,144 KB
testcase_40 AC 241 ms
32,756 KB
testcase_41 AC 238 ms
31,456 KB
testcase_42 AC 242 ms
33,008 KB
testcase_43 AC 4 ms
6,528 KB
testcase_44 AC 8 ms
11,136 KB
testcase_45 AC 190 ms
28,416 KB
testcase_46 AC 216 ms
28,416 KB
testcase_47 AC 207 ms
28,416 KB
testcase_48 AC 202 ms
28,416 KB
testcase_49 AC 184 ms
28,416 KB
testcase_50 AC 183 ms
28,416 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