Submission #1238420

#TimeUsernameProblemLanguageResultExecution timeMemory
1238420matsakyannnLongest Trip (IOI23_longesttrip)C++20
5 / 100
1127 ms1645480 KiB
#include <bits/stdc++.h>
#include "longesttrip.h"
using namespace std;

#ifndef ONLINE_JUDGE
#define dbg(x) cerr << #x << ' '; print(x); cerr << endl;
#else
#define dbg(x)
#endif 

void print(int x) {cerr << x;}
void print(long long x) {cerr << x;}
void print(char x) {cerr << x;}
void print(string x) {cerr << x;}
void print(double x) {cerr << x;}
template <class T> void print(vector <T> x);
template <class T> void print(set <T> x);
template <class T> void print(multiset <T> x);
template <class T, class V> void print(pair <T, V> x);
template <class T, class V> void print(map <T, V> x);
template <class T> void print(vector <T> x) {cerr << "[ "; for(auto i : x) {print(i); cerr << ' ';} cerr << "]";}
template <class T> void print(set <T> x) {cerr << "[ "; for(auto i : x) {print(i); cerr << ' ';} cerr << "]";}
template <class T> void print(multiset <T> x) {cerr << "[ "; for(auto i : x) {print(i); cerr << ' ';} cerr << "]";}
template <class T, class V> void print(pair <T, V> x) {cerr << "{"; print(x.first); cerr << ' '; print(x.second); cerr << "}";}
template <class T, class V> void print(map <T, V> x) {cerr << "[ "; for(auto i : x) {print(i); cerr << ' ';} cerr << "]";}

#define ll long long
#define pb push_back
#define ppb pop_back
#define PII pair <int, int>
#define PLL pair <ll, ll>
#define all(v) (v).begin(), (v).end()
#define OK cerr << "OK\n";
#define MP make_pair

const int N0 = 260;
vector <int> G[N0];
int dist[N0], n, par[N0];

void dfs(int node, int parent, int d){
    dist[node] = d;
    par[node] = parent;
    for(int u : G[node]){
        if(u == parent) continue;
        dfs(u, node, d + 1);
    }
}

void clearh(int n){
    for(int i = 0; i < n; i++){
        G[i].clear();
    }
}

vector <int> longest_trip(int N, int D){
    clearh(N);
    n = N;
    int edge_count = 0;
    for(int i = 0; i < N; i++){
        for(int j = i + 1; j < N; j++){
            if(are_connected({i}, {j})){
                G[i].pb(j);
                G[j].pb(i);
                edge_count++;
            }
        }
    }

    vector <int> res;
    if(edge_count == N * (N - 1) / 2){ 
        for(int i = 0; i < N; i++) res.pb(i);
        return res;
    }

    if(edge_count != N - 1){
        assert(edge_count == N);
    }

    int root = 0, mx_depth = -1;
    for(int i = 0; i < n; i++){
        dfs(i, -1, 0);
        int mx = -1, leaf = i;
        for(int j = 0; j < n; j++){
            if(dist[j] > mx){
                mx = max(mx, dist[j]);
                leaf = j;
            }
        }
        if(mx > mx_depth){
            mx_depth = mx;
            root = i;
        }
    }
    dfs(root, -1, 0);
    int leaf = root, mx = -1;
    for(int i = 0; i < n; i++){
        if(dist[i] > mx){
            mx = dist[i];
            leaf = i;
        }
    }
    while(leaf != -1){
        res.pb(leaf);
        leaf = par[leaf];
    }
    return res;
}
#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...