이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "islands.h"
#include <bits/stdc++.h>
#include <variant>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef pair<int,int> pi;
typedef vector<pi> pii;
typedef tuple<int,int,int> tii;
typedef vector<ll> li;
typedef vector<li> lii;
#define REP(i,a,b) for(int i=a;i<b;i++)
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define LSOne(s) ((s)&(-s))
ll INF=1e18+10;
int inf=1e9+10;
ll M=1e9+7;
int n,m;
vector<pii> arr;
vi vis,p,ans,ind,done;
void bfs(int x){
vis[x]=1;
int s=arr[x].size();
if(s==1){
ans.PB(-1);
return;
}
if(s==2){
int k=0;
if(vis[arr[x][k].F])k++;
ans.PB(arr[x][k].S);
bfs(arr[x][k].F);
if(ans.back()==-1)return;
ans.PB(arr[x][k].S);
return;
}
int k=0;
if(vis[arr[x][k].F])k++;
int z=arr[x][k].S;
int y=-1;
for(auto u:arr[arr[x][k].F])if(u.F==x)y=u.S;
k++;
while(vis[arr[x][k].F])k++;
int c=arr[x][k].S;
int d=-1;
for(auto u:arr[arr[x][k].F])if(u.F==x&&u.S!=y)d=u.S;
ans.PB(z);ans.PB(y);ans.PB(c);ans.PB(d);ans.PB(y);ans.PB(z);ans.PB(d);ans.PB(c);
return;
}
int dfs(int x){
vis[x]=1;ind.PB(x);
for(auto u:arr[x]){
if(done[u.F])continue;
if(vis[u.F]){
ind.PB(u.F);
return u.F;
}
int k=dfs(u.F);
if(k!=-1)return k;
}
ind.pop_back();
done[x]=1;
return -1;
}
std::variant<bool, std::vector<int>> find_journey(
int N, int M, std::vector<int> U, std::vector<int> V) {
n=N;m=M;
map<pi,int> mp,mp2;
REP(i,0,m){
mp[{U[i],V[i]}]=i;
i++;
mp2[{U[i],V[i]}]=i;
}
if(n==2){
if(m<=2)return false;
vi a;
REP(i,0,m)if(U[i]==0)a.PB(i);
int p=-1;
REP(i,0,m)if(U[i]!=0)p=i;
int k=a.size();
if(k<2||p==-1)return false;
ans.PB(a[0]);ans.PB(p);ans.PB(a[1]);ans.PB(a[0]);ans.PB(p);ans.PB(a[1]);
return ans;
}
if(m>=2&&U[0]==U[1]&&V[0]==V[1]){
arr.resize(n);
REP(i,0,m){
arr[U[i]].PB({V[i],i});
i++;
}
vis.resize(n,0);done.resize(n,0);
vis[0]=1;int k=-1;
ind.PB(0);
for(auto u:arr[0]){
k=dfs(u.F);
done[u.F]=1;
if(k!=-1)break;
}
if(k==-1)return false;
if(k==-1)cerr<<k<<" ";
int pos=0;
vi a;
while(ind[pos]!=k){
a.PB(mp[{ind[pos],ind[pos+1]}]);
ans.PB(mp[{ind[pos],ind[pos+1]}]);
pos++;
}
vi tmp;
int z=ind.size();
REP(i,pos,z-1)tmp.PB(ind[i]);
z=tmp.size();vi b,c;
REP(i,0,z-1){
b.PB(mp[{tmp[i],tmp[i+1]}]);
ans.PB(mp[{tmp[i],tmp[i+1]}]);
}
b.PB(mp[{tmp[z-1],tmp[0]}]);
ans.PB(mp[{tmp[z-1],tmp[0]}]);
REP(i,0,z-1){
c.PB(mp2[{tmp[i],tmp[i+1]}]);
ans.PB(mp2[{tmp[i],tmp[i+1]}]);
}
c.PB(mp2[{tmp[z-1],tmp[0]}]);
ans.PB(mp2[{tmp[z-1],tmp[0]}]);
reverse(b.begin(),b.end());
reverse(c.begin(),c.end());
for(auto u:b)ans.PB(u);
for(auto u:c)ans.PB(u);
reverse(a.begin(),a.end());
for(auto u:a)ans.PB(u);
return ans;
}
arr.resize(n);
REP(i,0,m)arr[U[i]].PB({V[i],i});
if(arr[0].size()>=2){
int x=arr[0][0].S;
int y=-1;
for(auto u:arr[arr[0][0].F])if(u.F==0)y=u.S;
int c=arr[0][1].S;
int d=-1;
for(auto u:arr[arr[0][1].F])if(u.F==0)d=u.S;
ans.PB(x);ans.PB(y);ans.PB(c);ans.PB(d);ans.PB(y);ans.PB(x);ans.PB(d);ans.PB(c);
return ans;
}
if(arr[0].empty())return false;
vis.resize(n,0);
vis[0]=1;
ans.PB(arr[0][0].S);
bfs(arr[0][0].F);
if(ans.back()==-1)return false;
ans.PB(arr[0][0].S);
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |