시간 제한 메모리 제한 제출 정답 맞은 사람 정답 비율
4 초 537 MB 13 12 12 92.308%

문제

There are N cities and M roads in Ibaraki Prefecture, Japan. Cities are numbered from 0 through N - 1 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 Q - 1. The trip i (0 ≤ iQ - 1) 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 (0 ≤ iQ - 1), there are two thresholds Li and Ri (0 ≤ LiRiN - 1) that indicate which cities must be avoided. More specifically, you must avoid the cities 0, 1, ..., Li - 1 when you are in human form, and must avoid the cities Ri + 1, Ri + 2, ..., N - 1 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.

구현

You should implement the following function:

int[] check_validity(int N, int[] X, int[] Y, int[] S, int[] E, int[] L, int[] R)
  • N: the number of cities.
  • X and Y: arrays of length M. For each j (0 ≤ jM - 1), the city X[j] is directly connected to the city X[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 (0 ≤ iQ - 1) must be 1 if the trip i is possible while satisfying the aforementioned conditions, or 0 otherwise.

예시

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], and R = [2, 2, 4].

The grader calls 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 the city 4 (You are in human form)
  • Move to the city 3 (You are in human form)
  • Move to the 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].

제한

  • 2 ≤ N ≤ 200,000
  • N - 1 ≤ M ≤ 400 000
  • 1 ≤ Q ≤ 200,000
  • For each 0 ≤ jM - 1
    • 0 ≤ XjN - 1
    • 0 ≤ YjN - 1
    • 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 0 ≤ j < kM - 1, (Xj, Yj) ≠ (Xk, Yk) and (Yj, Xj) ≠ (Xk, Yk).
  • For each 0 ≤ iQ - 1
    • 0 ≤ LiSiN - 1
    • 0 ≤ EiRiN - 1
    • SiEi
    • LiRi

서브태스크

번호 배점 조건
1 7

N ≤ 100, M ≤ 200, Q ≤ 100

2 8

N ≤ 3,000, M ≤ 6,000, Q ≤ 3,000

3 34

M = N - 1 and each city is incident to at most 2 roads (the cities are connected in a line)

4 51

No additional constraints

샘플 그레이더

The sample grader reads the input in the following format:

  • line 1: N M Q
  • line 2 + j (0 ≤ jM - 1): Xj Yj
  • line 2 + M + i (0 ≤ iQ - 1): Si Ei Li Ri

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

  • line 1 + i (0 ≤ iQ - 1): Ai

제출할 수 있는 언어

C++17, C++14, C++20, C++14 (Clang), C++17 (Clang), C++20 (Clang)

채점 및 기타 정보

  • 예제는 채점하지 않는다.