結果

問題 No.1647 Travel in Mitaru city 2
ユーザー たたき@競プロたたき@競プロ
提出日時 2023-08-30 06:55:56
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 420 ms / 2,500 ms
コード長 1,287 bytes
コンパイル時間 5,541 ms
コンパイル使用メモリ 316,152 KB
実行使用メモリ 35,144 KB
最終ジャッジ日時 2023-08-30 06:56:21
合計ジャッジ時間 24,898 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 4 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 6 ms
4,376 KB
testcase_08 AC 343 ms
32,564 KB
testcase_09 AC 321 ms
30,012 KB
testcase_10 AC 364 ms
32,784 KB
testcase_11 AC 266 ms
26,752 KB
testcase_12 AC 309 ms
26,612 KB
testcase_13 AC 276 ms
27,692 KB
testcase_14 AC 348 ms
33,464 KB
testcase_15 AC 343 ms
33,176 KB
testcase_16 AC 345 ms
31,192 KB
testcase_17 AC 265 ms
27,884 KB
testcase_18 AC 280 ms
27,692 KB
testcase_19 AC 295 ms
33,420 KB
testcase_20 AC 338 ms
31,268 KB
testcase_21 AC 323 ms
33,980 KB
testcase_22 AC 377 ms
34,096 KB
testcase_23 AC 402 ms
34,164 KB
testcase_24 AC 324 ms
31,696 KB
testcase_25 AC 346 ms
32,564 KB
testcase_26 AC 412 ms
34,152 KB
testcase_27 AC 401 ms
34,356 KB
testcase_28 AC 403 ms
34,980 KB
testcase_29 AC 384 ms
35,132 KB
testcase_30 AC 362 ms
33,628 KB
testcase_31 AC 417 ms
35,068 KB
testcase_32 AC 324 ms
34,648 KB
testcase_33 AC 339 ms
32,352 KB
testcase_34 AC 420 ms
34,940 KB
testcase_35 AC 354 ms
32,620 KB
testcase_36 AC 386 ms
35,144 KB
testcase_37 AC 412 ms
34,984 KB
testcase_38 AC 348 ms
30,160 KB
testcase_39 AC 304 ms
28,016 KB
testcase_40 AC 350 ms
29,968 KB
testcase_41 AC 335 ms
28,364 KB
testcase_42 AC 346 ms
30,156 KB
testcase_43 AC 1 ms
4,376 KB
testcase_44 AC 5 ms
7,780 KB
testcase_45 AC 286 ms
24,132 KB
testcase_46 AC 293 ms
24,336 KB
testcase_47 AC 285 ms
24,200 KB
testcase_48 AC 303 ms
24,712 KB
testcase_49 AC 284 ms
24,404 KB
testcase_50 AC 277 ms
24,188 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
using mint=modint998244353; //1000000007;
using ll=long long;
using pp=pair<int,int>;
#define sr string 
#define vc vector
#define fi first
#define se second
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define pb push_back
#define all(v) v.begin(),v.end()
#define pque priority_queue
#define bpc(a) __builtin_popcount(a)
int main(){
  int h,w,n;cin>>h>>w>>n;
  vc v(h+w,vc<int>(0));
  map<pp,int>m;
  rep(i,n){
    int x,y;cin>>x>>y;
    x--; y--;
    v[x].pb(h+y); v[h+y].pb(x);
    m[{x,y+h}]=i; m[{y+h,x}]=i;
  }
  vc<bool>ch(h+w,false);
  vc<int>ans;
  auto f=[&](auto f,int a,int p)->int{
    if(ch[a])return a;
    ch[a]=true;
    int res=-1;
    for(auto au:v[a])if(au!=p){
      res=f(f,au,a);
      if(res==-2)return -2;
      if(res!=-1)break;
    }
    if(res!=-1)ans.pb(a);
    if(res==a)res=-2;
    return res;
  };
  rep(i,h)if(!ch[i]){
    f(f,i,-1);
    if(ans.size())break;
  }
  if(!ans.size()){
    cout<<-1;
    return 0;
  }
  cout<<ans.size()<<"\n";
  if(ans[0]>=h){
    ans.pb(ans[0]); ans.pb(ans[1]);
    for(int i=1;i<ans.size()-1;i++)cout<<m[{ans[i],ans[i+1]}]+1<<' ';
  }
  else{
    ans.pb(ans[0]);
    rep(i,ans.size()-1)cout<<m[{ans[i],ans[i+1]}]+1<<' ';
  }
}
    
0