Submission #329728

#TimeUsernameProblemLanguageResultExecution timeMemory
329728chenwzStations (IOI20_stations)C++14
100 / 100
1006 ms1140 KiB
#include "stations.h"
#include <vector>
using namespace std;
typedef vector<int> IVec;

void rec(int u, int fa, bool pre_order, int &clock, const vector<IVec>& G, IVec& L) {
  if (pre_order) L[u] = clock++;
  for (int i : G[u])
    if (i != fa) rec(i, u, !pre_order, clock, G, L);
  if (!pre_order) L[u] = clock++;
}

IVec label(int n, int k, IVec u, IVec v) {
  IVec L(n);
  vector<IVec> G(n, IVec());
  for (int i = 0; i < u.size(); i++)
    G[u[i]].push_back(v[i]), G[v[i]].push_back(u[i]);
  int clock = 0;
  rec(0, -1, true, clock, G, L);
  return L;
}

int find_next_station(int s, int t, IVec c) {
  if (c[0] > s) {
    // Case 1: node s is in pre_order level.
    // All the neighbors are higher than s.

    if (t < s) {
      // The target is smaller than the source.
      // The target is definitely not in this subtree, go to parent.
      return c.back();
    }

    if (t > c.back()) {
      // The target is higher than the largest neighbor.
      // The target cannot be in this subtree, go to parent.
      return c.back();
    }

    // The target is in this subtree.
    // Pick the smallest child that's at least the target.
    int next = 0;
    while (c[next] < t) next++;
    return c[next];
  }


  // Case 2: node s is in the post_order level.
  if (t < c[0]) {
    // The target is smaller than the pre_order root c[0],
    // thus not in this subtree, go to the root.
    return c[0];
  }

  if (t > s) {
    // The target is higher than this post_order value.
    // Thus it's not in this subtree, go to the root.
    return c[0];
  }

  int next = c.size() - 1;
  while (c[next] > t) next--;
  return c[next];
}

Compilation message (stderr)

stations.cpp: In function 'IVec label(int, int, IVec, IVec)':
stations.cpp:16:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   16 |   for (int i = 0; i < u.size(); i++)
      |                   ~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...