답안 #883059

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
883059 2023-12-04T13:08:18 Z LeticiaFCS 가장 긴 여행 (IOI23_longesttrip) C++17
100 / 100
12 ms 1808 KB
#include "longesttrip.h"
#include<bits/stdc++.h>
using namespace std;



vector<vector<int>> memo;

int point_query(int a, int b){
    if(memo[a][b] != -1) return memo[a][b];
    for(int c = 0; c < memo.size(); c++){
        if(a == c) continue;
        if(b == c) continue;
        if(memo[a][c] == 0 && memo[b][c] == 0)
            return memo[a][b] = memo[b][a] = 1;
    }
    return memo[a][b] = memo[b][a] = are_connected({a}, {b});
}


bool merge_paths(vector<int> p1, vector<int> p2, vector<int> &path){
    if(p1.size() < p2.size()) swap(p1, p2);
    int a = p1[0], b = p1.back();
    int c = p2[0], d = p2.back();
    vector<int> ends_p1, ends_p2;
    if(a == b) ends_p1 = {a};
    else ends_p1 = {a,b};
    if(c == d) ends_p2 = {c};
    else ends_p2 = {c,d};
    if(!are_connected(ends_p1, ends_p2)) return false;
    if(point_query(a, d)){
        path = p2;
        path.insert(path.end(), p1.begin(), p1.end());
        return true;
    }
    if(point_query(b, d)){
        path = p1;
        path.insert(path.end(), p2.rbegin(), p2.rend());
        return true;
    }
    reverse(p2.begin(), p2.end());

    d = p2.back();
    if(point_query(a, d)){
        path = p2;
        path.insert(path.end(), p1.begin(), p1.end());
        return true;
    }
    return false;
}
bool merge_cycles(vector<int> c1, vector<int> c2, vector<int> &path){
    int a = -1, b = -1;
    if(c1.size() > c2.size()) swap(c1, c2);
    int n = c1.size(), m = c2.size();
    int left = 0, right = m;
    while(left + 1 < right){
        int mid = (left + right) / 2;
        vector<int> c = c2; c.resize(mid);
        if(are_connected(c1, c)) right = mid;
        else left = mid;
    }
    b = right-1;
    if(b == -1) return false;
    left = 0, right = n;
    while(left + 1 < right){
        int mid = (left + right) / 2;
        vector<int> c = c1; c.resize(mid);
        if(are_connected({c2[b]}, c)) right = mid;
        else left = mid;
    }
    a = right-1;


    for(int i = (a + 1) % n; i != a; i = (i+1)%n) path.push_back(c1[i]);
    path.push_back(c1[a]);
    path.push_back(c2[b]);
    for(int i = (b + 1) % m; i != b; i = (i+1)%m) path.push_back(c2[i]);
    return true;
}

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){
        deque<int> ans;
        int start = 3;
        if(are_connected({0}, {1}))
            ans = {0, 1}, start = 2; 
        else
            ans = {0, 2, 1};
        
        for(int v = start; v < N; v++){
            if(are_connected({ans.front()}, {v})) ans.push_front(v);
            else ans.push_back(v);
        }
        return {ans.begin(), ans.end()};
    }
    if(D == 1){ 
        memo = vector<vector<int>>(N, vector<int>(N, -1));
        vector<vector<int>> ans = {{0}, {1}};
        vector<int> vert(N-2); iota(vert.begin(), vert.end(), 2);
        for(int v : vert){
            int a = ans[0].back();
            int b = ans[1].back();
            if(point_query(a, v)){
                ans[0].push_back(v);
            } else if(point_query(b, v))
                ans[1].push_back(v);
            else{
                ans[0].insert(ans[0].end(), ans[1].rbegin(), ans[1].rend());
                ans[1] = {v};
            }          
        }
        vector<int> path;
        if(!are_connected(ans[0], ans[1])){
            if(ans[0].size() > ans[1].size()) return ans[0];
                return ans[1];
        }
            
        if(merge_paths(ans[0], ans[1], path))
            return path;
        if(merge_cycles(ans[0], ans[1], path))
            return path;
        
    }
    return {};
}

Compilation message

