답안 #1087995

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1087995 2024-09-13T16:34:56 Z Valaki2 가장 긴 여행 (IOI23_longesttrip) C++17
70 / 100
88 ms 964 KB
#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;

#define pb push_back

const int maxn = 256;

int n;
int edge[maxn][maxn];

void getedges() {
    for(int i = 0; i < n; i++) {
        for(int j = i + 1; j < n; j++) {
            int val = are_connected({i}, {j});
            edge[i][j] = edge[j][i] = val;
        }
    }
}

bool vis[maxn];

void reset() {
    for(int i = 0; i < n; i++) {
        vis[i] = false;
        for(int j = 0; j < n; j++) {
            edge[i][j] = 0;
        }
    }
}

int binsearch(vector<int> a, vector<int> b) {
    // find one in b that is connected to one in a
    while(b.size() > 1) {
        vector<int> c;
        for(int i = 0; i < b.size(); i++) {
            if(i % 2 == 0) {
                c.pb(b[i]);
            }
        }
        int res = are_connected(a, c);
        if(res == 1) {
            b = c;
        } else {
            c.clear();
            for(int i = 0; i < b.size();  i++) {
                if(i % 2 == 1) {
                    c.pb(b[i]);
                }
            }
            b = c;
        }
    }
    return b[0];
}

void insert_at(vector<int> &path, int x, int y) {
    /*vis[y] = true;
    path.pb(-1);
    for(int i = path.size(); i >= 1; i--) {
        if(path[i - 1] == x) {
            path[i] = y;
            break;
        } else {
            path[i] = path[i - 1];
        }
    }*/
    vis[y] = true;
    vector<int> a, b;
    for(int i = 0; i < path.size(); i++) {
        if(path[i] == x) {
            a = path;
            a.resize(i + 1);
            a.pb(y);
            for(int j = i + 1; j < path.size(); j++) {
                b.pb(path[j]);
            }
            break;
        }
    }
    path.clear();
    for(int cur : b) {
        path.pb(cur);
    }
    for(int cur : a) {
        path.pb(cur);
    }
}

bool iterate(vector<int> &path) {
    /*for(int i = 0; i < n; i++) {
        if(vis[i]) {
            continue;
        }
        if(edge[i][path[0]]) {
            reverse(path.begin(), path.end());
            path.pb(i);
            vis[i] = true;
            reverse(path.begin(), path.end());
            return true;
        }
        if(edge[i][path.back()]) {
            path.pb(i);
            vis[i] = true;
            return true;
        }
    }
    for(int x : path) {
        for(int y = 0; y < n; y++) {
            if(vis[y]) {
                continue;
            }
            if(edge[x][y]) {
                insert_at(path, x, y);
                return true;
            }
        }
    }
    return false;*/
    vector<int> v;
    v.pb(path[0]);
    v.pb(path.back());
    if(v[0] == v[1]) {
        v.resize(1);
    }
    // vector<int> v = {path.back()};
    vector<int> rem;
    for(int i = 0; i < n; i++) {
        if(!vis[i]) {
            rem.pb(i);
        }
    }
    if(are_connected(v, rem)) {
        int newitem = binsearch(v, rem);
        if(path.size() == 1) {
            path.pb(newitem);
            vis[newitem] = true;
            return true;
        } else {
            if(are_connected({v[0]}, {newitem})) {
                reverse(path.begin(), path.end());
                path.pb(newitem);
                vis[newitem] = true;
                reverse(path.begin(), path.end());
                return true;
            } else {
                path.pb(newitem);
                vis[newitem] = true;
                return true;
            }
        }
        /*path.pb(newitem);
        vis[newitem] = true;
        return true;*/
    }
    if(!are_connected(path, rem)) {
        return false;
    }
    int b = binsearch(path, rem);
    int a = binsearch({b}, path);
    insert_at(path, a, b);
    for(int cur : rem) {
        if(cur != b) {
            path.pb(cur);
        }
    }
    return true;
}

vector<int> longest_trip(int N, int D){
    reset();
    n = N;
    // getedges();
    vector<int> path = {0};
    vis[0] = true;
    while(path.size() < n) {
        bool isgood = iterate(path);
        if(!isgood) {
            if(path.size() > n - path.size()) {
                return path;
            } else {
                path.clear();
                for(int i = 0; i < n; i++) {
                    if(!vis[i]) {
                        path.pb(i);
                    }
                }
                return path;
            }
        }
    }
    return path;
}

Compilation message

