結果
問題 | No.1647 Travel in Mitaru city 2 |
ユーザー | 沙耶花 |
提出日時 | 2021-08-13 21:59:43 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 125 ms / 2,500 ms |
コード長 | 1,759 bytes |
コンパイル時間 | 4,796 ms |
コンパイル使用メモリ | 259,468 KB |
最終ジャッジ日時 | 2025-01-23 19:07:34 |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 48 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:42:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 42 | scanf("%d %d",&a,&b); | ~~~~~^~~~~~~~~~~~~~~
ソースコード
#include <stdio.h> #include <bits/stdc++.h> #include <atcoder/all> using namespace atcoder; using mint = modint998244353; using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i) #define Inf 1000000001 vector<int> ans; vector<bool> f; bool dfs(vector<vector<pair<int,int>>> &E,int cur,int p){ f[cur] = true; rep(i,E[cur].size()){ int to = E[cur][i].first; if(to==p)continue; if(f[to]){ ans.push_back(E[cur][i].second); return true; } if(dfs(E,to,cur)){ ans.push_back(E[cur][i].second); return true; } } return false; } int main(){ int H,W; cin>>H>>W; int N; cin>>N; dsu D(H+W); vector<int> x,y; rep(i,N){ int a,b; scanf("%d %d",&a,&b); a--;b--; a = a,b = H+b; x.push_back(a); y.push_back(b); if(D.same(a,b)){ for(int j=i+1;j<N;j++){ cin>>a>>b; } break; } D.merge(a,b); if(i==N-1){ cout<<-1<<endl; return 0; } } N = x.size(); vector<int> ind; rep(i,H+W){ if(D.same(i,x.back()))ind.push_back(i); } //cout<<ind.size()<<endl; vector<vector<pair<int,int>>> E(ind.size()); rep(i,N){ if(D.same(x[i],x.back())){ int a = distance(ind.begin(),lower_bound(ind.begin(),ind.end(),x[i])); int b = distance(ind.begin(),lower_bound(ind.begin(),ind.end(),y[i])); E[a].emplace_back(b,i); E[b].emplace_back(a,i); } } f.resize(ind.size(),false); dfs(E,0,-1); dsu DD(H+W); vector<int> Ans; rep(i,ans.size()){ Ans.push_back(ans[i]); int a = x[ans[i]],b = y[ans[i]]; if(DD.same(a,b)){ break; } DD.merge(a,b); } if(x[Ans[0]]==x[Ans[1]]){ int t = Ans.back(); Ans.pop_back(); Ans.insert(Ans.begin(),t); } cout<<Ans.size()<<endl; rep(i,Ans.size()){ if(i!=0)cout<<' '; cout<<Ans[i]+1; } cout<<endl; return 0; }