Submission #948751

# Submission time Handle Problem Language Result Execution time Memory
948751 2024-03-18T13:07:35 Z thinknoexit Longest Trip (IOI23_longesttrip) C++17
Compilation error
0 ms 0 KB
#include <bits/stdc++.h>
#include "longesttrip.h"
using namespace std;
using ll = long long;
int n, d;
vector<int> longest_trip(int N, int D) {
    n = N;
    d = D;
    vector<vector<int>> adj(n, vector<int>(n, 1));
    vector<int> deg(n, 0), p(n, 0);
    for (int i = 0;i < n;i++) {
        for (int j = i + 1;j < n;j++) {
            adj[i][j] = adj[j][i] = are_connected({ i }, { j });
            if (adj[i][j]) deg[i]++, deg[j]++;
        }
    }
    set<int> rem;
    for (int i = 0;i < n;i++) if (deg[i] != n - 1) rem.insert(i);
    queue<int> q;
    while (!rem.empty()) {
        vector<int> udp;
        for (auto& i : rem) {
            q.push(i);
            p[i] = 1;
            rem.erase(i);
            break;
        }
        while (!q.empty()) {
            int v = q.front();
            q.pop();
            udp.push_back(v);
            vector<int> del;
            for (auto& i : rem) {
                if (!p[i] && !adj[v][i]) {
                    p[i] = 3 - p[v];
                    del.push_back(i);
                    q.push(i);
                }
            }
            for (auto& x : del) rem.erase(x);
        }
        bool ch = 0;
        for (int i = 0;i < n;i++) {
            for (int j = 0;j < n;j++) {
                if (!p[i] || !p[j]) continue;
                if (adj[i][j] && p[i] != p[j]) {
                    ch = 1;
                    break;
                }
            }
        }
        if (ch) for (auto& x : udp) p[i] = 3 - p[i];
    }
    vector<int> r1, r2;
    for (int i = 0;i < n;i++) {
        if (!p[i]) p[i] = 1;
        if (p[i] == 1) r1.push_back(i);
        else r2.push_back(i);
    }
    if ((int)r1.size() == n) return r1; // Complete Graph
    int s1 = r1.size(), s2 = r2.size();
    for (int i = 0;i < s1;i++) {
        for (int j = 0;j < s2;j++) {
            if (adj[r1[i]][r2[j]]) {
                swap(r1[s1 - 1], r1[i]);
                swap(r2[0], r2[j]);
                vector<int> ans;
                for (auto& x : r1) ans.push_back(x);
                for (auto& x : r2) ans.push_back(x);
                return ans;
            }
        }
    }
    if ((int)r1.size() > (int)r2.size()) return r1;
    return r2;
}

Compilation message

longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:52:39: error: 'i' was not declared in this scope
   52 |         if (ch) for (auto& x : udp) p[i] = 3 - p[i];
      |                                       ^
longesttrip.cpp:52:28: warning: unused variable 'x' [-Wunused-variable]
   52 |         if (ch) for (auto& x : udp) p[i] = 3 - p[i];
      |                            ^