結果
| 問題 |
No.1647 Travel in Mitaru city 2
|
| コンテスト | |
| ユーザー |
たたき@競プロ
|
| 提出日時 | 2023-08-30 06:55:56 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 374 ms / 2,500 ms |
| コード長 | 1,287 bytes |
| コンパイル時間 | 6,320 ms |
| コンパイル使用メモリ | 317,024 KB |
| 実行使用メモリ | 35,204 KB |
| 最終ジャッジ日時 | 2024-12-31 20:05:59 |
| 合計ジャッジ時間 | 22,194 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 48 |
ソースコード
#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<<' ';
}
}
たたき@競プロ