Submission #1020173

#TimeUsernameProblemLanguageResultExecution timeMemory
1020173bobbilykingLongest Trip (IOI23_longesttrip)C++17
5 / 100
913 ms864 KiB
#include "longesttrip.h" using namespace std; #include <bits/stdc++.h> #define F(i, l, r) for (int i = (l); i < (r); ++i) #define A(a) (a).begin(), (a).end() int edge[300][300]; int parent[300]; int find(int i) { return i == parent[i] ? i : parent[i] = find(parent[i]); } int GN; bool query(int A, int B) { assert(min(A, B) >= 0 and max(A, B) < GN); return edge[A][B] | edge[B][A]; } void verify(vector<int> v) { set<int> cum(A(v)); F(i, 1, v.size()) assert(query(v[i-1], v[i])); assert(v.size() == cum.size()); for (auto x: v) assert(x >= 0 and x < GN); } vector<int> longest_trip(int N, int D) { GN = N; // okay we're just going to build the spanning tree straight up, fuck this shit. iota(parent, parent + N, 0ll); F(i, 0, N) F(j, i + 1, N) { if (are_connected({i}, {j})) { parent[find(i)] = find(j); edge[i][j] = edge[j][i] = 1; } } F(i, 0, N) if (find(i) != find(0)) { assert(D == 1); vector<int> v[2]; F(j, 0, N) { v[find(j) == find(0)].push_back(j); assert(find(j) == find(i) || find(j) == find(0)); } if (v[0].size() < v[1].size()) swap(v[0], v[1]); verify(v[0]); assert(v[0].size() >= v[1].size()); return v[0]; } vector<int> hamil; auto gen = [&]() { set<int> v; F(i, 0, N) if (find(A(hamil), i) == hamil.end()) v.insert(i); return v; }; if (query({0}, {1})) { hamil = {0, 1}; } else if (query({1}, {2})) { hamil = {1, 2}; } else { hamil = {0, 2}; } while (hamil.size() != N) { // this will reach N, with the exception of one case // cout << "HOW " << endl; verify(hamil); if (query({hamil[0]}, {hamil.back()})) { // its true that we have a hamiltonian CYCLE, so any rotation is valid. // (well in the case of n=2 behaves like it for our purposes) } else { // Pick any node not in hamil, this must be connected to either the front or the back. auto i = *gen().begin(); if (query({hamil[0]}, {i})) hamil.insert(hamil.begin(), i); else { assert(query(hamil.back(), i)); hamil.push_back(i); } continue; } // Okay, we want to find *any* connection between hamil and others, if exists already. auto bad = gen(); bool seen_edge = false; F(___, 0, hamil.size()) { auto v = hamil.back(); hamil.pop_back(); hamil.insert(hamil.begin(), v); verify(hamil); for (auto y: bad) if (edge[v][y]) { hamil.insert(hamil.begin(), y); verify(hamil); seen_edge = true; goto end; } } end: assert(seen_edge); // we already handle the CC = 2 case up there in this brute force approach } verify(hamil); assert(hamil.size() == N); return hamil; }

Compilation message (stderr)

longesttrip.cpp: In function 'void verify(std::vector<int>)':
longesttrip.cpp:5:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    5 | #define F(i, l, r) for (int i = (l); i < (r); ++i)
      |                                        ^
longesttrip.cpp:23:5: note: in expansion of macro 'F'
   23 |     F(i, 1, v.size()) assert(query(v[i-1], v[i]));
      |     ^
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:72:25: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   72 |     while (hamil.size() != N) { // this will reach N, with the exception of one case
      |            ~~~~~~~~~~~~~^~~~
longesttrip.cpp:5:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    5 | #define F(i, l, r) for (int i = (l); i < (r); ++i)
      |                                        ^
longesttrip.cpp:95:9: note: in expansion of macro 'F'
   95 |         F(___, 0, hamil.size()) {
      |         ^
In file included from /usr/include/c++/10/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
                 from longesttrip.cpp:3:
longesttrip.cpp:111:25: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  111 |     assert(hamil.size() == N);
      |            ~~~~~~~~~~~~~^~~~
#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...