結果
問題 |
No.1647 Travel in Mitaru city 2
|
ユーザー |
![]() |
提出日時 | 2021-08-29 20:52:03 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 136 ms / 2,500 ms |
コード長 | 1,424 bytes |
コンパイル時間 | 3,825 ms |
コンパイル使用メモリ | 234,296 KB |
実行使用メモリ | 22,668 KB |
最終ジャッジ日時 | 2024-11-22 08:36:11 |
合計ジャッジ時間 | 12,794 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 48 |
ソースコード
#include <bits/stdc++.h> using namespace std; #include <atcoder/all> using namespace atcoder; using ll=long long; #define MOD 998244353 using Graph=vector<vector<pair<int,int>>>; #define INF 1000000000 #define MAX 200000 vector<int> ans; bool dfs(Graph &G,int v,int p,vector<int> &seen,int &g){ seen[v]=1; for(auto e:G[v]){ int nv=e.first; if(nv==p){ continue; } if(seen[nv]==1){ g=nv; ans.push_back(e.second); return true; }else{ if(dfs(G,nv,v,seen,g)){ if(g!=-1){ ans.push_back(e.second); if(g==v){ g=-1; } } return true; } } } return false; } int main(){ int H,W,N; cin>>H>>W>>N; vector<int> x(N),y(N); Graph G(H+W); for(int i=0;i<N;i++){ cin>>x[i]>>y[i]; x[i]--;y[i]--; G[x[i]].push_back(make_pair(H+y[i],i)); G[H+y[i]].push_back(make_pair(x[i],i)); } vector<int> seen(H+W,0); for(int i=0;i<H+W;i++){ if(seen[i]==0){ int g=-1; if(dfs(G,i,-1,seen,g)){ cout<<ans.size()<<'\n'; if(y[ans[0]]==y[ans[1]]){ for(int j=0;j<ans.size();j++){ cout<<ans[j]+1<<' '; } cout<<'\n'; }else{ for(int j=0;j<ans.size();j++){ cout<<ans[(j+1)%(int)ans.size()]+1<<' '; } cout<<'\n'; } return 0; } } } cout<<-1<<'\n'; }