longesttrip.cpp: In function 'int binsearch(std::vector<int>, std::vector<int>)':
longesttrip.cpp:36:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   36 |         for(int i = 0; i < b.size(); i++) {
      |                        ~~^~~~~~~~~~
longesttrip.cpp:46:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |             for(int i = 0; i < b.size();  i++) {
      |                            ~~^~~~~~~~~~
longesttrip.cpp: In function 'void insert_at(std::vector<int>&, int, int)':
longesttrip.cpp:70:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |     for(int i = 0; i < path.size(); i++) {
      |                    ~~^~~~~~~~~~~~~
longesttrip.cpp:75:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   75 |             for(int j = i + 1; j < path.size(); j++) {
      |                                ~~^~~~~~~~~~~~~
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:176:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  176 |     while(path.size() < n) {
      |           ~~~~~~~~~~~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 14 ms 708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 344 KB Output is correct
2 Correct 19 ms 344 KB Output is correct
3 Correct 39 ms 344 KB Output is correct
4 Correct 52 ms 344 KB Output is correct
5 Correct 84 ms 588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 344 KB Output is correct
2 Correct 17 ms 344 KB Output is correct
3 Correct 42 ms 344 KB Output is correct
4 Correct 54 ms 344 KB Output is correct
5 Correct 74 ms 592 KB Output is correct
6 Correct 10 ms 344 KB Output is correct
7 Correct 19 ms 344 KB Output is correct
8 Correct 39 ms 448 KB Output is correct
9 Correct 62 ms 344 KB Output is correct
10 Correct 76 ms 468 KB Output is correct
11 Correct 88 ms 688 KB Output is correct
12 Correct 72 ms 856 KB Output is correct
13 Correct 63 ms 724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 344 KB Output is correct
2 Correct 19 ms 344 KB Output is correct
3 Correct 46 ms 344 KB Output is correct
4 Correct 55 ms 344 KB Output is correct
5 Correct 75 ms 592 KB Output is correct
6 Correct 8 ms 344 KB Output is correct
7 Correct 21 ms 440 KB Output is correct
8 Correct 42 ms 344 KB Output is correct
9 Correct 43 ms 344 KB Output is correct
10 Correct 86 ms 480 KB Output is correct
11 Correct 66 ms 600 KB Output is correct
12 Correct 68 ms 480 KB Output is correct
13 Correct 75 ms 464 KB Output is correct
14 Correct 10 ms 344 KB Output is correct
15 Correct 10 ms 344 KB Output is correct
16 Correct 28 ms 344 KB Output is correct
17 Correct 45 ms 344 KB Output is correct
18 Correct 41 ms 344 KB Output is correct
19 Correct 53 ms 344 KB Output is correct
20 Correct 52 ms 344 KB Output is correct
21 Correct 82 ms 696 KB Output is correct
22 Correct 77 ms 704 KB Output is correct
23 Correct 72 ms 464 KB Output is correct
24 Correct 63 ms 592 KB Output is correct
25 Correct 6 ms 344 KB Output is correct
26 Correct 7 ms 344 KB Output is correct
27 Correct 15 ms 344 KB Output is correct
28 Correct 21 ms 340 KB Output is correct
29 Correct 18 ms 344 KB Output is correct
30 Correct 45 ms 344 KB Output is correct
31 Correct 29 ms 344 KB Output is correct
32 Correct 21 ms 344 KB Output is correct
33 Correct 39 ms 600 KB Output is correct
34 Correct 56 ms 344 KB Output is correct
35 Correct 39 ms 456 KB Output is correct
36 Correct 73 ms 592 KB Output is correct
37 Correct 65 ms 460 KB Output is correct
38 Correct 62 ms 848 KB Output is correct
39 Correct 57 ms 464 KB Output is correct
40 Correct 50 ms 600 KB Output is correct
41 Correct 32 ms 600 KB Output is correct
42 Correct 34 ms 592 KB Output is correct
43 Correct 42 ms 856 KB Output is correct
44 Correct 36 ms 600 KB Output is correct
45 Correct 15 ms 344 KB Output is correct
46 Correct 11 ms 344 KB Output is correct
47 Correct 20 ms 344 KB Output is correct
48 Correct 17 ms 344 KB Output is correct
49 Correct 28 ms 344 KB Output is correct
50 Correct 41 ms 344 KB Output is correct
51 Correct 27 ms 344 KB Output is correct
52 Correct 29 ms 444 KB Output is correct
53 Correct 65 ms 344 KB Output is correct
54 Correct 58 ms 600 KB Output is correct
55 Correct 42 ms 344 KB Output is correct
56 Correct 61 ms 592 KB Output is correct
57 Correct 59 ms 600 KB Output is correct
58 Correct 78 ms 476 KB Output is correct
59 Correct 48 ms 856 KB Output is correct
60 Correct 38 ms 708 KB Output is correct
61 Correct 47 ms 708 KB Output is correct
62 Correct 42 ms 716 KB Output is correct
63 Correct 40 ms 456 KB Output is correct
64 Correct 44 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 344 KB Output is correct
2 Correct 23 ms 344 KB Output is correct
3 Correct 36 ms 344 KB Output is correct
4 Partially correct 55 ms 600 KB Output is partially correct
5 Partially correct 59 ms 712 KB Output is partially correct
6 Correct 9 ms 344 KB Output is correct
7 Correct 21 ms 344 KB Output is correct
8 Correct 49 ms 340 KB Output is correct
9 Partially correct 53 ms 600 KB Output is partially correct
10 Partially correct 72 ms 704 KB Output is partially correct
11 Partially correct 71 ms 460 KB Output is partially correct
12 Partially correct 56 ms 600 KB Output is partially correct
13 Partially correct 59 ms 592 KB Output is partially correct
14 Correct 7 ms 344 KB Output is correct
15 Correct 9 ms 344 KB Output is correct
16 Correct 23 ms 344 KB Output is correct
17 Correct 39 ms 344 KB Output is correct
18 Correct 61 ms 344 KB Output is correct
19 Partially correct 55 ms 452 KB Output is partially correct
20 Partially correct 56 ms 452 KB Output is partially correct
21 Correct 8 ms 344 KB Output is correct
22 Correct 7 ms 344 KB Output is correct
23 Correct 19 ms 344 KB Output is correct
24 Correct 14 ms 344 KB Output is correct
25 Correct 16 ms 344 KB Output is correct
26 Partially correct 42 ms 344 KB Output is partially correct
27 Correct 25 ms 344 KB Output is correct
28 Correct 24 ms 344 KB Output is correct
29 Partially correct 39 ms 344 KB Output is partially correct
30 Partially correct 59 ms 344 KB Output is partially correct
31 Partially correct 47 ms 344 KB Output is partially correct
32 Correct 10 ms 344 KB Output is correct
33 Correct 14 ms 344 KB Output is correct
34 Correct 22 ms 344 KB Output is correct
35 Correct 19 ms 344 KB Output is correct
36 Correct 18 ms 344 KB Output is correct
37 Partially correct 44 ms 596 KB Output is partially correct
38 Partially correct 24 ms 344 KB Output is partially correct
39 Partially correct 25 ms 344 KB Output is partially correct
40 Partially correct 44 ms 344 KB Output is partially correct
41 Partially correct 45 ms 344 KB Output is partially correct
42 Partially correct 52 ms 600 KB Output is partially correct
43 Partially correct 73 ms 596 KB Output is partially correct
44 Partially correct 67 ms 592 KB Output is partially correct
45 Partially correct 85 ms 716 KB Output is partially correct
46 Partially correct 78 ms 848 KB Output is partially correct
47 Partially correct 74 ms 472 KB Output is partially correct
48 Partially correct 73 ms 592 KB Output is partially correct
49 Partially correct 53 ms 848 KB Output is partially correct
50 Partially correct 47 ms 600 KB Output is partially correct
51 Partially correct 38 ms 600 KB Output is partially correct
52 Partially correct 39 ms 600 KB Output is partially correct
53 Partially correct 32 ms 600 KB Output is partially correct
54 Partially correct 57 ms 596 KB Output is partially correct
55 Partially correct 35 ms 688 KB Output is partially correct
56 Partially correct 60 ms 592 KB Output is partially correct
57 Partially correct 43 ms 592 KB Output is partially correct
58 Partially correct 36 ms 712 KB Output is partially correct
59 Partially correct 35 ms 720 KB Output is partially correct
60 Partially correct 32 ms 600 KB Output is partially correct
61 Partially correct 38 ms 592 KB Output is partially correct
62 Partially correct 77 ms 724 KB Output is partially correct
63 Partially correct 48 ms 472 KB Output is partially correct
64 Partially correct 84 ms 592 KB Output is partially correct
65 Partially correct 42 ms 596 KB Output is partially correct
66 Partially correct 49 ms 724 KB Output is partially correct
67 Partially correct 41 ms 700 KB Output is partially correct
68 Partially correct 44 ms 856 KB Output is partially correct
69 Partially correct 47 ms 848 KB Output is partially correct
70 Partially correct 40 ms 592 KB Output is partially correct
71 Partially correct 75 ms 600 KB Output is partially correct
72 Partially correct 55 ms 612 KB Output is partially correct
73 Partially correct 44 ms 600 KB Output is partially correct
74 Partially correct 48 ms 856 KB Output is partially correct
75 Partially correct 44 ms 960 KB Output is partially correct
76 Partially correct 43 ms 964 KB Output is partially correct
77 Partially correct 73 ms 848 KB Output is partially correct
78 Partially correct 45 ms 592 KB Output is partially correct
79 Partially correct 56 ms 604 KB Output is partially correct
80 Partially correct 43 ms 704 KB Output is partially correct
81 Partially correct 39 ms 704 KB Output is partially correct
82 Partially correct 46 ms 592 KB Output is partially correct