#include #include #include #include #include #include #include "validate.h" using namespace std; class Graph { public: Graph(int vertices); void addEdge(int u, int v); bool hasCycle(); private: int vertices; std::vector > adj; std::vector visited; std::vector recStack; bool isCyclic(int v); }; Graph::Graph(int vertices) { this->vertices = vertices; adj.resize(vertices); visited.assign(vertices, false); recStack.assign(vertices, false); } void Graph::addEdge(int u, int v) { adj[u].push_back(v); } bool Graph::isCyclic(int v) { if (!visited[v]) { visited[v] = true; recStack[v] = true; for (int neighbor : adj[v]) { if (!visited[neighbor] && isCyclic(neighbor)) { return true; } else if (recStack[neighbor]) { return true; } } } recStack[v] = false; return false; } bool Graph::hasCycle() { for (int i = 0; i < vertices; ++i) { if (isCyclic(i)) { return true; } } return false; } int main(int argc, char **argv) { init_io(argc, argv); int n; int m; judge_in >> n >> m; vector a; vector b; for (int i = 0; i < m; ++i) { int ai, bi; judge_in >> ai >> bi; a.push_back(ai); b.push_back(bi); } string correct_ans; string author_ans; judge_ans >> correct_ans; author_out >> author_ans; if (author_ans == "YES") { int k; if (!(author_out >> k)) { wrong_answer("Number of edges not present in the contestant output"); } if (k < m / 2) { wrong_answer("k < m/2; k=%d; m/2=%d", k, m / 2); } set edges; for (int i = 0; i < k; ++i) { int e; if (!(author_out >> e)) { wrong_answer("Not enough edges provided"); } if (e > m || e < 1) { wrong_answer("Incorrect edge number: %d (m=%d)", e, m); } if (edges.find(e) != edges.end()) { wrong_answer("Edge %d mentioned twice in the contestant output", e); } edges.insert(e); } Graph g(n); for (set::iterator it = edges.begin(); it != edges.end(); ++it) { int e = *it; g.addEdge(a.at(e - 1) - 1, b.at(e - 1) - 1); } if (g.hasCycle()) { wrong_answer("Contestant output is not acyclic"); } if (correct_ans == "YES") { accept(); } else if (correct_ans == "NO") { judge_error("Contestant has found the solution, while jury has not"); } } else if (author_ans == "NO") { wrong_answer("Solution exists, while contestant output is NO"); } else { wrong_answer("Not YES or NO: " + author_ans); } // Should never be reached accept(); }