結果

問題 No.1647 Travel in Mitaru city 2
ユーザー 蜜蜂蜜蜂
提出日時 2021-07-27 22:27:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,094 bytes
コンパイル時間 5,240 ms
コンパイル使用メモリ 232,968 KB
実行使用メモリ 37,088 KB
最終ジャッジ日時 2024-04-14 15:45:33
合計ジャッジ時間 25,886 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 4 ms
6,940 KB
testcase_02 WA -
testcase_03 AC 4 ms
6,944 KB
testcase_04 AC 7 ms
6,940 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 8 ms
7,040 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 AC 5 ms
6,944 KB
testcase_44 WA -
testcase_45 WA -
testcase_46 AC 272 ms
28,444 KB
testcase_47 WA -
testcase_48 WA -
testcase_49 AC 257 ms
28,408 KB
testcase_50 AC 251 ms
28,408 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+1)%ans.size()]+1<<" ";
    }
    cout<<"\n";
  }
}
0