#include "validation.h" using std::vector; using std::pair; using std::set; using std::cbrt; // A validator. It checks if all consecutive vertices in the answer are at distance of at most three as follows: // // Categorize vertices into those with large degree and those with small degree with border value c = sqrt_3(m^2/n). // For every large degree vertex, determine the set of all nodes at distance 2 with a BFS. This takes O(m) time per large degree vertex. // Since the total degree is O(m), there are at most O(m/c) = O(sqrt_3(mn)) large degree vertices. // Thus, this step takes O(sqrt_3(m^4n)) time total. // // Now, for every pair of consecutive vertices in the anwer, do the following: // If one of the nodes has large degree, then check for each neighbor of the other node if it occurs in the distance-2-set of the large node. // This takes O(size-of-neighborhood) time for each check and thus O(m) time over all checks of this form. // If both nodes have small degree, then check for each neighbor of the first vertex and each neighbor of the second vertex if there is an edge between them. // This takes O(size-of-first-neighborhood * size-of-second-neighborhood) time for each check. // Since both nodes have small degree, this is O(sqrt_3(m^4/n^2)). Since there are at most n checks, this is O(sqrt_3(m^4n)) time overall. // // In total, the validator runs in O(sqrt_3(m^4n)) time which is doable for m,n < 2*10^5. int main(int argc, char **argv) { // Set up the input and answer streams. std::ifstream in(argv[1]); std::ifstream ans(argv[2]); // Only for custom checker. OutputValidator v(argc, argv); // Read graph int n,m; in >> n >> m; vector> graph(n,vector{}); set> edges; for (int i=0;i> x >> y; x--; y--; graph[x].push_back(y); graph[y].push_back(x); edges.insert({x,y}); edges.insert({y,x}); } // Compute large and small degree vertices int c = cbrt(m*m/n); vector is_large(n,false); for (int i=0;i c; // Precompute distance 2 sets vector> N2(n,set{}); for (int i=0;i answer = v.read_integers("answer", n, 1, n, Unique); // Check team answer for (int i=0;i