Submission #1024858

# Submission time Handle Problem Language Result Execution time Memory
1024858 2024-07-16T11:15:58 Z Svizel_pritula Longest Trip (IOI23_longesttrip) C++17
40 / 100
216 ms 436 KB
#include <bits/stdc++.h>

#include "longesttrip.h"

std::vector<int> longest_trip(int cities, int density)
{
    std::vector<int> path;
    path.push_back(0);

    std::vector<int> unconnected;
    for (int i = 1; i < cities; i++)
        unconnected.push_back(i);

    while (unconnected.size() > 0)
    {
        bool found = false;

        for (auto p = unconnected.begin(); p < unconnected.end(); p++)
        {
            if (are_connected({path.back()}, {*p}))
            {
                path.push_back(*p);
                unconnected.erase(p);
                found = true;
                break;
            }
        }

        if (!found)
        {
            break;
        }
    }

    if (unconnected.size() == 0)
        return path;

    int max_dist = std::min((path.size() + 1) / 2, unconnected.size());
    int join_point = -1;

    for (int dist = 0; dist <= max_dist; dist++)
    {
        int a = dist;
        int b = path.size() - dist - 1;

        if (are_connected(unconnected, {path[a]}))
        {
            join_point = a;
            break;
        }

        if (are_connected(unconnected, {path[b]}))
        {
            join_point = b;
            break;
        }
    }

    if (join_point == -1)
    {
        if (path.size() > unconnected.size())
            return path;
        else
            return unconnected;
    }

    int opposite_node = -1;

    for (int node : unconnected)
    {
        if (are_connected({node}, {path[join_point]}))
        {
            opposite_node = node;
            break;
        }
    }

    std::vector<int> result;

    if (join_point >= path.size() / 2)
    {
        for (int i = 0; i <= join_point; i++)
            result.push_back(path[i]);
    }
    else
    {
        for (int i = path.size() - 1; i >= join_point; i--)
            result.push_back(path[i]);
    }

    result.push_back(opposite_node);

    for (int node : unconnected)
        if (node != opposite_node)
            result.push_back(node);

    return result;
}

Compilation message

longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:80:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   80 |     if (join_point >= path.size() / 2)
      |         ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 2 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 344 KB Output is correct
2 Correct 4 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 7 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 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 4 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 5 ms 344 KB Output is correct
8 Correct 6 ms 344 KB Output is correct
9 Correct 5 ms 344 KB Output is correct
10 Correct 4 ms 344 KB Output is correct
11 Correct 5 ms 344 KB Output is correct
12 Correct 4 ms 344 KB Output is correct
13 Correct 4 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 4 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 4 ms 344 KB Output is correct
9 Correct 4 ms 344 KB Output is correct
10 Correct 4 ms 344 KB Output is correct
11 Correct 5 ms 344 KB Output is correct
12 Correct 5 ms 344 KB Output is correct
13 Correct 5 ms 344 KB Output is correct
14 Correct 10 ms 344 KB Output is correct
15 Correct 9 ms 344 KB Output is correct
16 Correct 7 ms 344 KB Output is correct
17 Correct 6 ms 344 KB Output is correct
18 Correct 6 ms 344 KB Output is correct
19 Correct 6 ms 432 KB Output is correct
20 Correct 5 ms 436 KB Output is correct
21 Correct 5 ms 344 KB Output is correct
22 Correct 7 ms 424 KB Output is correct
23 Correct 7 ms 344 KB Output is correct
24 Correct 5 ms 344 KB Output is correct
25 Correct 9 ms 344 KB Output is correct
26 Correct 12 ms 344 KB Output is correct
27 Correct 5 ms 344 KB Output is correct
28 Correct 9 ms 344 KB Output is correct
29 Correct 10 ms 344 KB Output is correct
30 Correct 8 ms 344 KB Output is correct
31 Correct 37 ms 344 KB Output is correct
32 Correct 49 ms 344 KB Output is correct
33 Correct 9 ms 436 KB Output is correct
34 Correct 13 ms 344 KB Output is correct
35 Correct 25 ms 344 KB Output is correct
36 Correct 5 ms 344 KB Output is correct
37 Correct 29 ms 344 KB Output is correct
38 Correct 90 ms 344 KB Output is correct
39 Correct 171 ms 344 KB Output is correct
40 Correct 166 ms 344 KB Output is correct
41 Correct 179 ms 344 KB Output is correct
42 Correct 212 ms 344 KB Output is correct
43 Correct 216 ms 340 KB Output is correct
44 Correct 197 ms 340 KB Output is correct
45 Correct 7 ms 344 KB Output is correct
46 Correct 7 ms 344 KB Output is correct
47 Correct 8 ms 344 KB Output is correct
48 Correct 7 ms 344 KB Output is correct
49 Correct 7 ms 344 KB Output is correct
50 Correct 6 ms 344 KB Output is correct
51 Correct 24 ms 344 KB Output is correct
52 Correct 32 ms 344 KB Output is correct
53 Correct 4 ms 344 KB Output is correct
54 Correct 7 ms 344 KB Output is correct
55 Correct 12 ms 344 KB Output is correct
56 Correct 7 ms 344 KB Output is correct
57 Correct 31 ms 344 KB Output is correct
58 Correct 83 ms 344 KB Output is correct
59 Correct 148 ms 344 KB Output is correct
60 Correct 151 ms 344 KB Output is correct
61 Correct 177 ms 344 KB Output is correct
62 Correct 142 ms 344 KB Output is correct
63 Correct 189 ms 344 KB Output is correct
64 Correct 199 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 344 KB Output is correct
2 Correct 5 ms 344 KB Output is correct
3 Correct 6 ms 344 KB Output is correct
4 Correct 5 ms 344 KB Output is correct
5 Correct 6 ms 340 KB Output is correct
6 Correct 11 ms 344 KB Output is correct
7 Correct 8 ms 344 KB Output is correct
8 Correct 4 ms 344 KB Output is correct
9 Correct 6 ms 344 KB Output is correct
10 Correct 5 ms 344 KB Output is correct
11 Correct 6 ms 344 KB Output is correct
12 Correct 4 ms 344 KB Output is correct
13 Correct 7 ms 344 KB Output is correct
14 Correct 15 ms 344 KB Output is correct
15 Correct 12 ms 344 KB Output is correct
16 Incorrect 0 ms 344 KB Incorrect
17 Halted 0 ms 0 KB -