IOI '18 P3 - Werewolf

View as PDF

Submit solution


Points: 30 (partial)
Time limit: 1.8s
Memory limit: 512M

Problem type
Allowed languages
C++

There are N cities and M roads in Ibaraki Prefecture, Japan. Cities are numbered from 0 through N1 in the increasing order of their population. Each road connects a pair of distinct cities, and can be traveled in both directions. You can travel from any city to any other city by using one or more of these roads.

You planned Q trips, numbered from 0 through Q1. The trip i (0iQ1) is to travel from the city Si to the city Ei.

You are a werewolf. You have two forms: human form and wolf form. At the beginning of each trip you are in human form. At the end of each trip, you must be in wolf form. During the trip you have to transform (change from human form to wolf form) exactly once. You can transform only when you are in some city (possibly Si or Ei).

Living as a werewolf is not easy. You must avoid low-populated cities when you are in human form, and avoid highly-populated cities when you are in wolf form. For each trip i (0iQ1), there are two thresholds Li and Ri (0LiRiN1) that indicate which cities must be avoided. More specifically, you must avoid the cities 0,1,,Li1 when you are in human form, and must avoid the cities Ri+1,Ri+2,,N1 when you are in wolf form. This means in the trip i, you can only transform in one of the cities Li,Li+1,,Ri.

Your task is to determine, for each trip, whether it is possible to travel from the city Si to the city Ei in a way that satisfies the aforementioned constraints. The route you take can have an arbitrary length.

Implementation details

You should implement the following function:

Copy
std::vector<int> check_validity(int N, std::vector<int> X, std::vector<int> Y, std::vector<int> S, std::vector<int> E, std::vector<int> L, std::vector<int> R)
  • N: the number of cities.
  • X and Y: arrays of length M. For each j (0jM1), the city X[j] is directly connected to the city Y[j] by a road.
  • S, E, L, and R: arrays of length Q, representing the trips.

Note that the values of M and Q are the lengths of the arrays, and can be obtained as indicated in the implementation notice.

The function check_validity is called exactly once for each test case. This function should return an array A of integers of length Q. The value of Ai (0iQ1) must be 1 if the trip i is possible while satisfying the aforementioned conditions, or 0 otherwise.

Example

Let N=6, M=6, Q=3, X=[5,1,1,3,3,5], Y=[1,2,3,4,0,2], S=[4,4,5], E=[2,2,4], L=[1,2,3], R=[2,2,4].

Copy
check_validity(6, {5, 1, 1, 3, 3, 5}, {1, 2, 3, 4, 0, 2}, {4, 4, 5}, {2, 2, 4}, {1, 2, 3}, {2, 2, 4})

For the trip 0, you can travel from the city 4 to the city 2 as follows:

  • Start at city 4 (You are in human form)
  • Move to the city 3 (You are in human form)
  • Move to city 1 (You are in human form)
  • Transform yourself into wolf form (You are in wolf form)
  • Move to the city 2 (You are in wolf form)

For the trips 1 and 2, you cannot travel between the given cities.

Hence, your program should return [1, 0, 0].

Constraints

  • 2N200000
  • N1M400000
  • 1Q200000
  • For each 0jM1
    • 0XjN1
    • 0YjN1
    • XjYj
  • You can travel from any city to any other city by using roads.
  • Each pair of cities are directly connected by at most one road. In other words, for all 0j<kM1, (Xj,Yj)(Xk,Yk) and (Yj,Xj)(Xk,Yk).
  • For each 0iQ1
    • 0LiSiN1
    • 0EiRiN1
    • SiEi
    • LiRi

Subtasks

Subtask Points Constraints
1 7 points N100, M200, Q100
2 8 points N3000, M6000, Q3000
3 34 points M=N1 and each city is incident to at most 2 roads (the cities are connected in a line)
4 51 points No additional constraints

Sample Grader

The sample grader reads the input in the following format:

  • line 1: N M Q
  • line 2+j (0jM1): Xj Yj
  • line 2+M+i (0iQ1): Si Ei Li Ri

The sample grader prints the return value of check_validity in the following format:

  • line 1+i (0iQ1): Ai

Comments

There are no comments at the moment.