Submission #1065550

# Submission time Handle Problem Language Result Execution time Memory
1065550 2024-08-19T09:10:12 Z deera Longest Trip (IOI23_longesttrip) C++17
15 / 100
7 ms 344 KB
#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;

vector<int> solve_d3(int N, int D) {
    vector<int> res(N);
    iota(res.begin(), res.end(), 0);
    return res;
}

vector<int> solve_d2(int N, int D) {
    queue<int> stops;
    for (int i = 1; i < N; i++) stops.push(i);

    deque<int> trip = {0};
    while (stops.size() > 1) {
        int next = stops.front(); stops.pop();
        int last = trip.back();
        
        if (are_connected({next}, {last})) {
            trip.push_back(next);
        } else {
            trip.push_back(stops.front()); stops.pop();
            trip.push_back(next);
        }
    }

    if (stops.size() == 1) {
        int next = stops.front();
        int last = trip.back();
        if (are_connected({next}, {last})) {
            trip.push_back(next);
        } else {
            trip.push_front(stops.front()); stops.pop();
        }
    }

    vector<int> res;
    for (int x : trip) res.push_back(x);
    return res;
}

vector<int> longest_trip(int N, int D) {
    if (D == 3) return solve_d3(N, D);
    if (D == 2) return solve_d2(N, D);


    vector<vector<int>> adj;
    for (int i = 0; i < N; i++) for (int j = i + 1; j < N; j++) {
        if (are_connected({i}, {j})) {
            adj[i].push_back(j);
            adj[j].push_back(i);
        }
    }

    vector<int> comp;
    vector<bool> visited(N, false);

    queue<int> q;
    q.push(0);
    visited[0] = true;

    while (!q.empty()) {
        int u = q.front(); q.pop();
        comp.push_back(u);
        for (int v : adj[u]) {
            if (!visited[v]) {
                visited[v] = true;
                q.push(v);
            }
        }
    }

    if (comp.size() != N) {
        if (comp.size() >= N - comp.size()) {
            return comp;
        } else {
            vector<int> res;
            for (int i = 0; i < N; i++) {
                if (!visited[i]) res.push_back(i);
            }
            return res;
        }
    }

    assert(comp.size() == N);

    vector<int> best_trip;
    deque<int> trip;
    vector<bool> used(N, false);

    for (int start: comp) {
        trip.clear();
        fill(used.begin(), used.end(), false);

        trip.push_back(start);
        used[start] = true;

        while (trip.size() < N) {
            bool done = false;
            if (done == false) for (int v: adj[trip.back()]) if (!used[v]) {
                trip.push_back(v);
                done = true;
                break;
            }

            if (done == false) for (int v: adj[trip.front()]) if (!used[v]) {
                trip.push_front(v);
                done = true;
                break;
            }

            if (done == false) break;
        }

        if (trip.size() == N) {
            vector<int> res;
            for (int x : trip) res.push_back(x);
            return res;
        }

        if (trip.size() > best_trip.size()) {
            best_trip.clear();
            for (int x : trip) best_trip.push_back(x);
        }
    }

    return best_trip;
}

Compilation message

longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:74:21: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   74 |     if (comp.size() != N) {
      |         ~~~~~~~~~~~~^~~~
In file included from /usr/include/c++/10/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
                 from longesttrip.cpp:2:
longesttrip.cpp:86:24: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   86 |     assert(comp.size() == N);
      |            ~~~~~~~~~~~~^~~~
longesttrip.cpp:99:28: warning: comparison of integer expressions of different signedness: 'std::deque<int, std::allocator<int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   99 |         while (trip.size() < N) {
      |                ~~~~~~~~~~~~^~~
longesttrip.cpp:116:25: warning: comparison of integer expressions of different signedness: 'std::deque<int, std::allocator<int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  116 |         if (trip.size() == N) {
      |             ~~~~~~~~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 344 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 344 KB Output is correct
2 Correct 5 ms 344 KB Output is correct
3 Correct 5 ms 344 KB Output is correct
4 Correct 5 ms 344 KB Output is correct
5 Correct 6 ms 344 KB Output is correct
6 Correct 6 ms 344 KB Output is correct
7 Correct 7 ms 344 KB Output is correct
8 Correct 5 ms 344 KB Output is correct
9 Correct 7 ms 344 KB Output is correct
10 Correct 5 ms 344 KB Output is correct
11 Correct 5 ms 344 KB Output is correct
12 Correct 6 ms 344 KB Output is correct
13 Correct 6 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 344 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 0 ms 344 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -