#include "maze.h" #include #include #include #include using namespace std; static int n, num_queries; static long long total_cost; static vector> is_connected; static vector> reported_passageways; static vector seen_in_query; int query(vector A, vector B) { if (A.empty()) { printf("Invalid query: A is empty\n"); exit(0); } if (B.empty()) { printf("Invalid query: B is empty\n"); exit(0); } num_queries++; vector > AB = { A, B }; for (auto &v : AB) { for (int a : v) { if (a < 1 || a > n) { printf("Invalid query: invalid room number\n"); exit(0); } if (seen_in_query[a] == num_queries) { printf("Invalid query: Room %d appeared more than once in the query\n", a); exit(0); } seen_in_query[a] = num_queries; } } total_cost += A.size() * B.size(); int answer = 0; for (int a : A) { for (int b : B) { answer += is_connected[a][b]; } } return answer; } void report_passageway(int i, int j) { reported_passageways.push_back(make_pair(i, j)); } static void find_connections(int begin, int at, vector > &adj) { if (is_connected[begin][at]) return; is_connected[begin][at] = true; for (int b : adj[at]) { find_connections(begin, b, adj); } } int main() { int res = scanf("%d", &n); if (res != 1) { printf("Failed to read N\n"); return 0; } vector> adj(n+1); vector> passageways; for (int i = 1; i < n; i++) { int a, b; res = scanf("%d%d", &a, &b); if (res != 2) { printf("Failed to read passageway\n"); return 0; } if (a < 1 || a > n || b < 1 || b > n || a == b) { printf("Invalid passageway in input\n"); return 0; } adj[a].push_back(b); passageways.push_back(make_pair(a, b)); } is_connected = vector>(n+1, vector(n+1)); for (int i = 1; i <= n; i++) { find_connections(i, i, adj); } seen_in_query = vector(n+1); map_maze(n); vector> sorted_reported_passageways = reported_passageways; sort(passageways.begin(), passageways.end()); sort(sorted_reported_passageways.begin(), sorted_reported_passageways.end()); if (total_cost > 10000000) { printf("Total cost too high: %lld\n", total_cost); } else if (sorted_reported_passageways != passageways) { printf("Incorrect\n"); printf("Reported the following passageways:\n"); for (pair p : reported_passageways) { printf("%d %d\n", p.first, p.second); } } else { printf("Correct\n"); } printf("Used %d queries\n", num_queries); }