longesttrip.cpp: In function 'int point_query(int, int)':
longesttrip.cpp:11:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   11 |     for(int c = 0; c < memo.size(); c++){
      |                    ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 2 ms 856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 344 KB Output is correct
2 Correct 5 ms 600 KB Output is correct
3 Correct 4 ms 344 KB Output is correct
4 Correct 4 ms 344 KB Output is correct
5 Correct 5 ms 432 KB Output is correct
6 Correct 5 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 5 ms 344 KB Output is correct
10 Correct 5 ms 344 KB Output is correct
11 Correct 5 ms 600 KB Output is correct
12 Correct 4 ms 344 KB Output is correct
13 Correct 4 ms 436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 344 KB Output is correct
2 Correct 7 ms 344 KB Output is correct
3 Correct 6 ms 600 KB Output is correct
4 Correct 5 ms 680 KB Output is correct
5 Correct 5 ms 1364 KB Output is correct
6 Correct 9 ms 340 KB Output is correct
7 Correct 6 ms 344 KB Output is correct
8 Correct 6 ms 344 KB Output is correct
9 Correct 5 ms 600 KB Output is correct
10 Correct 6 ms 1116 KB Output is correct
11 Correct 6 ms 1116 KB Output is correct
12 Correct 7 ms 1396 KB Output is correct
13 Correct 5 ms 1224 KB Output is correct
14 Correct 8 ms 344 KB Output is correct
15 Correct 7 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 632 KB Output is correct
19 Correct 6 ms 600 KB Output is correct
20 Correct 7 ms 900 KB Output is correct
21 Correct 5 ms 1220 KB Output is correct
22 Correct 6 ms 976 KB Output is correct
23 Correct 8 ms 1220 KB Output is correct
24 Correct 6 ms 1368 KB Output is correct
25 Correct 6 ms 344 KB Output is correct
26 Correct 6 ms 344 KB Output is correct
27 Correct 6 ms 344 KB Output is correct
28 Correct 7 ms 344 KB Output is correct
29 Correct 6 ms 344 KB Output is correct
30 Correct 5 ms 452 KB Output is correct
31 Correct 5 ms 456 KB Output is correct
32 Correct 6 ms 456 KB Output is correct
33 Correct 6 ms 856 KB Output is correct
34 Correct 5 ms 600 KB Output is correct
35 Correct 5 ms 652 KB Output is correct
36 Correct 5 ms 1404 KB Output is correct
37 Correct 5 ms 1372 KB Output is correct
38 Correct 6 ms 1368 KB Output is correct
39 Correct 6 ms 964 KB Output is correct
40 Correct 7 ms 1096 KB Output is correct
41 Correct 7 ms 1472 KB Output is correct
42 Correct 7 ms 1220 KB Output is correct
43 Correct 6 ms 1112 KB Output is correct
44 Correct 6 ms 1372 KB Output is correct
45 Correct 11 ms 600 KB Output is correct
46 Correct 12 ms 348 KB Output is correct
47 Correct 7 ms 436 KB Output is correct
48 Correct 7 ms 612 KB Output is correct
49 Correct 7 ms 436 KB Output is correct
50 Correct 5 ms 456 KB Output is correct
51 Correct 6 ms 456 KB Output is correct
52 Correct 8 ms 460 KB Output is correct
53 Correct 5 ms 660 KB Output is correct
54 Correct 5 ms 716 KB Output is correct
55 Correct 7 ms 344 KB Output is correct
56 Correct 5 ms 1224 KB Output is correct
57 Correct 7 ms 1372 KB Output is correct
58 Correct 7 ms 1384 KB Output is correct
59 Correct 8 ms 1484 KB Output is correct
60 Correct 7 ms 976 KB Output is correct
61 Correct 7 ms 1112 KB Output is correct
62 Correct 10 ms 1212 KB Output is correct
63 Correct 10 ms 1372 KB Output is correct
64 Correct 8 ms 1248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 596 KB Output is correct
2 Correct 7 ms 344 KB Output is correct
3 Correct 6 ms 440 KB Output is correct
4 Correct 5 ms 344 KB Output is correct
5 Correct 6 ms 968 KB Output is correct
6 Correct 10 ms 344 KB Output is correct
7 Correct 7 ms 600 KB Output is correct
8 Correct 6 ms 596 KB Output is correct
9 Correct 6 ms 856 KB Output is correct
10 Correct 6 ms 976 KB Output is correct
11 Correct 6 ms 1256 KB Output is correct
12 Correct 7 ms 1104 KB Output is correct
13 Correct 6 ms 980 KB Output is correct
14 Correct 7 ms 344 KB Output is correct
15 Correct 8 ms 344 KB Output is correct
16 Correct 7 ms 344 KB Output is correct
17 Correct 7 ms 344 KB Output is correct
18 Correct 6 ms 344 KB Output is correct
19 Correct 6 ms 856 KB Output is correct
20 Correct 7 ms 344 KB Output is correct
21 Correct 6 ms 344 KB Output is correct
22 Correct 7 ms 344 KB Output is correct
23 Correct 7 ms 344 KB Output is correct
24 Correct 7 ms 344 KB Output is correct
25 Correct 7 ms 344 KB Output is correct
26 Correct 5 ms 456 KB Output is correct
27 Correct 5 ms 452 KB Output is correct
28 Correct 6 ms 456 KB Output is correct
29 Correct 5 ms 656 KB Output is correct
30 Correct 7 ms 608 KB Output is correct
31 Correct 7 ms 600 KB Output is correct
32 Correct 11 ms 344 KB Output is correct
33 Correct 11 ms 344 KB Output is correct
34 Correct 10 ms 344 KB Output is correct
35 Correct 8 ms 432 KB Output is correct
36 Correct 7 ms 600 KB Output is correct
37 Correct 5 ms 456 KB Output is correct
38 Correct 7 ms 460 KB Output is correct
39 Correct 8 ms 456 KB Output is correct
40 Correct 5 ms 344 KB Output is correct
41 Correct 6 ms 644 KB Output is correct
42 Correct 6 ms 344 KB Output is correct
43 Correct 6 ms 1220 KB Output is correct
44 Correct 6 ms 1116 KB Output is correct
45 Correct 5 ms 1220 KB Output is correct
46 Correct 7 ms 1384 KB Output is correct
47 Correct 5 ms 1372 KB Output is correct
48 Correct 6 ms 1116 KB Output is correct
49 Correct 7 ms 1296 KB Output is correct
50 Correct 6 ms 1372 KB Output is correct
51 Correct 9 ms 1248 KB Output is correct
52 Correct 9 ms 1476 KB Output is correct
53 Correct 7 ms 1368 KB Output is correct
54 Correct 6 ms 964 KB Output is correct
55 Correct 6 ms 1244 KB Output is correct
56 Correct 5 ms 1136 KB Output is correct
57 Correct 6 ms 1116 KB Output is correct
58 Correct 8 ms 972 KB Output is correct
59 Correct 6 ms 1368 KB Output is correct
60 Correct 8 ms 1556 KB Output is correct
61 Correct 6 ms 1112 KB Output is correct
62 Correct 6 ms 1224 KB Output is correct
63 Correct 7 ms 1228 KB Output is correct
64 Correct 7 ms 1480 KB Output is correct
65 Correct 9 ms 1120 KB Output is correct
66 Correct 8 ms 1220 KB Output is correct
67 Correct 7 ms 976 KB Output is correct
68 Correct 7 ms 1116 KB Output is correct
69 Correct 7 ms 1224 KB Output is correct
70 Correct 7 ms 1808 KB Output is correct
71 Correct 7 ms 964 KB Output is correct
72 Correct 7 ms 1376 KB Output is correct
73 Correct 7 ms 1468 KB Output is correct
74 Correct 8 ms 1124 KB Output is correct
75 Correct 8 ms 1364 KB Output is correct
76 Correct 8 ms 1124 KB Output is correct
77 Correct 6 ms 1240 KB Output is correct
78 Correct 7 ms 1232 KB Output is correct
79 Correct 6 ms 1224 KB Output is correct
80 Correct 7 ms 1236 KB Output is correct
81 Correct 7 ms 1368 KB Output is correct
82 Correct 8 ms 972 KB Output is correct