답안 #952494

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
952494 2024-03-24T06:26:08 Z kilkuwu Easter Eggs (info1cup17_eastereggs) C++17
100 / 100
19 ms 1504 KB
#include <bits/stdc++.h>
#ifndef LOCAL
#include "grader.h"
#else

using namespace std;

int findEgg(int N, vector < pair < int, int > > bridges);
int query(vector < int > islands);
#endif

#ifdef LOCAL
#include "template\debug.hpp"
#else
#define dbg(...) ;
#define timer(...) ;
#endif

int findEgg (int N, vector<pair<int, int>> bridges) {
  std::vector<std::vector<int>> adj(N + 1);

  for (auto bridge : bridges) {
    int u, v;
    std::tie(u, v) = bridge;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }

  std::vector<int> maybe(N + 1, 1);

  // bfs and take half each time

  int num_candidates = N;
  while (num_candidates > 1) {
    dbg(num_candidates);
    std::vector<int> to_query;
    std::vector<int> inside(N + 1);
    int target = num_candidates / 2;
    std::function<void(int, int)> dfs = [&](int u, int p) -> void {
      if (target == 0) {
        return;
      }
      to_query.push_back(u);
      target -= maybe[u];
      inside[u] = 1;

      for (int v : adj[u]) {
        if (v == p) continue;
        dfs(v, u);
      }
    };

    dfs(1, 1);

    int res = query(to_query);
    if (res == 1) {
      num_candidates = 0;
      for (int i = 1; i <= N; i++) {
        if (!inside[i]) {
          maybe[i] = 0;
        }
        num_candidates += maybe[i];
      }
    } else {
      num_candidates = 0;
      for (int i = 1; i <= N; i++) {
        if (inside[i]) {
          maybe[i] = 0;
        }
        num_candidates += maybe[i];
      }
    }
  }

  for (int i = 1; i <= N; i++) {
    if (maybe[i]) return i;
  }

  assert(false);
}

#ifdef LOCAL

#include<bits/stdc++.h>

using namespace std;

static int N, X, cntQ;
static vector < int > v[1009];

int query (vector < int > h)
{
    cntQ ++;
    int ap[1009];
    if (h.empty ()) return 0;
    for (int i=1; i<=N; i++)
        ap[i] = 0;
    for (auto it = h.begin (); it != h.end (); it ++)
        ap[*it] = 1;
    queue < int > cc;
    cc.push (h[0]), ap[h[0]] = 2;
    while (!cc.empty ())
    {
        int nod = cc.front ();
        cc.pop ();
        for (auto it = v[nod].begin (); it != v[nod].end (); it ++)
            if (ap[*it] == 1)
                ap[*it] = 2, cc.push (*it);
    }
    for (int i=1; i<=N; i++)
        if (ap[i] == 1) return -1;
    for (auto it = h.begin (); it != h.end (); it ++)
        if (*it == X) return 1;
    return 0;
}

int main ()
{
// freopen ("input", "r", stdin);
//freopen ("output", "w", stdout);

scanf ("%d", &N);
int Queries;
vector < pair < int, int > > param;
for (int i=1; i<N; i++)
{
    int x, y;
    scanf ("%d %d", &x, &y);
    v[x].push_back (y);
    v[y].push_back (x);
    param.push_back ({x, y});
}
scanf ("%d", &Queries);
while (Queries --)
{
    scanf ("%d", &X), cntQ = 0;
    int Y = findEgg (N, param);
    if (X != Y)
    {
        printf ("WA %d instead of %d\n", Y, X);
        return 0;
    }
    printf ("OK %d\n", cntQ);
}
return 0;
}
#endif
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Number of queries: 4
2 Correct 1 ms 344 KB Number of queries: 4
3 Correct 1 ms 344 KB Number of queries: 4
4 Correct 1 ms 344 KB Number of queries: 4
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 976 KB Number of queries: 8
2 Correct 10 ms 744 KB Number of queries: 9
3 Correct 15 ms 752 KB Number of queries: 9
4 Correct 13 ms 740 KB Number of queries: 9
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 1024 KB Number of queries: 9
2 Correct 13 ms 1248 KB Number of queries: 9
3 Correct 17 ms 1504 KB Number of queries: 9
4 Correct 14 ms 936 KB Number of queries: 9