Submission #744372

#TimeUsernameProblemLanguageResultExecution timeMemory
744372kwongwengThousands Islands (IOI22_islands)C++17
5 / 100
35 ms8168 KiB
#include "islands.h"
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long double ld;
typedef pair<ll, ll> pll;
#define FOR(i, a, b) for(int i = a; i < b; i++)
#define ROF(i, a, b) for(int i = a; i >= b; i--)
#define ms memset
#define pb push_back
#define fi first
#define se second

int mat[1000][1000][2];

variant<bool, vi> find_journey(int N, int M, vi U, vi V) {
  if (N == 2){
    vi e0, e1;
    FOR(i,0,M){
      if (U[i]==0) e0.pb(i);
      else e1.pb(i);
    }
    if (e0.size() < 2 || e1.size() < 1) return false;
    return vi({e0[0], e1[0], e0[1], e0[0], e1[0], e0[1]});
  }
  /*
  if (N <= 400 && M == N*(N-1)){
    vii p = {{0,1},{1,2},{2,0},{0,2},{2,1},{1,0}};
    vi l(6);
    FOR(i,0,M){
      FOR(j,0,6){
        if (p[j] == ii({U[i],V[i]})){
          l[j] = i;
        }
      }
    }
    return vi({l[0],l[1],l[2],l[3],l[4],l[5],l[2],l[1],l[0],l[5],l[4],l[3]});
  }
  */
  if (N <= 1000 && U[0] != U[1]){
    ms(mat, -1, sizeof(mat));
    FOR(i,0,M){
      // labelling edges
      int pos = 0;
      if (mat[U[i]][V[i]][0] != -1) pos = 1;
      mat[U[i]][V[i]][pos] = i;
    }
    vi p(N);
    vi bfs; bfs.pb(0);
    vi used(N); used[0] = 1;
    FOR(k,0,bfs.size()){
      int u = bfs[k];
      FOR(i,0,N){
        if (mat[u][i][0] == -1) continue;
        if (used[i] && p[u] != i && p[i] != u){
          // cycle found
          vi paru; int v = u;
          while (v != 0){
            paru.pb(v);
            v = p[v];
          }
          paru.pb(0);
          vi pari; v = i;
          while (v != 0){
            pari.pb(v); v = p[v];
          }
          pari.pb(0);
          reverse(paru.begin(),paru.end());
          reverse(pari.begin(),pari.end());
          int a = 0;
          vi ans;
          //path from 0 to cycle
          FOR(j,1,min(paru.size(),pari.size())){
            if (paru[j] == pari[j]){
              ans.pb(mat[paru[j-1]][paru[j]][0]);
              a=j;
            }else{
              break;
            }
          }
          //first loop
          FOR(j,a+1,paru.size()){
            ans.pb(mat[paru[j-1]][paru[j]][0]);
          }
          ans.pb(mat[u][i][0]);
          ROF(j,pari.size()-1,a+1){
            ans.pb(mat[pari[j]][pari[j-1]][0]);
          }
          //second loop
          FOR(j,a+1,pari.size()){
            ans.pb(mat[pari[j-1]][pari[j]][0]);
          }
          ans.pb(mat[i][u][0]);
          ROF(j,paru.size()-1,a+1){
            ans.pb(mat[paru[j]][paru[j-1]][0]);
          }
          //reverse 1st loop
          FOR(j,a+1,pari.size()){
            ans.pb(mat[pari[j]][pari[j-1]][0]);
          }
          ans.pb(mat[u][i][0]);
          ROF(j,paru.size()-1,a+1){
            ans.pb(mat[paru[j-1]][paru[j]][0]);
          }
          //reverse 2nd loop
          FOR(j,a+1,paru.size()){
            ans.pb(mat[paru[j]][paru[j-1]][0]);
          }
          ans.pb(mat[i][u][0]);
          ROF(j,pari.size()-1,a+1){
            ans.pb(mat[pari[j-1]][pari[j]][0]);
          }
          //path back to 0
          ROF(j,a,1){
            ans.pb(mat[paru[j]][paru[j-1]][0]);
          }
          return ans;
        }
        if (mat[u][i][1] != -1){
          // double edge found, solution from Subtask 1
          vi par; int v = u;
          while (v != 0){
            par.pb(v);
            v = p[v];
          }
          par.pb(0);
          reverse(par.begin(), par.end());
          vi ans;
          FOR(j,0,par.size()-1){
            ans.pb(mat[par[j]][par[j+1]][0]); //path 0 -> u
          }
          // e0[0], e1[0], e0[1], e0[0], e1[0], e0[1]
          vi path = {mat[u][i][0], mat[i][u][0], mat[u][i][1], mat[u][i][0], mat[i][u][0], mat[u][i][1]};
          FOR(j,0,6) ans.pb(path[j]);
          ROF(j,par.size()-2,0){
            ans.pb(mat[par[j]][par[j+1]][0]); //path u -> 0
          }
          return ans;
        }
        if (used[i]) continue;
        used[i] = 1; p[i] = u;
        bfs.pb(i);
      }
    }
  }
  return false;
}

