This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "islands.h"
#include <variant>
#include <iostream>
#include <vector>
#include <algorithm>
#include <tuple>
#include <map>
using namespace std;
int n,m;
vector<int>u,v;
variant<bool,vector<int>>sub1(){
vector<int>c1,c2;
for(int i=0;i<m;i++){
if(u[i]==0) c1.push_back(i);
else c2.push_back(i);
}
if(c1.size()<2 || c2.size()<1) return false;
return vector<int>({c1[0],c2[0],c1[1],c1[0],c2[0],c1[1]});
}
vector<int>sub2(){
vector<vector<int>>a(3,vector<int>(3));
for(int i=0;i<m;i++){
if(u[i]<3 && v[i]<3) a[u[i]][v[i]]=i;
}
return vector<int>({a[0][1],a[1][0],a[0][2],a[2][1],a[0][1],a[1][0],a[2][1],a[0][2]});
}
#define ff first
#define ss second
vector<vector<pair<int,int>>>adj;
vector<int>vis,cf,cycle;
bool found;
vector<map<int,vector<int>>>b;
variant<bool,vector<int>>dfs(int x){
vis[x]=true;
if(x==0 && adj[x].size()>1 || (adj[x].size()>2)){
auto y=adj[x][0],z=adj[x][1];
if(adj[x].size()>2) y=adj[x][2];
vector<int>asn={y.ss,y.ss^1,z.ss,z.ss^1,y.ss^1,y.ss,z.ss^1,z.ss};
return asn;
}
for(auto [z,c]:adj[x]){
if(vis[z]) continue;
variant<bool,vector<int>>res=dfs(z);
if(res.index()==0) return false;
vector<int>sm=get<vector<int>>(res);
vector<int>asn={c};
for(int h:sm) asn.push_back(h);
asn.push_back(c);
return asn;
}
return false;
}
variant<bool,vector<int>>sub3(){
adj.resize(n);
b.resize(n);
for(int i=0;i<m;i++){
adj[u[i]].push_back({v[i],i});
b[u[i]][v[i]].push_back(i);
}
vis.assign(n,0);
found=false;
cf.resize(n);
return dfs(0);
}
std::variant<bool, std::vector<int>> find_journey(int N, int M, std::vector<int> U, std::vector<int> V) {
tie(n,m,u,v)=tie(N,M,U,V);
if(n==2) return sub1();
return sub3();
}
/*
4 8
0 1
1 0
1 2
2 1
2 3
3 2
0 3
3 0
*/
Compilation message (stderr)
islands.cpp: In function 'std::variant<bool, std::vector<int, std::allocator<int> > > dfs(int)':
islands.cpp:43:13: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
43 | if(x==0 && adj[x].size()>1 || (adj[x].size()>2)){
| ~~~~~^~~~~~~~~~~~~~~~~~
# | 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... |