Submission #840517

# Submission time Handle Problem Language Result Execution time Memory
840517 2023-08-31T13:17:00 Z arbuzick Longest Trip (IOI23_longesttrip) C++17
40 / 100
848 ms 588 KB
#include <bits/stdc++.h>

#include "longesttrip.h"

using namespace std;

vector<int> longest_trip(int n, int d) {
    if (d == 3) {
        vector<int> ans(n);
        iota(ans.begin(), ans.end(), 0);
        return ans;
    }
    if (d == 2) {
        vector<int> ans = {0};
        vector<int> used(n);
        used[0] = 1;
        while (ans.size() < n) {
            int nxt = -1;
            for (int i = 0; i < n; ++i) {
                if (!used[i] && are_connected({ans.back()}, {i})) {
                    nxt = i;
                    used[i] = 1;
                    break;
                }
            }
            if (nxt == -1) {
                reverse(ans.begin(), ans.end());
                for (int i = 0; i < n; ++i) {
                    if (!used[i] && are_connected({ans.back()}, {i})) {
                        nxt = i;
                        used[i] = 1;
                        break;
                    }
                }
            }
            ans.push_back(nxt);
        }
        return ans;
    }
    if (d == 1) {
        vector<vector<int>> g(n, vector<int>(n));
        for (int i = 0; i < n; ++i) {
            for (int j = i + 1; j < n; ++j) {
                if (are_connected({i}, {j})) {
                    g[i][j] = g[j][i] = 1;
                }
            }
        }
        vector<int> ans = {0};
        vector<int> used(n);
        used[0] = 1;
        // auto try_to_add = [&]() {
        //     int nxt = -1;
        //     for (int i = 0; i < n; ++i) {
        //         if (!used[i] && g[ans.back()][i]) {
        //             nxt = i;
        //             used[i] = 1;
        //             break;
        //         }
        //     }
        //     if (nxt == -1) {
        //         return false;
        //     }
        //     ans.push_back(nxt);
        //     return true;
        // };
        // while (true) {
        //     if (!try_to_add()) {
        //         break;
        //     }
        // }
        while (true) {
            int nxt = -1;
            for (int i = 0; i < n; ++i) {
                if (!used[i] && g[ans.back()][i]) {
                    nxt = i;
                    used[i] = 1;
                    break;
                }
            }
            if (nxt == -1) {
                break;
            }
            ans.push_back(nxt);
        }
        // if (ans.size() < n) {
        //     reverse(ans.begin(), ans.end());
        //     if (try_to_add()) {
        //         while (true) {
        //             if (!try_to_add()) {
        //                 break;
        //             }
        //         }
        //     } else {
        //         for (int _ = 0; _ < (int)ans.size(); ++_) {
        //             reverse(ans.begin(), ans.end());
        //             int vl = ans.back();
        //             ans.pop_back();
        //             reverse(ans.begin(), ans.end());
        //             ans.push_back(vl);
        //             if (try_to_add()) {
        //                 while (true) {
        //                     if (!try_to_add()) {
        //                         break;
        //                     }
        //                 }
        //                 break;
        //             }
        //         }
        //     }
        // }
        if (ans.size() * 2 < n) {
            ans.clear();
            for (int i = 0; i < n; ++i) {
                if (!used[i]) {
                    ans.push_back(i);
                }
            }
        }
        return ans;
    }
}

Compilation message

longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:17:27: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   17 |         while (ans.size() < n) {
      |                ~~~~~~~~~~~^~~
longesttrip.cpp:112:28: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  112 |         if (ans.size() * 2 < n) {
      |             ~~~~~~~~~~~~~~~^~~
longesttrip.cpp:122:1: warning: control reaches end of non-void function [-Wreturn-type]
  122 | }
      | ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 193 ms 464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 208 KB Output is correct
2 Correct 1 ms 208 KB Output is correct
3 Correct 1 ms 208 KB Output is correct
4 Correct 1 ms 208 KB Output is correct
5 Correct 1 ms 208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 208 KB Output is correct
2 Correct 4 ms 332 KB Output is correct
3 Correct 9 ms 208 KB Output is correct
4 Correct 8 ms 208 KB Output is correct
5 Correct 8 ms 260 KB Output is correct
6 Correct 10 ms 208 KB Output is correct
7 Correct 11 ms 208 KB Output is correct
8 Correct 8 ms 260 KB Output is correct
9 Correct 7 ms 208 KB Output is correct
10 Correct 9 ms 208 KB Output is correct
11 Correct 9 ms 208 KB Output is correct
12 Correct 8 ms 208 KB Output is correct
13 Correct 8 ms 208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 208 KB Output is correct
2 Correct 29 ms 208 KB Output is correct
3 Correct 114 ms 208 KB Output is correct
4 Correct 420 ms 344 KB Output is correct
5 Correct 756 ms 464 KB Output is correct
6 Correct 10 ms 208 KB Output is correct
7 Correct 19 ms 208 KB Output is correct
8 Correct 99 ms 208 KB Output is correct
9 Correct 311 ms 336 KB Output is correct
10 Correct 806 ms 464 KB Output is correct
11 Correct 846 ms 588 KB Output is correct
12 Correct 750 ms 516 KB Output is correct
13 Correct 781 ms 464 KB Output is correct
14 Correct 8 ms 208 KB Output is correct
15 Correct 15 ms 208 KB Output is correct
16 Correct 51 ms 208 KB Output is correct
17 Correct 99 ms 208 KB Output is correct
18 Correct 162 ms 208 KB Output is correct
19 Correct 293 ms 336 KB Output is correct
20 Correct 298 ms 336 KB Output is correct
21 Correct 760 ms 464 KB Output is correct
22 Correct 797 ms 464 KB Output is correct
23 Correct 761 ms 464 KB Output is correct
24 Correct 729 ms 464 KB Output is correct
25 Correct 11 ms 208 KB Output is correct
26 Correct 13 ms 208 KB Output is correct
27 Correct 33 ms 208 KB Output is correct
28 Correct 33 ms 208 KB Output is correct
29 Correct 31 ms 208 KB Output is correct
30 Correct 195 ms 208 KB Output is correct
31 Correct 117 ms 332 KB Output is correct
32 Correct 217 ms 208 KB Output is correct
33 Correct 254 ms 336 KB Output is correct
34 Correct 331 ms 336 KB Output is correct
35 Correct 252 ms 336 KB Output is correct
36 Correct 784 ms 464 KB Output is correct
37 Correct 796 ms 464 KB Output is correct
38 Correct 813 ms 464 KB Output is correct
39 Correct 791 ms 464 KB Output is correct
40 Correct 806 ms 464 KB Output is correct
41 Correct 791 ms 464 KB Output is correct
42 Correct 786 ms 464 KB Output is correct
43 Correct 848 ms 464 KB Output is correct
44 Correct 826 ms 464 KB Output is correct
45 Correct 16 ms 208 KB Output is correct
46 Correct 16 ms 208 KB Output is correct
47 Correct 32 ms 208 KB Output is correct
48 Correct 34 ms 208 KB Output is correct
49 Correct 30 ms 208 KB Output is correct
50 Correct 192 ms 208 KB Output is correct
51 Correct 182 ms 208 KB Output is correct
52 Correct 168 ms 208 KB Output is correct
53 Correct 301 ms 336 KB Output is correct
54 Correct 304 ms 336 KB Output is correct
55 Correct 327 ms 336 KB Output is correct
56 Correct 651 ms 464 KB Output is correct
57 Correct 846 ms 464 KB Output is correct
58 Correct 832 ms 500 KB Output is correct
59 Correct 840 ms 464 KB Output is correct
60 Correct 579 ms 464 KB Output is correct
61 Correct 762 ms 464 KB Output is correct
62 Correct 804 ms 464 KB Output is correct
63 Correct 824 ms 464 KB Output is correct
64 Correct 688 ms 464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 208 KB Output is correct
2 Correct 31 ms 208 KB Output is correct
3 Partially correct 155 ms 208 KB Output is partially correct
4 Partially correct 408 ms 344 KB Output is partially correct
5 Partially correct 799 ms 464 KB Output is partially correct
6 Incorrect 0 ms 208 KB Incorrect
7 Halted 0 ms 0 KB -