답안 #846585

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
846585 2023-09-08T03:21:27 Z math_rabbit_1028 가장 긴 여행 (IOI23_longesttrip) C++17
70 / 100
66 ms 1508 KB
#include <bits/stdc++.h>
#include "longesttrip.h"
using namespace std;

int find_connected(vector<int> v, vector<int> arr) {
    if (arr.size() == 0 || !are_connected(v, arr)) return -1;

    int s = 0, e = arr.size() - 1;
    while (s < e) {
        int m = (s + e) / 2;
        vector<int> temp;
        for (int i = s; i <= m; i++) temp.push_back(arr[i]);
        if (are_connected(v, temp)) e = m;
        else s = m + 1;
    }
    return arr[s];
}

int find_connected_idx(vector<int> v, vector<int> arr) {
    if (arr.size() == 0 || !are_connected(v, arr)) return -1;

    int s = 0, e = arr.size() - 1;
    while (s < e) {
        int m = (s + e) / 2;
        vector<int> temp;
        for (int i = s; i <= m; i++) temp.push_back(arr[i]);
        if (are_connected(v, temp)) e = m;
        else s = m + 1;
    }
    return s;
}

vector<int> longest_trip(int N, int D) {
    int n = N; 
    vector<int> res;

    int ch[256]; for (int i = 0; i < n; i++) ch[i] = 0;
    vector<int> path, clique;
    path.push_back(0);
    ch[0] = 1;
    while (true) {
        vector<int> temp;
        for (int i = 0; i < n; i++) if (ch[i] == 0) temp.push_back(i);
        int v = find_connected({path.back()}, temp);
        if (v == -1) break;
        path.push_back(v);
        ch[v] = 1;
    }
    for (int i = 0; i < n; i++) if (ch[i] == 0) clique.push_back(i);

    bool connected = false;
    if (path.size() > 0 && clique.size() > 0) connected = are_connected(path, clique);
    if (connected) {
        int target = find_connected({path[0]}, clique);
        if (target != -1) {
            res = path;
            reverse(res.begin(), res.end());
            int t = 0;
            for (t = 0; t < clique.size(); t++) if (clique[t] == target) break;
            swap(clique[0], clique[t]);                    
            for (int j = 0; j < clique.size(); j++) res.push_back(clique[j]);
            return res;
        }
        int s = path.size() - 1;
        target = find_connected({path[s]}, clique);
        if (target != -1) {
            res = path;
            int t = 0;
            for (t = 0; t < clique.size(); t++) if (clique[t] == target) break;
            swap(clique[0], clique[t]);
            for (int j = 0; j < clique.size(); j++) res.push_back(clique[j]);
            return res;
        }

        int idx = find_connected_idx(path, clique);
        target = find_connected({clique[idx]}, path);
        swap(clique[idx], clique.back());
        res = clique;
        while (path[0] != target) {
            int front = path[0];
            path.erase(path.begin());
            path.push_back(front);
        }
        for (int k = 0; k < path.size(); k++) res.push_back(path[k]);
        return res;
    }
    else {
        if (path.size() > clique.size()) return path;
        return clique;
    }
    return {}; // never reached
}

Compilation message

longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:59:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |             for (t = 0; t < clique.size(); t++) if (clique[t] == target) break;
      |                         ~~^~~~~~~~~~~~~~~
longesttrip.cpp:61:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |             for (int j = 0; j < clique.size(); j++) res.push_back(clique[j]);
      |                             ~~^~~~~~~~~~~~~~~
longesttrip.cpp:69:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   69 |             for (t = 0; t < clique.size(); t++) if (clique[t] == target) break;
      |                         ~~^~~~~~~~~~~~~~~
longesttrip.cpp:71:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |             for (int j = 0; j < clique.size(); j++) res.push_back(clique[j]);
      |                             ~~^~~~~~~~~~~~~~~
longesttrip.cpp:84:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   84 |         for (int k = 0; k < path.size(); k++) res.push_back(path[k]);
      |                         ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 15 ms 720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 344 KB Output is correct
