Submission #1835685


Source Code Expand

#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define int ll
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a<=x&&x<b)
#define MP make_pair
#define PB push_back
#ifdef int
const ll INF = (1LL<<60);
#else
const int INF = (1LL<<30);
#endif
const double PI = 3.14159265359;
const double EPS = 1e-12;
const int MOD = 1000000007;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template<class S,class T>
ostream &operator <<(ostream& out,const pair<S,T>& a){
  out<<'('<<a.first<<','<<a.second<<')';
  return out;
}

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

VI g[100010];
int p[100010];
PII x[100010];
int rn;

PII dfs(int v, int par, int d) {
  if(g[v].size() == 1 && par != -1) {
    if(p[v] == -1) {
      x[v] = {-INF, INF};
    } else {
      x[v] = {p[v], p[v]};
      if(abs(p[v] - rn)%2 != d%2) x[v] = {INF, -INF};
    }
    // cout << v << " " << x[v] << endl;
    return x[v];
  } else {
    PII ret = {-INF, INF};
    for(int i: g[v]) {
      if(i == par) continue;
      PII tmp = dfs(i, v, d+1);
      // tmp.first-1 から tmp.second+1
      if(ret.first < tmp.first-1) ret.first = tmp.first-1;
      if(ret.second > tmp.second+1) ret.second = tmp.second+1;
    }
    if(ret.first > ret.second) ret = {INF, -INF};
    if(p[v] != -1) {
      if(ret.first > p[v] || p[v] > ret.second) ret = {INF, -INF};
      else if(abs(p[v] - rn)%2 != d%2) ret = {INF, -INF};
      else ret = {p[v], p[v]};
    }
    // cout << v << " " << ret << endl;
    return x[v] = ret;
  }
}

void dfs2(int v, int par, int d) {
  for(int i: g[v]) {
    if(i == par) continue;
    // p[v]-1 と p[v]+1のどっちを入れるか?
    if(x[i].first <= p[v]-1 && p[v]-1 <= x[i].second) p[i] = p[v]-1;
    else if(x[i].first <= p[v]+1 && p[v]+1 <= x[i].second) p[i] = p[v]+1;
    else assert(false);
    dfs2(i, v, d+1);
  }
}

signed main(void)
{
  int n, k;
  cin >> n;
  REP(i, n-1) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    g[a].PB(b);
    g[b].PB(a);
  }
  memset(p, -1, sizeof(p));
  int root;
  cin >> k;
  REP(i, k) {
    int a, b;
    cin >> a >> b;
    a--;
    p[a] = b;
    root = a; rn = b;
  }

  PII ret = dfs(root, -1, 0);
  if(ret.first > ret.second) cout << "No" << endl;
  else {
    cout << "Yes" << endl;
    dfs2(root, -1, 0);
    REP(i, n) cout << p[i] << endl;
  }

  return 0;
}

Submission Info

Submission Time
Task E - Integers on a Tree
User ferin_tech
Language C++14 (GCC 5.4.1)
Score 800
Code Size 2797 Byte
Status AC
Exec Time 291 ms
Memory 16512 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 247 ms 13440 KB
binary_02.txt AC 73 ms 12800 KB
hand_01.txt AC 2 ms 3328 KB
hand_02.txt AC 2 ms 3328 KB
hand_03.txt AC 2 ms 3328 KB
kary_01.txt AC 69 ms 8832 KB
kary_02.txt AC 216 ms 9216 KB
kary_03.txt AC 67 ms 8576 KB
line_01.txt AC 87 ms 15872 KB
line_02.txt AC 219 ms 13184 KB
line_03.txt AC 222 ms 15872 KB
line_04.txt AC 235 ms 16512 KB
line_05.txt AC 72 ms 14592 KB
line_06.txt AC 84 ms 15872 KB
random0_01.txt AC 106 ms 8576 KB
random1_01.txt AC 230 ms 9216 KB
random1_02.txt AC 234 ms 9216 KB
random1_03.txt AC 236 ms 9216 KB
random1_04.txt AC 261 ms 9216 KB
random1_05.txt AC 233 ms 9216 KB
random1_06.txt AC 231 ms 9344 KB
random1_07.txt AC 233 ms 9344 KB
random1_08.txt AC 291 ms 9216 KB
random2_01.txt AC 76 ms 8576 KB
random2_02.txt AC 76 ms 8576 KB
random2_03.txt AC 77 ms 8576 KB
random2_04.txt AC 80 ms 8576 KB
random2_05.txt AC 109 ms 8576 KB
random2_06.txt AC 135 ms 8576 KB
random3_01.txt AC 77 ms 8576 KB
random3_02.txt AC 106 ms 8576 KB
random4_01.txt AC 230 ms 9216 KB
random4_02.txt AC 232 ms 9344 KB
random4_03.txt AC 232 ms 9344 KB
sample_01.txt AC 2 ms 3328 KB
sample_02.txt AC 2 ms 3328 KB
sample_03.txt AC 2 ms 3328 KB
star_01.txt AC 232 ms 9460 KB
star_02.txt AC 81 ms 8820 KB