Submission #375994

# Submission time Handle Problem Language Result Execution time Memory
375994 2021-03-10T15:24:07 Z mihai145 Travelling Salesperson (CCO20_day2problem1) C++14
0 / 25
1 ms 384 KB
#include <iostream>
#include <string>
#include <deque>
#include <algorithm>

using namespace std;

const int NMAX = 2000;

int N;
string m[NMAX + 2];

///0-> BLUE
///1-> RED
bool Edge(int x, int y) {
    if(x < y) {
        swap(x, y);
    }

    if(m[x][y] == 'B') {
        return 0;
    }

    return 1;
}

deque <int> dq;
void Push(int vert) {
    if(dq.empty()) {
        dq.push_back(vert);
    } else {
        if(Edge(dq.back(), vert) == 1) {
            dq.push_back(vert);
        } else if(Edge(dq.front(), vert) == 0) {
            dq.push_front(vert);
        } else if(Edge(dq.front(), dq.back()) == 1) {
            int frontVert = dq.front();
            dq.pop_front();
            dq.push_back(frontVert);
            dq.push_back(vert);
        } else {
            int backVert = dq.back();
            dq.pop_back();
            dq.push_back(backVert);
            dq.push_back(vert);
        }
    }
}

void SolveFor(int vert) {
    dq.clear();

    for(int i = 0; i < N; i++) {
        if(i != vert) {
            Push(i);
        }
    }

    Push(vert);

    if(dq.back() == vert) {
        reverse(dq.begin(), dq.end());
    }

    cout << N << '\n';
    for(int v : dq) {
        cout << v + 1 << ' ';
    }
    cout << '\n';
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> N;

    for(int i = 1; i < N; i++) {
        cin >> m[i];
    }

    for(int i = 0; i < N; i++) {
        SolveFor(i);
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Incorrect 1 ms 364 KB Output isn't correct
9 Halted 0 ms 0 KB -