Submission #1372378


Source Code Expand

#include <iostream>
#include <vector>
#include <list>
#include <queue>

int main() {
  unsigned N;
  std::cin >> N;
  std::vector<std::vector<unsigned>> G(N);
  std::vector<std::vector<unsigned>> G0(N);
  for (unsigned i = 0; i < N - 1; ++i) {
    unsigned x, y;
    std::cin >> x >> y;
    --x;
    --y;
    G0[x].push_back(y);
    G0[y].push_back(x);
  }
  unsigned K;
  std::cin >> K;
  std::vector<std::pair<int, int>> color(N);
  for (auto &i : color) {
    i.first = std::numeric_limits<int>::min() / 2;
    i.second = std::numeric_limits<int>::max() / 2;
  }
  unsigned valid_index = 0;
  for (unsigned i = 0; i < K; ++i) {
    unsigned x, y;
    std::cin >> x >> y;
    valid_index = --x;
    color[x] = std::make_pair(y, y);
  }
  std::queue<unsigned> queue;
  std::vector<unsigned> order(N);
  std::vector<unsigned> rev_order(N);
  std::vector<bool> used(N, false);
  queue.push(valid_index);
  unsigned o = 0;
  while (!queue.empty()) {
    auto x = queue.front();
    used[x] = true;
    rev_order[x] = o;
    order[o++] = x;
    queue.pop();
    for (auto &e : G0[x]) {
      if (!used[e]) { queue.push(e); }
    }
  }
  for (unsigned i = 0; i < N; ++i) {
    for (auto &e : G0[i]) {
      if (rev_order[i] < rev_order[e]) {
        G[i].push_back(e);
      }
    }
  }
  std::vector<int> odd(N);
  odd[order[0]] = color[order[0]].first % 2;
  for (unsigned t = 0; t < N; ++t) {
    auto i = order[t];
    for (auto e : G[i]) { odd[e] = odd[i] == 1 ? 0 : 1; }
  }
  for (unsigned t = N; t-- > 0;) {
    auto i = order[t];
    for (auto e : G[i]) {
      color[i].first = std::max(color[i].first, color[e].first - 1);
      color[i].second = std::min(color[i].second, color[e].second + 1);
    }
    if (color[i].first % 2 != odd[i]) { ++color[i].first; }
    if (color[i].second % 2 != odd[i]) { --color[i].second; }
    if (color[i].first > color[i].second) {
      std::cout << "No" << std::endl;
      return 0;
    }
  }
  std::vector<int> ans(N);
  ans[order[0]] = color[order[0]].first;
  for (unsigned t = 0; t < N; ++t) {
    auto i = order[t];
    for (auto e : G[i]) {
      if (color[e].first <= ans[i] - 1) {
        ans[e] = ans[i] - 1;
      } else if (ans[i] + 1 <= color[e].second) {
        ans[e] = ans[i] + 1;
      } else {
        std::cout << "No";
        return 0;
      }
    }
  }
  std::cout << "Yes" << std::endl;
  for (unsigned i = 0; i < N; ++i) {
    std::cout << ans[i] << std::endl;
  }
  return 0;
}

Submission Info

Submission Time
Task E - Integers on a Tree
User ytsmiling
Language C++14 (Clang 3.8.0)
Score 800
Code Size 2541 Byte
Status AC
Exec Time 555 ms
Memory 14208 KB

Judge Result

Set Name sample All
Score / Max Score 0 / 0 800 / 800
Status
AC × 3
AC × 39
Set Name Test Cases
sample sample_01.txt, sample_02.txt, sample_03.txt
All binary_01.txt, binary_02.txt, hand_01.txt, hand_02.txt, hand_03.txt, kary_01.txt, kary_02.txt, kary_03.txt, line_01.txt, line_02.txt, line_03.txt, line_04.txt, line_05.txt, line_06.txt, random0_01.txt, random1_01.txt, random1_02.txt, random1_03.txt, random1_04.txt, random1_05.txt, random1_06.txt, random1_07.txt, random1_08.txt, random2_01.txt, random2_02.txt, random2_03.txt, random2_04.txt, random2_05.txt, random2_06.txt, random3_01.txt, random3_02.txt, random4_01.txt, random4_02.txt, random4_03.txt, sample_01.txt, sample_02.txt, sample_03.txt, star_01.txt, star_02.txt
Case Name Status Exec Time Memory
binary_01.txt AC 443 ms 12928 KB
binary_02.txt AC 176 ms 11648 KB
hand_01.txt AC 1 ms 256 KB
hand_02.txt AC 1 ms 256 KB
hand_03.txt AC 1 ms 256 KB
kary_01.txt AC 171 ms 11648 KB
kary_02.txt AC 360 ms 12160 KB
kary_03.txt AC 169 ms 11136 KB
line_01.txt AC 201 ms 13184 KB
line_02.txt AC 365 ms 14208 KB
line_03.txt AC 364 ms 14208 KB
line_04.txt AC 397 ms 14208 KB
line_05.txt AC 170 ms 13184 KB
line_06.txt AC 207 ms 13184 KB
random0_01.txt AC 260 ms 11904 KB
random1_01.txt AC 387 ms 13056 KB
random1_02.txt AC 388 ms 13056 KB
random1_03.txt AC 421 ms 13056 KB
random1_04.txt AC 469 ms 13056 KB
random1_05.txt AC 391 ms 13056 KB
random1_06.txt AC 388 ms 13056 KB
random1_07.txt AC 392 ms 13056 KB
random1_08.txt AC 555 ms 13056 KB
random2_01.txt AC 184 ms 11904 KB
random2_02.txt AC 186 ms 11904 KB
random2_03.txt AC 189 ms 12032 KB
random2_04.txt AC 197 ms 11904 KB
random2_05.txt AC 265 ms 11904 KB
random2_06.txt AC 346 ms 11904 KB
random3_01.txt AC 188 ms 11904 KB
random3_02.txt AC 267 ms 11904 KB
random4_01.txt AC 383 ms 13056 KB
random4_02.txt AC 388 ms 13056 KB
random4_03.txt AC 390 ms 13056 KB
sample_01.txt AC 1 ms 256 KB
sample_02.txt AC 1 ms 256 KB
sample_03.txt AC 1 ms 256 KB
star_01.txt AC 417 ms 12536 KB
star_02.txt AC 213 ms 11512 KB