# | TimeUTC-0 | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1146237 | rado15 | Easter Eggs (info1cup17_eastereggs) | C++20 | 0 ms | 0 KiB |
#include <vector>
#include <algorithm>
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);