답안 #1056758

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1056758 2024-08-13T10:52:52 Z mc061 가장 긴 여행 (IOI23_longesttrip) C++17
0 / 100
1000 ms 856 KB
#include <bits/stdc++.h>
using namespace std;

vector<int> path;
vector<int> mxpath;
int n;
set<int> have;

const int maxN = 256;
bool connected[maxN][maxN]={};
bool ans[maxN][maxN]={};
bool vis[maxN]={};
set<int> comp;

int tar;

bool are_connected(vector<int> A, vector<int> B);

void dfs(int v, bool fill_comp = false, bool X = false) {
    path.push_back(v);
    have.insert(v);
    if (vis[v] && X) return;
    vis[v] = true;
    if (fill_comp) comp.insert(v);
    if (path.size() == tar) {
        return;
    }
    vector<int> ord(n);
    iota(ord.begin(), ord.end(), 0);
    random_shuffle(ord.begin(), ord.end());
    for (int i : ord) {
        if (!have.count(i)) {
            bool c = false;
            if (ans[v][i]) {
                c = connected[v][i];
            }
            else {
                ans[v][i] = ans[i][v] = true;
                bool x = are_connected(vector<int>{i}, vector<int>{v});
                connected[v][i] = connected[i][v] = x;
                c = x;
            }
            if (c)
                dfs(i);
        }
        if (path.size() == tar) return;
    }
    if (path.size() > mxpath.size()) {
        mxpath = path;
    }
    if (path.size() == tar) return;
    path.pop_back();
    have.erase(v);
}

vector<int> longest_trip(int N, int D) {
    tar = 1e9;
    memset(connected, 0, sizeof(connected));
    memset(ans, 0, sizeof(ans));
    n = N;
    int start = 0;
    comp.clear();
    memset(vis, 0, sizeof(vis));
    mxpath.clear();
    path.clear();
    have.clear();
    dfs(0, true, true);
    int cnt = comp.size();
    tar = max(cnt, n - cnt);
    if (cnt == tar) {
        path.clear();
        have.clear();
        dfs(0);
        return mxpath.size() > path.size() ? mxpath : path;
    }
    for (int i = 0; i < n; ++i) {
        have.clear();
        path.clear();
        if (!vis[i]) {
            dfs(i);
            break;
        }
    }
    return mxpath.size() > path.size() ? mxpath : path;
}

Compilation message

longesttrip.cpp: In function 'void dfs(int, bool, bool)':
longesttrip.cpp:25:21: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   25 |     if (path.size() == tar) {
      |         ~~~~~~~~~~~~^~~~~~
longesttrip.cpp:46:25: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   46 |         if (path.size() == tar) return;
      |             ~~~~~~~~~~~~^~~~~~
longesttrip.cpp:51:21: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   51 |     if (path.size() == tar) return;
      |         ~~~~~~~~~~~~^~~~~~
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:61:9: warning: unused variable 'start' [-Wunused-variable]
   61 |     int start = 0;
      |         ^~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Execution timed out 3089 ms 856 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 344 KB Output is correct
2 Execution timed out 3054 ms 344 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 344 KB Output is correct
2 Execution timed out 3077 ms 344 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 344 KB Output is correct
2 Execution timed out 3078 ms 340 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 344 KB Output is correct
2 Execution timed out 3052 ms 344 KB Time limit exceeded
3 Halted 0 ms 0 KB -