답안 #967805

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
967805 2024-04-22T20:35:35 Z imann Burza (COCI16_burza) C++17
160 / 160
41 ms 1136 KB
#include <iostream>
#include <map>
#include <vector>
#include <array>
#include <queue>
#include <set>

const int MAX_N = 400 + 2;
const int MAX_H = 20 + 1;

std::array<std::vector<int>, MAX_N> graph;
std::array<int, MAX_N> parent, color, height;
std::array<std::pair<int, int>, MAX_N> interval;
std::map<int, int> toLeafIndex;
int leafCount;
std::array<int, (1 << MAX_H)> dp;
std::map<int, std::vector<int> > sameHeight;

void bfs(int k) {
  std::queue<int> q;
  height[0] = 0;
  sameHeight[0].push_back(0);
  q.push(0);
  color[0] = true;
  int index = 0;
  while (!q.empty()) {
    int n = q.front();
    q.pop();
    color[n] = true;
    for (int c : graph[n]) {
      if (!color[c]) {
        parent[c] = n;
        height[c] = height[n] + 1;
        sameHeight[height[c]].push_back(c);
        if (height[c] == k) {
          toLeafIndex[c] = index;
          index++;
          continue;
        }
        q.push(c);
      }
    }
  }
  leafCount = index;
}

bool solve(int n, int k) {
  bfs(k);
  bool flag = false;
  if (sameHeight[k].empty())
    return true;
  
  for (int i = 0; i < n; ++i) {
    interval[i] = {1e9, -1};
  }
  for (int i : sameHeight[k]) {
    int p = i;
    do {
      interval[p].first = std::min(interval[p].first, toLeafIndex[i]);
      interval[p].second = std::max(interval[p].second, toLeafIndex[i]);
      p = parent[p];
    } while (p != 0);
  }

  dp[0] = -1;
  for (int s = 1; s < (1 << k); ++s) {
    for (int i = 0; i < k; ++i) {
      if (s & (1 << i)) {
        int l = dp[s ^ (1 << i)];
        int nl = l;
        for (int sh : sameHeight[i + 1]) {
          if (interval[sh].first <= l + 1 && interval[sh].second >= l + 1) {
            nl = std::max(nl, interval[sh].second);
          }
        }
        dp[s] = std::max(dp[s], nl);
      }
    }
  }

  return dp[(1 << k) - 1] + 1 == leafCount;
}

int main() {
  int n, k; std::cin >> n >> k;
  for (int i = 0; i < n - 1; ++i) {
    int a, b; std::cin >> a >> b; a--; b--;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  if (k * k >= n) {
    std::cout << "DA" << std::endl;
  } else if (solve(n, k)) {
    std::cout << "DA" << std::endl;
  } else {
    std::cout << "NE" << std::endl;
  }
}

Compilation message

burza.cpp: In function 'bool solve(int, int)':
burza.cpp:49:8: warning: unused variable 'flag' [-Wunused-variable]
   49 |   bool flag = false;
      |        ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 348 KB Output is correct
2 Correct 39 ms 816 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 880 KB Output is correct
2 Correct 38 ms 920 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 39 ms 896 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 1136 KB Output is correct
2 Correct 38 ms 960 KB Output is correct
3 Correct 1 ms 456 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 600 KB Output is correct
2 Correct 39 ms 852 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 436 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 856 KB Output is correct
2 Correct 39 ms 852 KB Output is correct
3 Correct 1 ms 600 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 784 KB Output is correct
2 Correct 39 ms 856 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 968 KB Output is correct
2 Correct 37 ms 768 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 38 ms 960 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 604 KB Output is correct
2 Correct 39 ms 852 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 464 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 344 KB Output is correct
2 Correct 39 ms 756 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 600 KB Output is correct
2 Correct 38 ms 876 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 17 ms 552 KB Output is correct
6 Correct 1 ms 348 KB Output is correct