답안 #840527

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
840527 2023-08-31T13:27:54 Z arbuzick 가장 긴 여행 (IOI23_longesttrip) C++17
60 / 100
884 ms 544 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;
            }
        }
        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:72:24: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   72 |         if (ans.size() < n) {
      |             ~~~~~~~~~~~^~~
longesttrip.cpp:98:28: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   98 |         if (ans.size() * 2 < n) {
      |             ~~~~~~~~~~~~~~~^~~
longesttrip.cpp:108:1: warning: control reaches end of non-void function [-Wreturn-type]
  108 | }
      | ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 203 ms 464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 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 0 ms 208 KB Output is correct
5 Correct 1 ms 208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 208 KB Output is correct
2 Correct 8 ms 208 KB Output is correct
3 Correct 10 ms 208 KB Output is correct
4 Correct 7 ms 208 KB Output is correct
5 Correct 5 ms 208 KB Output is correct
6 Correct 9 ms 208 KB Output is correct
7 Correct 10 ms 208 KB Output is correct
8 Correct 6 ms 208 KB Output is correct
9 Correct 7 ms 208 KB Output is correct
10 Correct 7 ms 208 KB Output is correct
11 Correct 7 ms 208 KB Output is correct
12 Correct 7 ms 208 KB Output is correct
13 Correct 6 ms 208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 208 KB Output is correct
2 Correct 27 ms 208 KB Output is correct
3 Correct 113 ms 208 KB Output is correct
4 Correct 392 ms 348 KB Output is correct
5 Correct 754 ms 464 KB Output is correct
6 Correct 10 ms 208 KB Output is correct
7 Correct 21 ms 208 KB Output is correct
8 Correct 160 ms 208 KB Output is correct
9 Correct 307 ms 336 KB Output is correct
10 Correct 880 ms 464 KB Output is correct
11 Correct 811 ms 464 KB Output is correct
12 Correct 732 ms 464 KB Output is correct
13 Correct 743 ms 464 KB Output is correct
14 Correct 9 ms 208 KB Output is correct
15 Correct 14 ms 208 KB Output is correct
16 Correct 52 ms 208 KB Output is correct
17 Correct 104 ms 208 KB Output is correct
18 Correct 134 ms 208 KB Output is correct
19 Correct 328 ms 336 KB Output is correct
20 Correct 315 ms 336 KB Output is correct
21 Correct 768 ms 464 KB Output is correct
22 Correct 763 ms 464 KB Output is correct
23 Correct 820 ms 464 KB Output is correct
24 Correct 815 ms 464 KB Output is correct
25 Correct 13 ms 208 KB Output is correct
26 Correct 11 ms 208 KB Output is correct
27 Correct 30 ms 208 KB Output is correct
28 Correct 32 ms 208 KB Output is correct
29 Correct 31 ms 208 KB Output is correct
30 Correct 216 ms 208 KB Output is correct
31 Correct 221 ms 208 KB Output is correct
32 Correct 201 ms 208 KB Output is correct
33 Correct 276 ms 336 KB Output is correct
34 Correct 256 ms 336 KB Output is correct
35 Correct 277 ms 208 KB Output is correct
36 Correct 828 ms 500 KB Output is correct
37 Correct 846 ms 464 KB Output is correct
38 Correct 805 ms 464 KB Output is correct
39 Correct 814 ms 464 KB Output is correct
40 Correct 792 ms 464 KB Output is correct
41 Correct 827 ms 464 KB Output is correct
42 Correct 808 ms 464 KB Output is correct
43 Correct 797 ms 464 KB Output is correct
44 Correct 870 ms 464 KB Output is correct
45 Correct 14 ms 208 KB Output is correct
46 Correct 14 ms 208 KB Output is correct
47 Correct 30 ms 208 KB Output is correct
48 Correct 28 ms 208 KB Output is correct
49 Correct 28 ms 208 KB Output is correct
50 Correct 182 ms 208 KB Output is correct
51 Correct 207 ms 208 KB Output is correct
52 Correct 190 ms 208 KB Output is correct
53 Correct 290 ms 336 KB Output is correct
54 Correct 333 ms 336 KB Output is correct
55 Correct 337 ms 336 KB Output is correct
56 Correct 759 ms 464 KB Output is correct
57 Correct 786 ms 464 KB Output is correct
58 Correct 811 ms 464 KB Output is correct
59 Correct 710 ms 464 KB Output is correct
60 Correct 761 ms 464 KB Output is correct
61 Correct 774 ms 464 KB Output is correct
62 Correct 734 ms 464 KB Output is correct
63 Correct 684 ms 464 KB Output is correct
64 Correct 769 ms 544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 208 KB Output is correct
2 Correct 24 ms 208 KB Output is correct
3 Partially correct 129 ms 208 KB Output is partially correct
4 Partially correct 349 ms 344 KB Output is partially correct
5 Partially correct 802 ms 464 KB Output is partially correct
6 Correct 9 ms 208 KB Output is correct
7 Correct 32 ms 208 KB Output is correct
8 Partially correct 149 ms 208 KB Output is partially correct
9 Partially correct 332 ms 336 KB Output is partially correct
10 Partially correct 750 ms 464 KB Output is partially correct
11 Partially correct 780 ms 464 KB Output is partially correct
12 Partially correct 826 ms 464 KB Output is partially correct
13 Partially correct 824 ms 464 KB Output is partially correct
14 Correct 9 ms 208 KB Output is correct
15 Correct 17 ms 208 KB Output is correct
16 Correct 55 ms 208 KB Output is correct
17 Partially correct 99 ms 208 KB Output is partially correct
18 Partially correct 152 ms 208 KB Output is partially correct
19 Partially correct 316 ms 336 KB Output is partially correct
20 Partially correct 293 ms 336 KB Output is partially correct
21 Correct 12 ms 208 KB Output is correct
22 Correct 11 ms 208 KB Output is correct
23 Correct 30 ms 208 KB Output is correct
24 Correct 33 ms 208 KB Output is correct
25 Correct 28 ms 208 KB Output is correct
26 Partially correct 192 ms 208 KB Output is partially correct
27 Partially correct 182 ms 208 KB Output is partially correct
28 Partially correct 174 ms 208 KB Output is partially correct
29 Partially correct 332 ms 336 KB Output is partially correct
30 Partially correct 328 ms 336 KB Output is partially correct
31 Partially correct 303 ms 336 KB Output is partially correct
32 Correct 15 ms 208 KB Output is correct
33 Correct 9 ms 208 KB Output is correct
34 Correct 29 ms 208 KB Output is correct
35 Correct 40 ms 208 KB Output is correct
36 Correct 31 ms 208 KB Output is correct
37 Partially correct 220 ms 208 KB Output is partially correct
38 Partially correct 181 ms 208 KB Output is partially correct
39 Partially correct 226 ms 208 KB Output is partially correct
40 Partially correct 284 ms 336 KB Output is partially correct
41 Partially correct 281 ms 336 KB Output is partially correct
42 Partially correct 295 ms 336 KB Output is partially correct
43 Partially correct 884 ms 464 KB Output is partially correct
44 Partially correct 811 ms 464 KB Output is partially correct
45 Partially correct 817 ms 464 KB Output is partially correct
46 Partially correct 784 ms 464 KB Output is partially correct
47 Partially correct 754 ms 464 KB Output is partially correct
48 Partially correct 767 ms 464 KB Output is partially correct
49 Partially correct 833 ms 464 KB Output is partially correct
50 Partially correct 829 ms 464 KB Output is partially correct
51 Partially correct 768 ms 464 KB Output is partially correct
52 Partially correct 850 ms 464 KB Output is partially correct
53 Partially correct 773 ms 464 KB Output is partially correct
54 Partially correct 746 ms 464 KB Output is partially correct
55 Partially correct 804 ms 464 KB Output is partially correct
56 Partially correct 696 ms 464 KB Output is partially correct
57 Partially correct 823 ms 464 KB Output is partially correct
58 Partially correct 759 ms 464 KB Output is partially correct
59 Partially correct 765 ms 512 KB Output is partially correct
60 Partially correct 813 ms 464 KB Output is partially correct
61 Partially correct 812 ms 464 KB Output is partially correct
62 Partially correct 707 ms 464 KB Output is partially correct
63 Partially correct 732 ms 464 KB Output is partially correct
64 Partially correct 808 ms 464 KB Output is partially correct
65 Partially correct 833 ms 464 KB Output is partially correct
66 Partially correct 794 ms 464 KB Output is partially correct
67 Partially correct 820 ms 464 KB Output is partially correct
68 Partially correct 812 ms 464 KB Output is partially correct
69 Partially correct 688 ms 464 KB Output is partially correct
70 Partially correct 828 ms 464 KB Output is partially correct
71 Partially correct 857 ms 464 KB Output is partially correct
72 Partially correct 782 ms 464 KB Output is partially correct
73 Partially correct 800 ms 464 KB Output is partially correct
74 Partially correct 774 ms 464 KB Output is partially correct
75 Partially correct 802 ms 464 KB Output is partially correct
76 Partially correct 755 ms 464 KB Output is partially correct
77 Partially correct 817 ms 464 KB Output is partially correct
78 Partially correct 787 ms 464 KB Output is partially correct
79 Partially correct 759 ms 464 KB Output is partially correct
80 Partially correct 728 ms 464 KB Output is partially correct
81 Partially correct 879 ms 464 KB Output is partially correct
82 Partially correct 878 ms 464 KB Output is partially correct