結果

問題 No.1647 Travel in Mitaru city 2
ユーザー tko919tko919
提出日時 2021-08-24 00:19:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 111 ms / 2,500 ms
コード長 2,310 bytes
コンパイル時間 2,479 ms
コンパイル使用メモリ 209,980 KB
実行使用メモリ 19,456 KB
最終ジャッジ日時 2024-04-25 18:20:15
合計ジャッジ時間 9,578 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
8,288 KB
testcase_01 AC 5 ms
8,208 KB
testcase_02 AC 5 ms
8,204 KB
testcase_03 AC 4 ms
8,344 KB
testcase_04 AC 7 ms
8,436 KB
testcase_05 AC 5 ms
8,368 KB
testcase_06 AC 5 ms
8,340 KB
testcase_07 AC 6 ms
8,300 KB
testcase_08 AC 107 ms
17,160 KB
testcase_09 AC 103 ms
15,812 KB
testcase_10 AC 111 ms
16,424 KB
testcase_11 AC 97 ms
15,892 KB
testcase_12 AC 108 ms
17,856 KB
testcase_13 AC 83 ms
13,204 KB
testcase_14 AC 99 ms
17,996 KB
testcase_15 AC 95 ms
16,860 KB
testcase_16 AC 100 ms
16,600 KB
testcase_17 AC 98 ms
15,200 KB
testcase_18 AC 92 ms
14,676 KB
testcase_19 AC 92 ms
15,188 KB
testcase_20 AC 94 ms
14,736 KB
testcase_21 AC 86 ms
14,256 KB
testcase_22 AC 110 ms
18,772 KB
testcase_23 AC 109 ms
17,924 KB
testcase_24 AC 107 ms
15,968 KB
testcase_25 AC 110 ms
16,128 KB
testcase_26 AC 106 ms
18,816 KB
testcase_27 AC 104 ms
18,656 KB
testcase_28 AC 105 ms
18,780 KB
testcase_29 AC 93 ms
17,716 KB
testcase_30 AC 92 ms
14,140 KB
testcase_31 AC 109 ms
19,092 KB
testcase_32 AC 110 ms
16,612 KB
testcase_33 AC 100 ms
15,772 KB
testcase_34 AC 103 ms
17,364 KB
testcase_35 AC 102 ms
16,428 KB
testcase_36 AC 97 ms
19,456 KB
testcase_37 AC 101 ms
18,488 KB
testcase_38 AC 102 ms
15,524 KB
testcase_39 AC 93 ms
15,440 KB
testcase_40 AC 95 ms
15,796 KB
testcase_41 AC 90 ms
16,248 KB
testcase_42 AC 101 ms
16,088 KB
testcase_43 AC 4 ms
8,256 KB
testcase_44 AC 5 ms
8,656 KB
testcase_45 AC 71 ms
12,200 KB
testcase_46 AC 82 ms
13,680 KB
testcase_47 AC 75 ms
13,588 KB
testcase_48 AC 71 ms
12,352 KB
testcase_49 AC 87 ms
13,736 KB
testcase_50 AC 74 ms
12,700 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

//template
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define ALL(v) (v).begin(),(v).end()
using ll=long long int;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
template<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}

struct UnionFind{
   vector<int> par; int n;
   UnionFind(){}
   UnionFind(int _n):par(_n,-1),n(_n){}
   int root(int x){return par[x]<0?x:par[x]=root(par[x]);}
   bool same(int x,int y){return root(x)==root(y);}
   int size(int x){return -par[root(x)];}
   bool unite(int x,int y){
      x=root(x),y=root(y); if(x==y)return false;
      if(size(x)>size(y))swap(x,y);
      par[y]+=par[x]; par[x]=y; n--; return true;
   }
};

using P=pair<int,int>;
vector<P> g[201010];
bool dfs(int v,int p,int tar,vector<int>& path){
   if(v==tar)return true;
   for(auto& [to,id]:g[v])if(to!=p){
      path.push_back(id);
      if(dfs(to,v,tar,path))return true;
      path.pop_back();
   }
   return false;
}

int main(){
   int h,w,n;
   cin>>h>>w>>n;
   vector<int> x(n),y(n);
   rep(i,0,n){
      cin>>x[i]>>y[i];
      x[i]--; y[i]--;
   }

   UnionFind uni(h+w);
   rep(i,0,n){
      if(uni.unite(x[i],y[i]+h)){
         g[x[i]].push_back({y[i]+h,i});
         g[y[i]+h].push_back({x[i],i});
      }
      else{
         vector<int> res,add;
         int rt=uni.root(x[i]);
         dfs(rt,-1,x[i],res);
         dfs(rt,-1,y[i]+h,add);
         reverse(ALL(res));
         res.insert(res.end(),ALL(add));
         
         if(x[i]!=x[res.front()]){
            reverse(ALL(res));
            res.push_back(i);
            reverse(ALL(res));
         }
         else res.push_back(i);

         stack<int> st;
         rep(i,0,res.size()){
            if(!st.empty() and st.top()==res[i])st.pop();
            else st.push(res[i]);
         }
         res.clear();
         while(!st.empty()){
            res.push_back(st.top());
            st.pop();
         }
         reverse(ALL(res));
         
         cout<<res.size()<<'\n';
         rep(j,0,res.size())cout<<res[j]+1<<(j==res.size()-1?'\n':' ');
         return 0;
      }
   }
   puts("-1");
   return 0;
}
0