제출 #894700

#제출 시각아이디문제언어결과실행 시간메모리
894700JeanBombeur가장 긴 여행 (IOI23_longesttrip)C++17
15 / 100
765 ms1864 KiB
#include "longesttrip.h"
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;

//  <|°_°|>

const int MAX_NODES = 256;

bool Edge[MAX_NODES][MAX_NODES];
vector <int> Adj[MAX_NODES];

vector <int> Path;

int Seen[MAX_NODES];

int Dfs(int node) {
    if (Seen[node])
        return 0;
    Path.push_back(node);
    int ans = Seen[node] = 1;
    for (int dest : Adj[node])
        ans += Dfs(dest);
    return ans;
}

vector <int> longest_trip(int nbNodes, int density) {
    
    for (int i = 0; i < nbNodes; i ++)
    {
        Adj[i].clear();
        fill_n(Edge[i], nbNodes, 0);
    }
    Path.clear();
    fill_n(Seen, nbNodes, 0);
    for (int i = 0; i < nbNodes; i ++)
    {
        for (int j = i + 1; j < nbNodes; j ++)
        {
            if (are_connected({i}, {j}))
            {
                Edge[i][j] = Edge[j][i] = 1;
                Adj[i].push_back(j);
                Adj[j].push_back(i);
            }
        }
    }
    int n = Dfs(0);
    if (n != nbNodes)
    {
        int first = (2 * n) >= nbNodes;
        vector <int> v;
        for (int i = 0; i < nbNodes; i ++)
        {
            if (Seen[i] == first)
                v.push_back(i);
        }
        return v;
    }
    vector <int> v = {0};
    for (int a : Path)
    {
        if (!a)
            continue;
        if (Edge[v.back()][a])
            v.push_back(a);
        else if (Edge[v[0]][a])
        {
            reverse(v.begin(), v.end());
            v.push_back(a);
        }
        else
        {
            vector <int> nv;
            while (!Edge[a][v.back()])
            {
                nv.push_back(v.back());
                v.pop_back();
            }
            v.push_back(a);
            while (nv.size())
            {
                v.push_back(nv.back());
                nv.pop_back();
            }
        }
    }
    return v;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...