Submission #1065579

# Submission time Handle Problem Language Result Execution time Memory
1065579 2024-08-19T09:33:59 Z deera Longest Trip (IOI23_longesttrip) C++17
60 / 100
930 ms 1740 KB
#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;

// int MAT[1000][1000];
// bool are_connected(vector<int> A, vector<int> B) {
//     assert(A.size() == 1 && B.size() == 1);
//     int a = A[0], b = B[0];
//     return MAT[a][b];
// }

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(N);
    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);
                used[v] = true;
                done = true;
                break;
            }

            if (done == false) for (int v: adj[trip.front()]) if (!used[v]) {
                trip.push_front(v);
                used[v] = true;
                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:79:21: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   79 |     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:91:24: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   91 |     assert(comp.size() == N);
      |            ~~~~~~~~~~~~^~~~
longesttrip.cpp:104: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]
  104 |         while (trip.size() < N) {
      |                ~~~~~~~~~~~~^~~
longesttrip.cpp:123: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]
  123 |         if (trip.size() == N) {
      |             ~~~~~~~~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 197 ms 1012 KB Output is correct
# 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 1 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 6 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 4 ms 344 KB Output is correct
7 Correct 5 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 6 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 5 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 31 ms 344 KB Output is correct
3 Correct 156 ms 600 KB Output is correct
4 Correct 400 ms 488 KB Output is correct
5 Correct 853 ms 940 KB Output is correct
6 Correct 9 ms 344 KB Output is correct
7 Correct 24 ms 344 KB Output is correct
8 Correct 146 ms 344 KB Output is correct
9 Correct 297 ms 1360 KB Output is correct
10 Correct 853 ms 976 KB Output is correct
11 Correct 823 ms 716 KB Output is correct
12 Correct 820 ms 1072 KB Output is correct
13 Correct 815 ms 944 KB Output is correct
14 Correct 6 ms 344 KB Output is correct
15 Correct 10 ms 344 KB Output is correct
16 Correct 37 ms 344 KB Output is correct
17 Correct 90 ms 344 KB Output is correct
18 Correct 131 ms 440 KB Output is correct
19 Correct 339 ms 484 KB Output is correct
20 Correct 339 ms 480 KB Output is correct
21 Correct 835 ms 1312 KB Output is correct
22 Correct 811 ms 1104 KB Output is correct
23 Correct 872 ms 1032 KB Output is correct
24 Correct 817 ms 1344 KB Output is correct
25 Correct 12 ms 344 KB Output is correct
26 Correct 8 ms 344 KB Output is correct
27 Correct 30 ms 344 KB Output is correct
28 Correct 27 ms 344 KB Output is correct
29 Correct 24 ms 344 KB Output is correct
30 Correct 181 ms 344 KB Output is correct
31 Correct 186 ms 432 KB Output is correct
32 Correct 188 ms 344 KB Output is correct
33 Correct 309 ms 604 KB Output is correct
34 Correct 304 ms 636 KB Output is correct
35 Correct 294 ms 484 KB Output is correct
36 Correct 777 ms 1292 KB Output is correct
37 Correct 884 ms 968 KB Output is correct
38 Correct 753 ms 1740 KB Output is correct
39 Correct 930 ms 1132 KB Output is correct
40 Correct 774 ms 1100 KB Output is correct
41 Correct 878 ms 1040 KB Output is correct
42 Correct 775 ms 1016 KB Output is correct
43 Correct 809 ms 868 KB Output is correct
44 Correct 924 ms 1256 KB Output is correct
45 Correct 12 ms 344 KB Output is correct
46 Correct 13 ms 600 KB Output is correct
47 Correct 31 ms 344 KB Output is correct
48 Correct 24 ms 344 KB Output is correct
49 Correct 27 ms 344 KB Output is correct
50 Correct 212 ms 452 KB Output is correct
51 Correct 197 ms 444 KB Output is correct
52 Correct 222 ms 856 KB Output is correct
53 Correct 331 ms 600 KB Output is correct
54 Correct 350 ms 880 KB Output is correct
55 Correct 288 ms 592 KB Output is correct
56 Correct 809 ms 1104 KB Output is correct
57 Correct 884 ms 1092 KB Output is correct
58 Correct 848 ms 872 KB Output is correct
59 Correct 877 ms 716 KB Output is correct
60 Correct 818 ms 728 KB Output is correct
61 Correct 829 ms 640 KB Output is correct
62 Correct 849 ms 940 KB Output is correct
63 Correct 801 ms 1104 KB Output is correct
64 Correct 821 ms 1036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 24 ms 600 KB Output is correct
3 Partially correct 142 ms 344 KB Output is partially correct
4 Partially correct 387 ms 848 KB Output is partially correct
5 Partially correct 841 ms 940 KB Output is partially correct
6 Correct 9 ms 344 KB Output is correct
7 Correct 29 ms 344 KB Output is correct
8 Partially correct 146 ms 452 KB Output is partially correct
9 Partially correct 338 ms 480 KB Output is partially correct
10 Partially correct 789 ms 912 KB Output is partially correct
11 Partially correct 826 ms 964 KB Output is partially correct
12 Partially correct 805 ms 864 KB Output is partially correct
13 Partially correct 891 ms 1104 KB Output is partially correct
14 Correct 10 ms 344 KB Output is correct
15 Correct 16 ms 344 KB Output is correct
16 Correct 42 ms 344 KB Output is correct
17 Partially correct 114 ms 600 KB Output is partially correct
18 Partially correct 138 ms 696 KB Output is partially correct
19 Partially correct 267 ms 720 KB Output is partially correct
20 Partially correct 313 ms 620 KB Output is partially correct
21 Correct 9 ms 344 KB Output is correct
22 Correct 13 ms 344 KB Output is correct
23 Correct 25 ms 344 KB Output is correct
24 Correct 25 ms 344 KB Output is correct
25 Correct 31 ms 344 KB Output is correct
26 Partially correct 172 ms 708 KB Output is partially correct
27 Partially correct 184 ms 600 KB Output is partially correct
28 Partially correct 204 ms 344 KB Output is partially correct
29 Partially correct 298 ms 976 KB Output is partially correct
30 Partially correct 322 ms 740 KB Output is partially correct
31 Partially correct 335 ms 864 KB Output is partially correct
32 Correct 9 ms 344 KB Output is correct
33 Correct 9 ms 344 KB Output is correct
34 Correct 25 ms 344 KB Output is correct
35 Correct 32 ms 344 KB Output is correct
36 Correct 23 ms 344 KB Output is correct
37 Partially correct 216 ms 596 KB Output is partially correct
38 Partially correct 206 ms 440 KB Output is partially correct
39 Partially correct 183 ms 600 KB Output is partially correct
40 Partially correct 332 ms 972 KB Output is partially correct
41 Partially correct 317 ms 600 KB Output is partially correct
42 Partially correct 317 ms 724 KB Output is partially correct
43 Partially correct 887 ms 1252 KB Output is partially correct
44 Partially correct 841 ms 860 KB Output is partially correct
45 Partially correct 762 ms 1004 KB Output is partially correct
46 Partially correct 830 ms 1516 KB Output is partially correct
47 Partially correct 825 ms 1504 KB Output is partially correct
48 Partially correct 796 ms 1216 KB Output is partially correct
49 Partially correct 827 ms 668 KB Output is partially correct
50 Partially correct 833 ms 908 KB Output is partially correct
51 Partially correct 816 ms 848 KB Output is partially correct
52 Partially correct 839 ms 984 KB Output is partially correct
53 Partially correct 845 ms 592 KB Output is partially correct
54 Partially correct 901 ms 1032 KB Output is partially correct
55 Partially correct 815 ms 1036 KB Output is partially correct
56 Partially correct 820 ms 784 KB Output is partially correct
57 Partially correct 858 ms 1360 KB Output is partially correct
58 Partially correct 820 ms 988 KB Output is partially correct
59 Partially correct 855 ms 1120 KB Output is partially correct
60 Partially correct 865 ms 576 KB Output is partially correct
61 Partially correct 886 ms 780 KB Output is partially correct
62 Partially correct 838 ms 872 KB Output is partially correct
63 Partially correct 792 ms 1112 KB Output is partially correct
64 Partially correct 838 ms 980 KB Output is partially correct
65 Partially correct 845 ms 1140 KB Output is partially correct
66 Partially correct 824 ms 1244 KB Output is partially correct
67 Partially correct 825 ms 1556 KB Output is partially correct
68 Partially correct 775 ms 572 KB Output is partially correct
69 Partially correct 827 ms 948 KB Output is partially correct
70 Partially correct 863 ms 880 KB Output is partially correct
71 Partially correct 886 ms 696 KB Output is partially correct
72 Partially correct 836 ms 1004 KB Output is partially correct
73 Partially correct 873 ms 1092 KB Output is partially correct
74 Partially correct 871 ms 972 KB Output is partially correct
75 Partially correct 841 ms 568 KB Output is partially correct
76 Partially correct 819 ms 984 KB Output is partially correct
77 Partially correct 810 ms 960 KB Output is partially correct
78 Partially correct 841 ms 1332 KB Output is partially correct
79 Partially correct 810 ms 1368 KB Output is partially correct
80 Partially correct 858 ms 1368 KB Output is partially correct
81 Partially correct 839 ms 728 KB Output is partially correct
82 Partially correct 898 ms 812 KB Output is partially correct