Submission #846584

#TimeUsernameProblemLanguageResultExecution timeMemory
846584math_rabbit_1028Longest Trip (IOI23_longesttrip)C++17
70 / 100
68 ms1768 KiB
#include <bits/stdc++.h>
#include "longesttrip.h"
using namespace std;

int find_connected(int v, vector<int> arr) {
    if (arr.size() == 0 || !are_connected({v}, arr)) return -1;

    int s = 0, e = arr.size() - 1;
    while (s < e) {
        int m = (s + e) / 2;
        vector<int> temp;
        for (int i = s; i <= m; i++) temp.push_back(arr[i]);
        if (are_connected({v}, temp)) e = m;
        else s = m + 1;
    }
    return arr[s];
}

vector<int> longest_trip(int N, int D) {
    int n = N; 
    vector<int> res;

    int ch[256]; for (int i = 0; i < n; i++) ch[i] = 0;
    vector<int> path, clique;
    path.push_back(0);
    ch[0] = 1;
    while (true) {
        vector<int> temp;
        for (int i = 0; i < n; i++) if (ch[i] == 0) temp.push_back(i);
        int v = find_connected(path.back(), temp);
        if (v == -1) break;
        path.push_back(v);
        ch[v] = 1;
    }
    for (int i = 0; i < n; i++) if (ch[i] == 0) clique.push_back(i);

    bool connected = false;
    if (path.size() > 0 && clique.size() > 0) connected = are_connected(path, clique);
    if (connected) {
        int target = find_connected(path[0], clique);
        if (target != -1) {
            res = path;
            reverse(res.begin(), res.end());
            int t = 0;
            for (t = 0; t < clique.size(); t++) if (clique[t] == target) break;
            swap(clique[0], clique[t]);                    
            for (int j = 0; j < clique.size(); j++) res.push_back(clique[j]);
            return res;
        }
        int s = path.size() - 1;
        target = find_connected(path[s], clique);
        if (target != -1) {
            res = path;
            int t = 0;
            for (t = 0; t < clique.size(); t++) if (clique[t] == target) break;
            swap(clique[0], clique[t]);
            for (int j = 0; j < clique.size(); j++) res.push_back(clique[j]);
            return res;
        }

        for (int i = 0; i < clique.size(); i++) {
            target = find_connected(clique[i], path);
            if (target == -1) continue;
            swap(clique[i], clique.back());
            res = clique;
            while (path[0] != target) {
                int front = path[0];
                path.erase(path.begin());
                path.push_back(front);
            }
            for (int k = 0; k < path.size(); k++) res.push_back(path[k]);
            return res;
        }
    }
    else {
        if (path.size() > clique.size()) return path;
        return clique;
    }
    return {}; // never reached
}

Compilation message (stderr)

longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:45:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |             for (t = 0; t < clique.size(); t++) if (clique[t] == target) break;
      |                         ~~^~~~~~~~~~~~~~~
longesttrip.cpp:47:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |             for (int j = 0; j < clique.size(); j++) res.push_back(clique[j]);
      |                             ~~^~~~~~~~~~~~~~~
longesttrip.cpp:55:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |             for (t = 0; t < clique.size(); t++) if (clique[t] == target) break;
      |                         ~~^~~~~~~~~~~~~~~
longesttrip.cpp:57:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |             for (int j = 0; j < clique.size(); j++) res.push_back(clique[j]);
      |                             ~~^~~~~~~~~~~~~~~
longesttrip.cpp:61:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |         for (int i = 0; i < clique.size(); i++) {
      |                         ~~^~~~~~~~~~~~~~~
longesttrip.cpp:71:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |             for (int k = 0; k < path.size(); k++) res.push_back(path[k]);
      |                             ~~^~~~~~~~~~~~~
#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...