Submission #948775

# Submission time Handle Problem Language Result Execution time Memory
948775 2024-03-18T14:00:07 Z thinknoexit Longest Trip (IOI23_longesttrip) C++17
0 / 100
660 ms 1500 KB
#include <bits/stdc++.h>
#include "longesttrip.h"
using namespace std;
using ll = long long;
int n, d;
vector<int> adj2[266];
int col[266];
vector<int> r1, r2;
vector<int> t1, t2;
void dfs(int v) {
    if (col[v] == 1) t1.push_back(v);
    else t2.push_back(v);
    for (auto& x : adj2[v]) {
        if (!col[x]) col[x] = 3 - col[v], dfs(x);
    }
}
vector<int> longest_trip(int N, int D) {
    n = N;
    d = D;
    vector<vector<int>> adj(n, vector<int>(n, 1));
    r1.clear(), r2.clear();
    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]) adj2[i].push_back(j), adj2[j].push_back(i);
        }
    }
    {
        vector<int> g1, g2;
        for (int i = 1;i < n;i++) {
            if (adj[0][i]) g1.push_back(i);
            else g2.push_back(i);
        }
        bool gg = 0;
        for (auto& x : g1) for (auto& y : g2) if (adj[x][y]) gg = 1;
        if (!gg) {
            if (g1.size() > g2.size()) return g1;
            return g2;
        }
    }
    int tot = 0;
    for (int i = 0;i < n;i++) {
        if (!col[i]) {
            col[i] = 1;
            dfs(i);
            if (tot == 0) {
                tot = 1;
            }
            else {
                bool ch = 0;
                for (auto& x : r1) for (auto& y : t2) if (adj[x][y]) ch = 1;
                for (auto& x : r2) for (auto& y : t1) if (adj[x][y]) ch = 1;
                if (ch) {
                    swap(t1, t2);
                    for (auto& x : t1) col[x] = 3 - col[x];
                    for (auto& x : t2) col[x] = 3 - col[x];
                }
            }
            for (auto& x : t1) r1.push_back(x);
            for (auto& x : t2) r2.push_back(x);
            t1.clear(), t2.clear();
        }
    }
    for (int i = 0;i < n;i++) adj2[i].clear(), col[i] = 0;
    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[i], r1[s1 - 1]);
                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;
            }
        }
    }
}

Compilation message

longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:20:49: warning: control reaches end of non-void function [-Wreturn-type]
   20 |     vector<vector<int>> adj(n, vector<int>(n, 1));
      |                                                 ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 151 ms 1500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 344 KB Incorrect
2 Halted 0 ms 0 KB -
# 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 Correct 25 ms 344 KB Output is correct
3 Correct 126 ms 344 KB Output is correct
4 Correct 343 ms 592 KB Output is correct
5 Correct 660 ms 772 KB Output is correct
6 Incorrect 0 ms 344 KB Incorrect
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 344 KB Incorrect
2 Halted 0 ms 0 KB -