CF1200F.Graph Traveler

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Gildong is experimenting with an interesting machine Graph Traveler. In Graph Traveler, there is a directed graph consisting of nn vertices numbered from 11 to nn. The ii-th vertex has mim_i outgoing edges that are labeled as ei[0]e_i[0], ei[1]e_i[1], …\ldots, ei[mi−1]e_i[m_i-1], each representing the destination vertex of the edge. The graph can have multiple edges and self-loops. The ii-th vertex also has an integer kik_i written on itself.

A travel on this graph works as follows.

  1. Gildong chooses a vertex to start from, and an integer to start with. Set the variable cc to this integer.
  2. After arriving at the vertex ii, or when Gildong begins the travel at some vertex ii, add kik_i to cc.
  3. The next vertex is ei[x]e_i[x] where xx is an integer 0≤x≤mi−10 \le x \le m_i-1 satisfying x≡c(modmi)x \equiv c \pmod {m_i}. Go to the next vertex and go back to step 2.

It's obvious that a travel never ends, since the 2nd and the 3rd step will be repeated endlessly.

For example, assume that Gildong starts at vertex 11 with c=5c = 5, and m1=2m_1 = 2, e1[0]=1e_1[0] = 1, e1[1]=2e_1[1] = 2, k1=−3k_1 = -3. Right after he starts at vertex 11, cc becomes 22. Since the only integer xx (0≤x≤10 \le x \le 1) where x≡c(modmi)x \equiv c \pmod {m_i} is 00, Gildong goes to vertex e1[0]=1e_1[0] = 1. After arriving at vertex 11 again, cc becomes −1-1. The only integer xx satisfying the conditions is 11, so he goes to vertex e1[1]=2e_1[1] = 2, and so on.

Since Gildong is quite inquisitive, he's going to ask you qq queries. He wants to know how many distinct vertices will be visited infinitely many times, if he starts the travel from a certain vertex with a certain value of cc. Note that you should not count the vertices that will be visited only finite times.

吉尔东正在试验一台有趣的机器——图旅行器(Graph Traveler)。在图旅行器中,存在一个由 nn 个顶点构成的有向图,顶点编号从 11 到 nn。第 ii 个顶点有 mim_i 条出边,分别标记为 ei[0], ei[1], …, ei[mi−1]e_i[0],\, e_i[1],\, \ldots,\, e_i[m_i-1],每条边指向其目标顶点。该图允许存在重边和自环。第 ii 个顶点上还写有一个整数 kik_i。

在该图上的旅行过程如下:

  1. 吉尔东选择一个起始顶点和一个起始整数。将变量 cc 设为该整数。
  2. 每当到达顶点 ii(包括旅行初始时位于顶点 ii),将 kik_i 加到 cc 上。
  3. 下一个顶点为 ei[x]e_i[x],其中 xx 是满足 0≤x≤mi−10 \le x \le m_i-1 且 x≡c(modmi)x \equiv c \pmod {m_i} 的整数。前往下一个顶点,并返回步骤 2。

显然,该旅行永远不会终止,因为步骤 2 和步骤 3 将无限重复。

例如,假设吉尔东从顶点 11 出发,初始 c=5c = 5,且 m1=2m_1 = 2,e1[0]=1e_1[0] = 1,e1[1]=2e_1[1] = 2,k1=−3k_1 = -3。他刚在顶点 11 开始旅行时,cc 变为 22。由于在 0≤x≤10 \le x \le 1 范围内唯一满足 x≡c(modmi)x \equiv c \pmod {m_i} 的整数是 x=0x = 0,吉尔东前往顶点 e1[0]=1e_1[0] = 1。再次到达顶点 11 后,cc 变为 −1-1。此时唯一满足条件的 xx 是 11,因此他前往顶点 e1[1]=2e_1[1] = 2,依此类推。

由于吉尔东十分好奇,他将向你提出 qq 个询问。他想知道:若从某个特定顶点出发、并以某个特定的 cc 值开始旅行,则最终会被无限次访问的不同顶点的个数是多少?注意:你不应计入那些仅被有限次访问的顶点。

输入格式

The first line of the input contains an integer nn (1≤n≤10001 \le n \le 1000), the number of vertices in the graph.

The second line contains nn integers. The ii-th integer is kik_i (−109≤ki≤109-10^9 \le k_i \le 10^9), the integer written on the ii-th vertex.

Next 2⋅n2 \cdot n lines describe the edges of each vertex. The (2⋅i+1)(2 \cdot i + 1)-st line contains an integer mim_i (1≤mi≤101 \le m_i \le 10), the number of outgoing edges of the ii-th vertex. The (2⋅i+2)(2 \cdot i + 2)-nd line contains mim_i integers ei[0]e_i[0], ei[1]e_i[1], …\ldots, ei[mi−1]e_i[m_i-1], each having an integer value between 11 and nn, inclusive.

Next line contains an integer qq (1≤q≤1051 \le q \le 10^5), the number of queries Gildong wants to ask.

Next qq lines contains two integers xx and yy (1≤x≤n1 \le x \le n, −109≤y≤109-10^9 \le y \le 10^9) each, which mean that the start vertex is xx and the starting value of cc is yy.

输入的第一行包含一个整数 nn(1≤n≤10001 \le n \le 1000),表示图中顶点的数量。

第二行包含 nn 个整数。其中第 ii 个整数为 kik_i(−109≤ki≤109-10^9 \le k_i \le 10^9),表示写在第 ii 个顶点上的整数。

