Submission #1088000

#TimeUsernameProblemLanguageResultExecution timeMemory
1088000Valaki2Longest Trip (IOI23_longesttrip)C++17
60 / 100
892 ms1112 KiB
#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;

#define pb push_back

const int maxn = 256;

int n;
int edge[maxn][maxn];

void getedges() {
    for(int i = 0; i < n; i++) {
        for(int j = i + 1; j < n; j++) {
            int val = are_connected({i}, {j});
            edge[i][j] = edge[j][i] = val;
        }
    }
}

bool vis[maxn];

void reset() {
    for(int i = 0; i < n; i++) {
        vis[i] = false;
        for(int j = 0; j < n; j++) {
            edge[i][j] = 0;
        }
    }
}

void insert_at(vector<int> &path, int x, int y) {
    /*vis[y] = true;
    path.pb(-1);
    for(int i = path.size(); i >= 1; i--) {
        if(path[i - 1] == x) {
            path[i] = y;
            break;
        } else {
            path[i] = path[i - 1];
        }
    }*/
    vis[y] = true;
    vector<int> a, b;
    for(int i = 0; i < path.size(); i++) {
        if(path[i] == x) {
            a = path;
            a.resize(i + 1);
            a.pb(y);
            for(int j = i + 1; j < path.size(); j++) {
                b.pb(path[j]);
            }
            break;
        }
    }
    path.clear();
    for(int cur : b) {
        path.pb(cur);
    }
    for(int cur : a) {
        path.pb(cur);
    }
}

bool iterate(vector<int> &path) {
    for(int i = 0; i < n; i++) {
        if(vis[i]) {
            continue;
        }
        if(edge[i][path[0]]) {
            reverse(path.begin(), path.end());
            path.pb(i);
            vis[i] = true;
            reverse(path.begin(), path.end());
            return true;
        }
        if(edge[i][path.back()]) {
            path.pb(i);
            vis[i] = true;
            return true;
        }
    }
    for(int x : path) {
        for(int y = 0; y < n; y++) {
            if(vis[y]) {
                continue;
            }
            if(edge[x][y]) {
                insert_at(path, x, y);
                return true;
            }
        }
    }
    return false;
}

vector<int> longest_trip(int N, int D){
    reset();
    n = N;
    getedges();
    vector<int> path = {0};
    vis[0] = true;
    while(path.size() < n) {
        bool isgood = iterate(path);
        if(!isgood) {
            if(path.size() > n - path.size()) {
                return path;
            } else {
                path.clear();
                for(int i = 0; i < n; i++) {
                    if(!vis[i]) {
                        path.pb(i);
                    }
                }
                return path;
            }
        }
    }
    return path;
}

Compilation message (stderr)

longesttrip.cpp: In function 'void insert_at(std::vector<int>&, int, int)':
longesttrip.cpp:45:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |     for(int i = 0; i < path.size(); i++) {
      |                    ~~^~~~~~~~~~~~~
longesttrip.cpp:50:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |             for(int j = i + 1; j < path.size(); j++) {
      |                                ~~^~~~~~~~~~~~~
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:103:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  103 |     while(path.size() < n) {
      |           ~~~~~~~~~~~~^~~
#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...