結果

問題 No.1647 Travel in Mitaru city 2
ユーザー tko919tko919
提出日時 2021-08-24 00:15:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,003 bytes
コンパイル時間 2,077 ms
コンパイル使用メモリ 204,604 KB
実行使用メモリ 19,328 KB
最終ジャッジ日時 2024-04-25 18:17:10
合計ジャッジ時間 10,797 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
8,248 KB
testcase_01 AC 4 ms
8,468 KB
testcase_02 AC 3 ms
8,384 KB
testcase_03 WA -
testcase_04 AC 4 ms
8,148 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 5 ms
8,484 KB
testcase_08 AC 94 ms
17,152 KB
testcase_09 AC 90 ms
15,776 KB
testcase_10 AC 96 ms
16,512 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 94 ms
17,784 KB
testcase_15 AC 96 ms
16,912 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 83 ms
14,916 KB
testcase_21 AC 89 ms
14,476 KB
testcase_22 AC 95 ms
18,644 KB
testcase_23 AC 99 ms
17,920 KB
testcase_24 AC 91 ms
15,900 KB
testcase_25 AC 90 ms
16,048 KB
testcase_26 AC 94 ms
18,432 KB
testcase_27 AC 100 ms
18,600 KB
testcase_28 AC 96 ms
18,780 KB
testcase_29 AC 97 ms
17,408 KB
testcase_30 AC 86 ms
14,032 KB
testcase_31 AC 104 ms
19,072 KB
testcase_32 AC 94 ms
16,696 KB
testcase_33 WA -
testcase_34 AC 101 ms
17,388 KB
testcase_35 WA -
testcase_36 AC 98 ms
19,328 KB
testcase_37 AC 102 ms
18,304 KB
testcase_38 AC 90 ms
15,608 KB
testcase_39 AC 84 ms
15,396 KB
testcase_40 AC 91 ms
15,804 KB
testcase_41 AC 91 ms
16,208 KB
testcase_42 AC 90 ms
15,812 KB
testcase_43 AC 3 ms
8,412 KB
testcase_44 AC 3 ms
8,700 KB
testcase_45 WA -
testcase_46 AC 81 ms
13,796 KB
testcase_47 WA -
testcase_48 AC 71 ms
12,264 KB
testcase_49 WA -
testcase_50 AC 72 ms
12,784 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);
         
         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