接下来的 2⋅n2 \cdot n 行描述每个顶点的边。第 (2⋅i+1)(2 \cdot i + 1) 行包含一个整数 mim_i(1≤mi≤101 \le m_i \le 10),表示第 ii 个顶点的出边数量;第 (2⋅i+2)(2 \cdot i + 2) 行包含 mim_i 个整数 ei[0], ei[1], …, ei[mi−1]e_i[0],\, e_i[1],\, \ldots,\, e_i[m_i-1],每个值均在 11 到 nn(含)之间。

下一行包含一个整数 qq(1≤q≤1051 \le q \le 10^5),表示 Gildong 想要提出的查询次数。

接下来的 qq 行每行包含两个整数 xx 和 yy(1≤x≤n1 \le x \le n,−109≤y≤109-10^9 \le y \le 10^9),表示起始顶点为 xx,且变量 cc 的初始值为 yy。

输出格式

For each query, print the number of distinct vertices that will be visited infinitely many times, if Gildong starts at vertex xx with starting integer yy.

对于每个查询,请输出:如果吉隆从顶点 xx 出发,且初始整数为 yy,则会被无限次访问的不同顶点的数量。

输入输出样例

  • 输入#1

    4
    0 0 0 0
    2
    2 3
    1
    2
    3
    2 4 1
    4
    3 1 2 1
    6
    1 0
    2 0
    3 -1
    4 -2
    1 1
    1 5

    输出#1

    1
    1
    2
    1
    3
    2
  • 输入#2

    4
    4 -5 -3 -1
    2
    2 3
    1
    2
    3
    2 4 1
    4
    3 1 2 1
    6
    1 0
    2 0
    3 -1
    4 -2
    1 1
    1 5

    输出#2

    1
    1
    1
    3
    1
    1

说明/提示

The first example can be shown like the following image:

Three integers are marked on ii-th vertex: ii, kik_i, and mim_i respectively. The outgoing edges are labeled with an integer representing the edge number of ii-th vertex.

The travel for each query works as follows. It is described as a sequence of phrases, each in the format "vertex (cc after kik_i added)".

  • 1(0)→2(0)→2(0)→…1(0) \to 2(0) \to 2(0) \to \ldots
  • 2(0)→2(0)→…2(0) \to 2(0) \to \ldots
  • 3(−1)→1(−1)→3(−1)→…3(-1) \to 1(-1) \to 3(-1) \to \ldots
  • 4(−2)→2(−2)→2(−2)→…4(-2) \to 2(-2) \to 2(-2) \to \ldots
  • 1(1)→3(1)→4(1)→1(1)→…1(1) \to 3(1) \to 4(1) \to 1(1) \to \ldots
  • 1(5)→3(5)→1(5)→…1(5) \to 3(5) \to 1(5) \to \ldots

The second example is same as the first example, except that the vertices have non-zero values. Therefore the answers to the queries also differ from the first example.

The queries for the second example works as follows:

  • 1(4)→2(−1)→2(−6)→…1(4) \to 2(-1) \to 2(-6) \to \ldots
  • 2(−5)→2(−10)→…2(-5) \to 2(-10) \to \ldots
  • 3(−4)→1(0)→2(−5)→2(−10)→…3(-4) \to 1(0) \to 2(-5) \to 2(-10) \to \ldots
  • 4(−3)→1(1)→3(−2)→4(−3)→…4(-3) \to 1(1) \to 3(-2) \to 4(-3) \to \ldots
  • 1(5)→3(2)→1(6)→2(1)→2(−4)→…1(5) \to 3(2) \to 1(6) \to 2(1) \to 2(-4) \to \ldots
  • 1(9)→3(6)→2(1)→2(−4)→…1(9) \to 3(6) \to 2(1) \to 2(-4) \to \ldots

第一个示例可如下图所示:

第 ii 个顶点上标记有三个整数:ii、kik_i 和 mim_i。从该顶点出发的边用整数标注,表示该顶点的第几条出边。

每次查询的遍历过程如下所述,其形式为一系列短语,每个短语格式为“顶点(在加上 kik_i 后的 cc 值)”。

  • 1(0)→2(0)→2(0)→…1(0) \to 2(0) \to 2(0) \to \ldots
  • 2(0)→2(0)→…2(0) \to 2(0) \to \ldots
  • 3(−1)→1(−1)→3(−1)→…3(-1) \to 1(-1) \to 3(-1) \to \ldots
  • 4(−2)→2(−2)→2(−2)→…4(-2) \to 2(-2) \to 2(-2) \to \ldots
  • 1(1)→3(1)→4(1)→1(1)→…1(1) \to 3(1) \to 4(1) \to 1(1) \to \ldots
  • 1(5)→3(5)→1(5)→…1(5) \to 3(5) \to 1(5) \to \ldots

第二个示例与第一个示例相同,仅顶点上的值非零。因此,各查询的答案也与第一个示例不同。

第二个示例的查询过程如下:

  • 1(4)→2(−1)→2(−6)→…1(4) \to 2(-1) \to 2(-6) \to \ldots
  • 2(−5)→2(−10)→…2(-5) \to 2(-10) \to \ldots
  • 3(−4)→1(0)→2(−5)→2(−10)→…3(-4) \to 1(0) \to 2(-5) \to 2(-10) \to \ldots
  • 4(−3)→1(1)→3(−2)→4(−3)→…4(-3) \to 1(1) \to 3(-2) \to 4(-3) \to \ldots
  • 1(5)→3(2)→1(6)→2(1)→2(−4)→…1(5) \to 3(2) \to 1(6) \to 2(1) \to 2(-4) \to \ldots
  • 1(9)→3(6)→2(1)→2(−4)→…1(9) \to 3(6) \to 2(1) \to 2(-4) \to \ldots

输入解题思路,AI测评打分。不知道怎么写?

首页