2 Correct 18 ms 344 KB Output is correct
3 Correct 31 ms 344 KB Output is correct
4 Correct 48 ms 952 KB Output is correct
5 Correct 62 ms 1240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 17 ms 344 KB Output is correct
3 Correct 26 ms 344 KB Output is correct
4 Correct 48 ms 708 KB Output is correct
5 Correct 61 ms 992 KB Output is correct
6 Correct 8 ms 344 KB Output is correct
7 Correct 17 ms 344 KB Output is correct
8 Correct 30 ms 344 KB Output is correct
9 Correct 37 ms 440 KB Output is correct
10 Correct 59 ms 748 KB Output is correct
11 Correct 57 ms 980 KB Output is correct
12 Correct 53 ms 1000 KB Output is correct
13 Correct 62 ms 1256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 13 ms 344 KB Output is correct
3 Correct 29 ms 344 KB Output is correct
4 Correct 44 ms 952 KB Output is correct
5 Correct 64 ms 1468 KB Output is correct
6 Correct 9 ms 344 KB Output is correct
7 Correct 17 ms 340 KB Output is correct
8 Correct 31 ms 344 KB Output is correct
9 Correct 40 ms 440 KB Output is correct
10 Correct 57 ms 972 KB Output is correct
11 Correct 56 ms 1000 KB Output is correct
12 Correct 63 ms 736 KB Output is correct
13 Correct 61 ms 744 KB Output is correct
14 Correct 9 ms 344 KB Output is correct
15 Correct 9 ms 344 KB Output is correct
16 Correct 21 ms 596 KB Output is correct
17 Correct 22 ms 600 KB Output is correct
18 Correct 32 ms 344 KB Output is correct
19 Correct 37 ms 600 KB Output is correct
20 Correct 36 ms 444 KB Output is correct
21 Correct 63 ms 756 KB Output is correct
22 Correct 55 ms 752 KB Output is correct
23 Correct 59 ms 996 KB Output is correct
24 Correct 56 ms 984 KB Output is correct
25 Correct 8 ms 344 KB Output is correct
26 Correct 7 ms 420 KB Output is correct
27 Correct 20 ms 344 KB Output is correct
28 Correct 13 ms 344 KB Output is correct
29 Correct 16 ms 344 KB Output is correct
30 Correct 34 ms 436 KB Output is correct
31 Correct 23 ms 684 KB Output is correct
32 Correct 18 ms 436 KB Output is correct
33 Correct 39 ms 444 KB Output is correct
34 Correct 38 ms 604 KB Output is correct
35 Correct 32 ms 444 KB Output is correct
36 Correct 59 ms 728 KB Output is correct
37 Correct 56 ms 1228 KB Output is correct
38 Correct 55 ms 740 KB Output is correct
39 Correct 42 ms 1000 KB Output is correct
40 Correct 37 ms 1500 KB Output is correct
41 Correct 32 ms 984 KB Output is correct
42 Correct 36 ms 740 KB Output is correct
43 Correct 37 ms 1180 KB Output is correct
44 Correct 34 ms 996 KB Output is correct
45 Correct 12 ms 344 KB Output is correct
46 Correct 14 ms 344 KB Output is correct
47 Correct 16 ms 344 KB Output is correct
48 Correct 16 ms 340 KB Output is correct
49 Correct 15 ms 344 KB Output is correct
50 Correct 28 ms 692 KB Output is correct
51 Correct 26 ms 692 KB Output is correct
52 Correct 21 ms 436 KB Output is correct
53 Correct 33 ms 444 KB Output is correct
54 Correct 32 ms 692 KB Output is correct
55 Correct 32 ms 700 KB Output is correct
56 Correct 56 ms 704 KB Output is correct
57 Correct 51 ms 1000 KB Output is correct
58 Correct 45 ms 980 KB Output is correct
59 Correct 46 ms 996 KB Output is correct
60 Correct 46 ms 744 KB Output is correct
61 Correct 37 ms 1232 KB Output is correct
62 Correct 50 ms 1232 KB Output is correct
63 Correct 37 ms 984 KB Output is correct
64 Correct 49 ms 996 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 596 KB Output is correct
2 Correct 16 ms 344 KB Output is correct
3 Correct 27 ms 344 KB Output is correct
4 Partially correct 46 ms 860 KB Output is partially correct
5 Partially correct 58 ms 1252 KB Output is partially correct
6 Correct 8 ms 344 KB Output is correct
7 Correct 16 ms 344 KB Output is correct
8 Correct 29 ms 340 KB Output is correct
9 Partially correct 38 ms 440 KB Output is partially correct
10 Partially correct 62 ms 1500 KB Output is partially correct
11 Partially correct 62 ms 988 KB Output is partially correct
12 Partially correct 60 ms 992 KB Output is partially correct
13 Partially correct 61 ms 972 KB Output is partially correct
14 Correct 7 ms 344 KB Output is correct
15 Correct 9 ms 600 KB Output is correct
16 Correct 21 ms 344 KB Output is correct
17 Correct 25 ms 600 KB Output is correct
18 Correct 27 ms 344 KB Output is correct
19 Partially correct 39 ms 440 KB Output is partially correct
20 Partially correct 38 ms 444 KB Output is partially correct
21 Correct 7 ms 344 KB Output is correct
22 Correct 9 ms 344 KB Output is correct
23 Correct 18 ms 344 KB Output is correct
24 Correct 14 ms 344 KB Output is correct
25 Correct 15 ms 344 KB Output is correct
26 Correct 30 ms 436 KB Output is correct
27 Correct 23 ms 432 KB Output is correct
28 Correct 20 ms 684 KB Output is correct
29 Partially correct 44 ms 440 KB Output is partially correct
30 Partially correct 39 ms 604 KB Output is partially correct
31 Partially correct 36 ms 440 KB Output is partially correct
32 Correct 11 ms 344 KB Output is correct
33 Correct 12 ms 344 KB Output is correct
34 Correct 15 ms 344 KB Output is correct
35 Correct 15 ms 344 KB Output is correct
36 Correct 16 ms 344 KB Output is correct
37 Correct 32 ms 688 KB Output is correct
38 Correct 23 ms 692 KB Output is correct
39 Correct 24 ms 696 KB Output is correct
40 Partially correct 34 ms 604 KB Output is partially correct
41 Partially correct 32 ms 688 KB Output is partially correct
42 Partially correct 36 ms 600 KB Output is partially correct
43 Partially correct 62 ms 972 KB Output is partially correct
44 Partially correct 63 ms 492 KB Output is partially correct
45 Partially correct 57 ms 1000 KB Output is partially correct
46 Partially correct 64 ms 732 KB Output is partially correct
47 Partially correct 58 ms 1500 KB Output is partially correct
48 Partially correct 56 ms 736 KB Output is partially correct
49 Partially correct 54 ms 1000 KB Output is partially correct
50 Partially correct 43 ms 740 KB Output is partially correct
51 Partially correct 38 ms 964 KB Output is partially correct
52 Partially correct 42 ms 1216 KB Output is partially correct
53 Partially correct 35 ms 488 KB Output is partially correct
54 Partially correct 40 ms 996 KB Output is partially correct
55 Partially correct 41 ms 1212 KB Output is partially correct
56 Partially correct 52 ms 1508 KB Output is partially correct
57 Partially correct 39 ms 744 KB Output is partially correct
58 Partially correct 31 ms 728 KB Output is partially correct
59 Partially correct 32 ms 968 KB Output is partially correct
60 Partially correct 35 ms 972 KB Output is partially correct
61 Partially correct 36 ms 1244 KB Output is partially correct
62 Partially correct 54 ms 964 KB Output is partially correct
63 Partially correct 59 ms 968 KB Output is partially correct
64 Partially correct 44 ms 1240 KB Output is partially correct
65 Partially correct 47 ms 732 KB Output is partially correct
66 Partially correct 46 ms 1244 KB Output is partially correct
67 Partially correct 36 ms 1228 KB Output is partially correct
68 Partially correct 53 ms 720 KB Output is partially correct
69 Partially correct 37 ms 1000 KB Output is partially correct
70 Partially correct 49 ms 1252 KB Output is partially correct
71 Partially correct 55 ms 1232 KB Output is partially correct
72 Partially correct 37 ms 976 KB Output is partially correct
73 Partially correct 35 ms 700 KB Output is partially correct
74 Partially correct 46 ms 1220 KB Output is partially correct
75 Partially correct 49 ms 980 KB Output is partially correct
76 Partially correct 46 ms 1224 KB Output is partially correct
77 Partially correct 66 ms 720 KB Output is partially correct
78 Partially correct 52 ms 708 KB Output is partially correct
79 Partially correct 55 ms 1220 KB Output is partially correct
80 Partially correct 52 ms 696 KB Output is partially correct
81 Partially correct 58 ms 1224 KB Output is partially correct
82 Partially correct 52 ms 676 KB Output is partially correct