답안 #446382

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
446382 2021-07-21T19:32:10 Z aryan12 Easter Eggs (info1cup17_eastereggs) C++17
100 / 100
21 ms 480 KB
#include <bits/stdc++.h>
#include "grader.h"

using namespace std;
const int MAXN = 520;
vector<int> order;
vector<int> g[MAXN];

void dfs(int node, int par) {
    order.push_back(node);
    for(int i = 0; i < g[node].size(); i++) {
        if(g[node][i] != par) {
            dfs(g[node][i], node);
        }
    }
}

int findEgg(int N, vector<pair<int, int> > bridges) {
    if(N > 16 && N <= 500) {
        vector<int> queryVector;
        order.clear();
        for(int i = 0; i < MAXN; i++) {
            g[i].clear();
        }
        for(int i = 0; i < bridges.size(); i++) {
            g[bridges[i].first].push_back(bridges[i].second);
            g[bridges[i].second].push_back(bridges[i].first);
        }
        dfs(1, -1);
        int l = 0, r = order.size() - 1;
        int ans = order.size(), mid;
        while(l <= r) {
            mid = (l + r) >> 1;
            vector<int> tempVector;
            for(int i = 0; i < queryVector.size(); i++) {
                tempVector.push_back(queryVector[i]);
            }
            for(int i = l; i <= mid; i++) {
                tempVector.push_back(order[i]);
            }
            int foundSpecial = query(tempVector);
            assert(foundSpecial != -1);
            if(foundSpecial == 1) {
                ans = mid;
                r = mid - 1;
            }
            else {
                for(int i = l; i <= mid; i++) {
                    queryVector.push_back(order[i]);
                }
                l = mid + 1;
            }
        }
        return order[ans];
    }
    vector<int> queryVector;
    order.clear();
    for(int i = 0; i < MAXN; i++) {
        g[i].clear();
    }
    for(int i = 0; i < bridges.size(); i++) {
        g[bridges[i].first].push_back(bridges[i].second);
        g[bridges[i].second].push_back(bridges[i].first);
    }
    dfs(1, -1);
    int l = 0, r = order.size() - 1;
    int ans = order.size(), mid;
    while(l <= r) {
        mid = (l + r) >> 1;
        vector<int> tempVector;
        for(int i = 0; i < queryVector.size(); i++) {
            tempVector.push_back(queryVector[i]);
        }
        for(int i = l; i <= mid; i++) {
            tempVector.push_back(order[i]);
        }
        int foundSpecial = query(tempVector);
        assert(foundSpecial != -1);
        if(l == r - 1) {
            if(foundSpecial == 1) {
                ans = mid;
                break;
            }
            else {
                ans = mid + 1;
                break;
            }
        }
        if(foundSpecial == 1) {
            ans = mid;
            r = mid - 1;
        }
        else {
            for(int i = l; i <= mid; i++) {
                queryVector.push_back(order[i]);
            }
            l = mid + 1;
        }
    }
    return order[ans];
}

Compilation message

eastereggs.cpp: In function 'void dfs(int, int)':
eastereggs.cpp:11:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   11 |     for(int i = 0; i < g[node].size(); i++) {
      |                    ~~^~~~~~~~~~~~~~~~
eastereggs.cpp: In function 'int findEgg(int, std::vector<std::pair<int, int> >)':
eastereggs.cpp:25:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   25 |         for(int i = 0; i < bridges.size(); i++) {
      |                        ~~^~~~~~~~~~~~~~~~
eastereggs.cpp:35:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |             for(int i = 0; i < queryVector.size(); i++) {
      |                            ~~^~~~~~~~~~~~~~~~~~~~
eastereggs.cpp:61:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |     for(int i = 0; i < bridges.size(); i++) {
      |                    ~~^~~~~~~~~~~~~~~~
eastereggs.cpp:71:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |         for(int i = 0; i < queryVector.size(); i++) {
      |                        ~~^~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 200 KB Number of queries: 4
2 Correct 1 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
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 328 KB Number of queries: 9
2 Correct 13 ms 332 KB Number of queries: 9
3 Correct 21 ms 352 KB Number of queries: 9
4 Correct 21 ms 328 KB Number of queries: 9
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 480 KB Number of queries: 9
2 Correct 18 ms 336 KB Number of queries: 9
3 Correct 17 ms 328 KB Number of queries: 9
4 Correct 19 ms 328 KB Number of queries: 9