結果

問題 No.1647 Travel in Mitaru city 2
ユーザー 蜜蜂蜜蜂
提出日時 2021-07-27 23:21:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 275 ms / 2,500 ms
コード長 2,090 bytes
コンパイル時間 4,397 ms
コンパイル使用メモリ 232,252 KB
実行使用メモリ 36,960 KB
最終ジャッジ日時 2024-04-21 04:46:55
合計ジャッジ時間 17,234 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,528 KB
testcase_01 AC 5 ms
6,400 KB
testcase_02 AC 4 ms
6,400 KB
testcase_03 AC 4 ms
6,656 KB
testcase_04 AC 5 ms
6,912 KB
testcase_05 AC 4 ms
6,656 KB
testcase_06 AC 4 ms
6,656 KB
testcase_07 AC 7 ms
7,168 KB
testcase_08 AC 260 ms
34,636 KB
testcase_09 AC 218 ms
32,580 KB
testcase_10 AC 251 ms
34,812 KB
testcase_11 AC 184 ms
29,568 KB
testcase_12 AC 210 ms
30,080 KB
testcase_13 AC 196 ms
30,720 KB
testcase_14 AC 265 ms
35,380 KB
testcase_15 AC 247 ms
35,280 KB
testcase_16 AC 229 ms
33,396 KB
testcase_17 AC 191 ms
30,592 KB
testcase_18 AC 207 ms
30,976 KB
testcase_19 AC 220 ms
35,024 KB
testcase_20 AC 239 ms
33,744 KB
testcase_21 AC 238 ms
35,532 KB
testcase_22 AC 257 ms
35,932 KB
testcase_23 AC 272 ms
36,208 KB
testcase_24 AC 227 ms
34,004 KB
testcase_25 AC 246 ms
35,184 KB
testcase_26 AC 271 ms
36,204 KB
testcase_27 AC 260 ms
36,344 KB
testcase_28 AC 266 ms
36,824 KB
testcase_29 AC 275 ms
36,952 KB
testcase_30 AC 253 ms
35,716 KB
testcase_31 AC 273 ms
36,960 KB
testcase_32 AC 232 ms
36,308 KB
testcase_33 AC 238 ms
34,296 KB
testcase_34 AC 273 ms
36,864 KB
testcase_35 AC 255 ms
34,944 KB
testcase_36 AC 265 ms
36,956 KB
testcase_37 AC 271 ms
36,824 KB
testcase_38 AC 232 ms
33,152 KB
testcase_39 AC 215 ms
31,104 KB
testcase_40 AC 243 ms
32,768 KB
testcase_41 AC 226 ms
31,488 KB
testcase_42 AC 239 ms
32,896 KB
testcase_43 AC 4 ms
6,656 KB
testcase_44 AC 7 ms
11,264 KB
testcase_45 AC 193 ms
28,416 KB
testcase_46 AC 213 ms
28,416 KB
testcase_47 AC 190 ms
28,416 KB
testcase_48 AC 190 ms
28,416 KB
testcase_49 AC 189 ms
28,416 KB
testcase_50 AC 187 ms
28,544 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