Submission #1057106

#TimeUsernameProblemLanguageResultExecution timeMemory
1057106MalixThousands Islands (IOI22_islands)C++17
Compilation error
0 ms0 KiB
#include "islands.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;

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(p[x]==u.F)continue;
    if(vis[u.F]){
      ind.PB(u.F);
      return u.F;
    }
    p[u.F]=x;
    int k=dfs(u.F);
    if(k!=-1)return k;
  }
  ind.pop_back();
  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;
  }
  arr.resize(n);
  REP(i,0,m)arr[U[i]].PB({V[i],i});

if(U[0]==U[1]){
  vis.resize(n,0);
  p.resize(n,-1);
  vis[0]=1;int k=-1;
  ind.PB(0);
  for(auto u:arr[0]){
    p[u]=0;ind.PB(u.F);
    k=dfs(u);
    if(k!=-1)break;
    ind.pop_back();
  }
  if(k==-1)return false;
  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]}]);
  }
  REP(i,0,z-1){
    c.PB(mp2[{tmp[i],tmp[i+1]}]);
    ans.PB(mp2[{tmp[i],tmp[i+1]}]);
  }
  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;
}

  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;
}

Compilation message (stderr)

islands.cpp: In function 'std::variant<bool, std::vector<int, std::allocator<int> > > find_journey(int, int, std::vector<int>, std::vector<int>)':
islands.cpp:79:3: error: 'map' was not declared in this scope
   79 |   map<pi,int> mp,mp2;
      |   ^~~
islands.cpp:2:1: note: 'std::map' is defined in header '<map>'; did you forget to '#include <map>'?
    1 | #include "islands.h"
  +++ |+#include <map>
    2 | 
islands.cpp:79:9: error: expected primary-expression before ',' token
   79 |   map<pi,int> mp,mp2;
      |         ^
islands.cpp:79:10: error: expected primary-expression before 'int'
   79 |   map<pi,int> mp,mp2;
      |          ^~~
islands.cpp:81:5: error: 'mp' was not declared in this scope; did you mean 'p'?
   81 |     mp[{U[i],V[i]}]=i;
      |     ^~
      |     p
islands.cpp:83:5: error: 'mp2' was not declared in this scope
   83 |     mp2[{U[i],V[i]}]=i;
      |     ^~~
islands.cpp:105:6: error: no match for 'operator[]' (operand types are 'vi' {aka 'std::vector<int>'} and 'std::pair<int, int>')
  105 |     p[u]=0;ind.PB(u.F);
      |      ^
In file included from /usr/include/c++/10/vector:67,
                 from islands.h:2,
                 from islands.cpp:1:
/usr/include/c++/10/bits/stl_vector.h:1043:7: note: candidate: 'std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](std::vector<_Tp, _Alloc>::size_type) [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::reference = int&; std::vector<_Tp, _Alloc>::size_type = long unsigned int]'
 1043 |       operator[](size_type __n) _GLIBCXX_NOEXCEPT
      |       ^~~~~~~~
/usr/include/c++/10/bits/stl_vector.h:1043:28: note:   no known conversion for argument 1 from 'std::pair<int, int>' to 'std::vector<int>::size_type' {aka 'long unsigned int'}
 1043 |       operator[](size_type __n) _GLIBCXX_NOEXCEPT
      |                  ~~~~~~~~~~^~~
/usr/include/c++/10/bits/stl_vector.h:1061:7: note: candidate: 'std::vector<_Tp, _Alloc>::const_reference std::vector<_Tp, _Alloc>::operator[](std::vector<_Tp, _Alloc>::size_type) const [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::const_reference = const int&; std::vector<_Tp, _Alloc>::size_type = long unsigned int]'
 1061 |       operator[](size_type __n) const _GLIBCXX_NOEXCEPT
      |       ^~~~~~~~
/usr/include/c++/10/bits/stl_vector.h:1061:28: note:   no known conversion for argument 1 from 'std::pair<int, int>' to 'std::vector<int>::size_type' {aka 'long unsigned int'}
 1061 |       operator[](size_type __n) const _GLIBCXX_NOEXCEPT
      |                  ~~~~~~~~~~^~~
islands.cpp:106:11: error: cannot convert 'std::pair<int, int>' to 'int'
  106 |     k=dfs(u);
      |           ^
      |           |
      |           std::pair<int, int>
islands.cpp:60:13: note:   initializing argument 1 of 'int dfs(int)'
   60 | int dfs(int x){
      |         ~~~~^
islands.cpp:114:10: error: 'mp' was not declared in this scope; did you mean 'p'?
  114 |     a.PB(mp[{ind[pos],ind[pos+1]}]);
      |          ^~
      |          p
islands.cpp:123:10: error: 'mp' was not declared in this scope; did you mean 'tmp'?
  123 |     b.PB(mp[{tmp[i],tmp[i+1]}]);
      |          ^~
      |          tmp
islands.cpp:127:10: error: 'mp2' was not declared in this scope
  127 |     c.PB(mp2[{tmp[i],tmp[i+1]}]);
      |          ^~~
islands.cpp:130:3: error: 'reverse' was not declared in this scope
  130 |   reverse(b.begin,b.end());
      |   ^~~~~~~