Submission #389270

# Submission time Handle Problem Language Result Execution time Memory
389270 2021-04-14T01:57:06 Z wiwiho Easter Eggs (info1cup17_eastereggs) C++14
100 / 100
37 ms 384 KB
#include <bits/stdc++.h>
#include "grader.h"

#define mp make_pair
#define F first
#define S second
#define eb emplace_back
#define printv(a, b) { \
    for(auto pv : a) b << pv << " "; \
    b << "\n"; \
}

using namespace std;

using pii = pair<int, int>;

int n;
vector<vector<int>> g;

vector<int> tmp;

void dfs1(int now, int p){
    tmp.eb(now);
    for(int i : g[now]){
        if(i == p) continue;
        dfs1(i, now);
    }
}

vector<int> qs;
set<int> ts;

bool dfsq(int now, int p){
    bool tmp = ts.find(now) != ts.end();
    //cerr << "dfsq " << now << " " << tmp << "\n";
    for(int i : g[now]){
        if(i == p) continue;
        tmp = dfsq(i, now) || tmp;
    }
    if(tmp) qs.eb(now);
    return tmp;
}

int qry(vector<int>& _s){
    qs.clear();
    ts.clear();
    for(int i : _s) ts.insert(i);
    dfsq(1, 1);
    //cerr << "real  ";
    //printv(qs, cerr);
    //printv(ts, cerr);
    return query(qs);
}

int qry(int l, int r){
    vector<int> s;
    for(int i = l; i <= r; i++) s.eb(tmp[i]);
    //cerr << "qry " << l << " " << r << "  ";
    //printv(s, cerr);
    return qry(s);
}

int findEgg (int N, vector<pii> bridges){
    //cerr << "find " << N << "\n";
    g.clear();
    tmp.clear();
    n = N;
    g.resize(n + 1);
    for(int i = 0; i < n - 1; i++){
        int u, v;
        tie(u, v) = bridges[i];
        g[u].eb(v);
        g[v].eb(u);
    }

    dfs1(1, 1);

    //printv(tmp, cerr);

    int l = 0, r = n - 1;
    while(l < r){
        int m = (l + r) / 2;
        if(!qry(l, m)) l = m + 1;
        else r = m;
    }

    return tmp[l];
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 200 KB Number of queries: 4
2 Correct 2 ms 200 KB Number of queries: 4
3 Correct 1 ms 200 KB Number of queries: 4
4 Correct 1 ms 200 KB Number of queries: 4
# Verdict Execution time Memory Grader output
1 Correct 8 ms 340 KB Number of queries: 8
2 Correct 18 ms 328 KB Number of queries: 9
3 Correct 33 ms 352 KB Number of queries: 9
4 Correct 21 ms 328 KB Number of queries: 9
# Verdict Execution time Memory Grader output
1 Correct 37 ms 384 KB Number of queries: 9
2 Correct 28 ms 328 KB Number of queries: 9
3 Correct 30 ms 328 KB Number of queries: 9
4 Correct 23 ms 328 KB Number of queries: 9