Submission #843051

# Submission time Handle Problem Language Result Execution time Memory
843051 2023-09-03T15:23:08 Z ngano_upat_na Longest Trip (IOI23_longesttrip) C++17
5 / 100
8 ms 344 KB
#include "bits/stdc++.h"
#include "longesttrip.h"
using namespace std;

vector<int> longest_trip(int N, int D) {
    vector<int> ans;
    if (D == 3) {
        for (int i=0; i<N; i++) ans.push_back(i);
        return ans;
    }   
    vector<int> adj[N];
    for (int i=0; i<N-1; i++) {
        for (int j=i+1; j<N; j++) {
            bool con = are_connected({i},{j});
            if (con) {
                adj[i].push_back(j);
                adj[j].push_back(i);
            }   
        }   
    }   
    
    int source = -1, end = -1, distance = -1;
    for (int i=0; i<N; i++) {
        vector<bool> vis(N,false);
        vector<int> d(N,-1);
        queue<int> q;
        
        q.push(i);
        d[i] = 0;
        while (!q.empty()) {
            int s = q.front();
            q.pop();
            vis[s] = true;
            for (auto &v:adj[s]) {
                if (!vis[v]) {
                    q.push(v);
                    d[v] = d[s] + 1;
                }   
            }   
        }   
        int dis = 0;
        for (int j=0; j<N; j++) {
            if (d[j] > distance) {
                source = i;
                end = j;
                distance = d[j];
            }   
        }   
    }   
    
    // do bfs
    vector<bool> vis1(N,false);
    vector<int> path(N,-1);
    queue<int> q;
    
    q.push(source);
    while (!q.empty()) {
        int s = q.front();
        q.pop();
        vis1[s] = true;
        for (auto &v:adj[s]) {
            if (!vis1[v]) {
                q.push(v);
                path[s] = v;
            }   
        }   
    }   
    for (int i=source; i!=-1; i=path[i]) ans.push_back(i);
    return ans;
}    

Compilation message

longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:41:13: warning: unused variable 'dis' [-Wunused-variable]
   41 |         int dis = 0;
      |             ^~~
longesttrip.cpp:22:22: warning: variable 'end' set but not used [-Wunused-but-set-variable]
   22 |     int source = -1, end = -1, distance = -1;
      |                      ^~~
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 344 KB Incorrect
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 344 KB Incorrect
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 344 KB Output is correct
2 Incorrect 1 ms 344 KB Incorrect
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 344 KB Incorrect
2 Halted 0 ms 0 KB -