Submission #369308

# Submission time Handle Problem Language Result Execution time Memory
369308 2021-02-21T09:14:24 Z blue Travelling Salesperson (CCO20_day2problem1) C++17
0 / 25
1 ms 492 KB
#include <iostream>
#include <vector>
using namespace std;

int N;
vector<int> red[2001], blue[2001];

vector<int> res;
vector<bool> visited(2001, 0);
int visited_count;
int cost = 0;

void dfs(int u, int col, bool p)
{
    if(p) res.push_back(u);
    if(!visited[u])
    {
        visited[u] = 1;
        visited_count++;
    }
    bool flag;
    if(col == 0)
    {
        flag = 0;
        for(int v: red[u])
        {
            if(visited[v]) continue;
            flag = 1;
            dfs(v, col, 1);
            break;
        }
        if(!flag)
        {
            if(!cost)
            {
                cost++;
                dfs(u, !col, 0);
            }
        }
    }
    else
    {
        flag = 0;
        for(int v: blue[u])
        {
            if(visited[v]) continue;
            flag = 1;
            dfs(v, col, 1);
            break;
        }
        if(!flag)
        {
            if(!cost)
            {
                cost++;
                dfs(u, !col, 0);
            }
        }
    }
}

int main()
{
    cin >> N;
    char c;
    for(int i = 1; i <= N; i++)
    {
        for(int j = 1; j < i; j++)
        {
            cin >> c;
            if(c == 'R')
            {
                red[i].push_back(j);
                red[j].push_back(i);
            }
            else
            {
                blue[i].push_back(j);
                blue[j].push_back(i);
            }
        }
    }

    for(int i = 1; i <= N; i++)
    {
        // cout << "i = " << i << '\n';
        cout << N << '\n';

        visited_count = 0;
        visited = vector<bool>(2001, 0);
        res.clear();
        cost = 0;
        dfs(i, 0, 1);


        if(visited_count == N)
        {
            for(int x: res) cout << x << ' ';
            cout << '\n';
            continue;
        }

        visited_count = 0;
        visited = vector<bool>(2001, 0);
        res.clear();
        cost = 0;
        dfs(i, 1, 1);

        for(int x: res) cout << x << ' ';
        cout << '\n';
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 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 364 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Incorrect 1 ms 364 KB Output isn't correct
15 Halted 0 ms 0 KB -