Compilation message (stderr)

islands.cpp: In function 'std::variant<bool, std::vector<int, std::allocator<int> > > find_journey(int, int, vi, vi)':
islands.cpp:11:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   11 | #define FOR(i, a, b) for(int i = a; i < b; i++)
......
   55 |     FOR(k,0,bfs.size()){
      |         ~~~~~~~~~~~~~~                 
islands.cpp:55:5: note: in expansion of macro 'FOR'
   55 |     FOR(k,0,bfs.size()){
      |     ^~~
islands.cpp:11:39: warning: comparison of integer expressions of different signedness: 'int' and 'const long unsigned int' [-Wsign-compare]
   11 | #define FOR(i, a, b) for(int i = a; i < b; i++)
......
   77 |           FOR(j,1,min(paru.size(),pari.size())){
      |               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
islands.cpp:77:11: note: in expansion of macro 'FOR'
   77 |           FOR(j,1,min(paru.size(),pari.size())){
      |           ^~~
islands.cpp:11:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   11 | #define FOR(i, a, b) for(int i = a; i < b; i++)
......
   86 |           FOR(j,a+1,paru.size()){
      |               ~~~~~~~~~~~~~~~~~        
islands.cpp:86:11: note: in expansion of macro 'FOR'
   86 |           FOR(j,a+1,paru.size()){
      |           ^~~
islands.cpp:11:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   11 | #define FOR(i, a, b) for(int i = a; i < b; i++)
......
   94 |           FOR(j,a+1,pari.size()){
      |               ~~~~~~~~~~~~~~~~~        
islands.cpp:94:11: note: in expansion of macro 'FOR'
   94 |           FOR(j,a+1,pari.size()){
      |           ^~~
islands.cpp:11:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   11 | #define FOR(i, a, b) for(int i = a; i < b; i++)
......
  102 |           FOR(j,a+1,pari.size()){
      |               ~~~~~~~~~~~~~~~~~        
islands.cpp:102:11: note: in expansion of macro 'FOR'
  102 |           FOR(j,a+1,pari.size()){
      |           ^~~
islands.cpp:11:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   11 | #define FOR(i, a, b) for(int i = a; i < b; i++)
......
  110 |           FOR(j,a+1,paru.size()){
      |               ~~~~~~~~~~~~~~~~~        
islands.cpp:110:11: note: in expansion of macro 'FOR'
  110 |           FOR(j,a+1,paru.size()){
      |           ^~~
islands.cpp:11:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   11 | #define FOR(i, a, b) for(int i = a; i < b; i++)
......
  133 |           FOR(j,0,par.size()-1){
      |               ~~~~~~~~~~~~~~~~         
islands.cpp:133:11: note: in expansion of macro 'FOR'
  133 |           FOR(j,0,par.size()-1){
      |           ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...