結果
| 問題 |
No.1647 Travel in Mitaru city 2
|
| コンテスト | |
| ユーザー |
蜜蜂
|
| 提出日時 | 2021-07-27 23:21:18 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 282 ms / 2,500 ms |
| コード長 | 2,090 bytes |
| コンパイル時間 | 4,158 ms |
| コンパイル使用メモリ | 239,396 KB |
| 実行使用メモリ | 36,960 KB |
| 最終ジャッジ日時 | 2024-10-13 03:13:09 |
| 合計ジャッジ時間 | 16,697 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 48 |
ソースコード
//g++ 1.cpp -std=c++14 -O2 -I .
#include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vld = vector<ld>;
using vvld = vector<vld>;
#define fi first
#define se second
#define pb push_back
#define pq_big(T) priority_queue<T,vector<T>,less<T>>
#define pq_small(T) priority_queue<T,vector<T>,greater<T>>
#define all(a) a.begin(),a.end()
#define rep(i,start,end) for(ll i=start;i<(ll)(end);i++)
#define per(i,start,end) for(ll i=start;i>=(ll)(end);i--)
vi seen(410000,0);
vi fin(410000,0);
vi hist;
int pos=-1;
void dfs(vvi &g,int now,int p){
seen[now]=1;
hist.pb(now);
for(int next:g[now]){
if(next==p){
continue;
}
if(fin[next]==1){
continue;
}
if(seen[next]==1&&fin[next]==0){
pos=next;
return;
}
dfs(g,next,now);
if(pos!=-1){
return;
}
}
hist.pop_back();
fin[now]=1;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int h,w,n;
cin>>h>>w>>n;
vvi g(h+w);
vector<pair<int,int>> place(n);
map<pair<int,int>,int> inv;
rep(i,0,n){
int x,y;
cin>>x>>y;
x--;y--;
place[i].fi=x,place[i].se=y;
g[x].pb(h+y);
g[h+y].pb(x);
inv[{x,h+y}]=i;
inv[{h+y,x}]=i;
}
rep(i,0,h+w){
if(seen[i]==1)continue;
if(g[i].size()==0)continue;
dfs(g,i,-1);
if(pos!=-1){
break;
}
}
if(pos==-1){
cout<<"-1\n";
return 0;
}
vi cycle;
while(!hist.empty()){
int t=hist[hist.size()-1];
cycle.pb(t);
hist.pop_back();
if(t==pos){
break;
}
}
vi ans;
rep(i,0,cycle.size()){
int now=cycle[i],next=cycle[(i+1)%cycle.size()];
ans.pb(inv[{now,next}]);
}
cout<<ans.size()<<"\n";
if(place[ans[0]].se==place[ans[1]].se){
rep(i,0,ans.size()){
cout<<ans[i]+1<<" ";
}
cout<<"\n";
}
else{
rep(i,1,ans.size()+1){
cout<<ans[i%ans.size()]+1<<" ";
}
cout<<"\n";
}
}
蜜蜂