Submission #960365

# Submission time Handle Problem Language Result Execution time Memory
960365 2024-04-10T10:13:49 Z TAhmed33 Longest Trip (IOI23_longesttrip) C++17
Compilation error
0 ms 0 KB
#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;
vector <int> adj[256], tree[256];
bool vis[256];
struct DSU {
    int p[256], sze[256];
    void init (int n) {
        for (int i = 0; i < n; i++) {
            p[i] = i; sze[i] = 1;
        }
    }
    int find (int x) {
        return p[x] == x ? x : p[x] = find(p[x]);
    }
    bool merge (int a, int b) {
        a = find(a), b = find(b);
        if (a == b) return 0;
        if (sze[a] > sze[b]) swap(a, b);
        sze[b] += sze[a]; p[a] = b;
        return 1;
    }
} cur;
vector <int> x;
int p[256];
void dfs (int pos) {
    vis[pos] = 1; x.push_back(pos);
    for (auto j : adj[pos]) {
        if (vis[j]) continue;
        tree[pos].push_back(j);
        p[j] = pos;
        dfs(j);
    }
}
int get (int x) {
    if (tree[x].empty()) return x;
    return get(tree[x][0]);
}
vector <int> longest_trip (int n, int d) {
    for (int i = 0; i < n; i++) adj[i].clear(), tree[i].clear();
    memset(p, -1, sizeof(p));
    cur.init(n);
    for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
            if (are_connected({i}, {j}) || adj2[i][j]) {
                adj[i].push_back(j);
                adj[j].push_back(i);
                cur.merge(i, j);
            }
        }
    }
    bool flag = 0;
    for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
            flag |= cur.merge(i, j);
        }
    }
    if (flag) {
        memset(vis, 0, sizeof(vis));
        vector <int> ret; 
        for (int i = 0; i < n; i++) {
            if (!vis[i]) {
                x.clear(); dfs(i);
                if ((int)x.size() > (int)ret.size()) ret = x;
            }
        }
        return ret;
    }
    memset(vis, 0, sizeof(vis));
    x.clear(); dfs(0);
    int pos = -1;
    for (int i = 0; i < n; i++) {
        if ((int)tree[i].size() == 2) {
            pos = i;
        }
    }
    if (pos == -1) return x;
    int u = get(tree[pos][0]), v = get(tree[pos][1]);
    if (p[pos] != -1 && find(adj[p[pos]].begin(), adj[p[pos]].end(), u) == adj[p[pos]].end()) {
        swap(u, v); swap(tree[pos][0], tree[pos][1]);
    }
    if (p[pos] == -1 && find(adj[pos].begin(), adj[pos].end(), u) == adj[pos].end()) {
        swap(u, v); swap(tree[pos][0], tree[pos][1]);
    }
    u = tree[pos][0], v = tree[pos][1];
    while (!x.empty() && x.back() != p[pos]) x.pop_back();
    vector <int> l;
    while (true) {
        l.push_back(u);
        if (tree[u].empty()) break;
        u = tree[u][0];
    }
    reverse(l.begin(), l.end());
    for (auto i : l) x.push_back(i);
    x.push_back(pos);
    while (true) {
        x.push_back(v);
        if (tree[v].empty()) break;
        v = tree[v][0];
    }
    return x;   
}

Compilation message

longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:45:44: error: 'adj2' was not declared in this scope; did you mean 'adj'?
   45 |             if (are_connected({i}, {j}) || adj2[i][j]) {
      |                                            ^~~~
      |                                            adj