Submission #1352856

#TimeUsernameProblemLanguageResultExecution timeMemory
1352856darkdevilvaqifEaster Eggs (info1cup17_eastereggs)C++20
100 / 100
6 ms516 KiB
#include <bits/stdc++.h>
#include "grader.h"

using namespace std;

// Using a vector inside findEgg or clearing globals properly is vital
vector<int> adj[513];
vector<int> dfs_order;

void dfs(int u, int p) {
    dfs_order.push_back(u);
    for (int v : adj[u]) {
        if (v != p) dfs(v, u);
    }
}

int findEgg(int N, vector<pair<int, int>> bridges) {
    // 1. Reset everything (Important for multiple calls in one test)
    for (int i = 1; i <= N; i++) adj[i].clear();
    dfs_order.clear();

    for (auto edge : bridges) {
        adj[edge.first].push_back(edge.second);
        adj[edge.second].push_back(edge.first);
    }

    // 2. Generate DFS order starting from island 1
    dfs(1, 0);

    // 3. Binary Search with the Exclusion Principle
    // We only search up to N-2. If it's not in the first N-1 nodes, 
    // it must be the last node (dfs_order[N-1]).
    int low = 0, high = N - 2; 
    int ans = dfs_order[N - 1]; 

    while (low <= high) {
        int mid = low + (high - low) / 2;
        
        vector<int> query_set;
        for (int i = 0; i <= mid; i++) {
            query_set.push_back(dfs_order[i]);
        }

        if (query(query_set)) {
            ans = dfs_order[mid]; // Egg is in this prefix
            high = mid - 1;
        } else {
            low = mid + 1; // Egg is in the suffix
        }
    }

    return ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...