Submission #1146240

#TimeUsernameProblemLanguageResultExecution timeMemory
1146240rado15Easter Eggs (info1cup17_eastereggs)C++20
Compilation error
0 ms0 KiB
#include <vector>
#include <algorithm>
#include "grader.h"

using namespace std;

// Function to find the centroid of a tree
int findCentroid(int u, int parent, const vector<vector<int>>& adj, vector<int>& subtreeSize, int totalNodes) {
    subtreeSize[u] = 1;
    for (int v : adj[u]) {
        if (v != parent) {
            subtreeSize[u] += findCentroid(v, u, adj, subtreeSize, totalNodes);
        }
    }
    if (subtreeSize[u] * 2 >= totalNodes) {
        return u;
    }
    return -1;
}

// Function to perform the query
int query(const vector<int>& islands);

// Recursive function to find the Easter Egg
int findEggHelper(int u, int parent, const vector<vector<int>>& adj, vector<int>& subtreeSize) {
    int totalNodes = subtreeSize[u];
    int centroid = findCentroid(u, parent, adj, subtreeSize, totalNodes);
    
    // Prepare the query set
    vector<int> querySet;
    querySet.push_back(centroid);
    
    // Query the centroid
    if (query(querySet)) {
        return centroid;
    }
    
    // If not found, search in the subtrees
    for (int v : adj[centroid]) {
        if (v != parent && subtreeSize[v] * 2 > totalNodes) {
            return findEggHelper(v, centroid, adj, subtreeSize);
        }
    }
    
    return -1; // Should not reach here
}

// Main function to find the Easter Egg
int findEgg(int N, vector<pair<int, int>> bridges) {
    // Build the adjacency list
    vector<vector<int>> adj(N + 1);
    for (auto& bridge : bridges) {
        adj[bridge.first].push_back(bridge.second);
        adj[bridge.second].push_back(bridge.first);
    }
    
    // Initialize subtree sizes
    vector<int> subtreeSize(N + 1, 0);
    
    // Start the search from the root (island 1)
    return findEggHelper(1, -1, adj, subtreeSize);
}

Compilation message (stderr)

eastereggs.cpp: In function 'int findEggHelper(int, int, const std::vector<std::vector<int> >&, std::vector<int>&)':
eastereggs.cpp:34:14: error: call of overloaded 'query(std::vector<int>&)' is ambiguous
   34 |     if (query(querySet)) {
      |         ~~~~~^~~~~~~~~~
In file included from eastereggs.cpp:3:
grader.h:6:5: note: candidate: 'int query(std::vector<int>)'
    6 | int query(vector < int > islands);
      |     ^~~~~
eastereggs.cpp:22:5: note: candidate: 'int query(const std::vector<int>&)'
   22 | int query(const vector<int>& islands);
      |     ^~~~~