#P9361. [ICPC2022 Xi'an R] Contests

[ICPC2022 Xi'an R] Contests

题目描述

There are nn contestants and they take part in mm contests. You are given the ranklist of each contest. The ranklist of the kk-th contest is a sequence aka_k, indicating that the ak,ia_{k, i}-th contestant's rank is ii.

SolarPea and PolarSea are two of the nn contestants. SolarPea wants to prove that he is stronger than PolarSea.

Define xx is ll-stronger than yy, if and only if there exists a sequence bb of length l+1l + 1, such that b1=xb_1 = x, bl+1=yb_{l + 1} = y, and for all 1il1\leq i\leq l, bib_i has a smaller rank than bi+1b_{i + 1} in at least one contest.

There are qq queries. In the ii-th query, SolarPea is contestant xx and PolarSea is contestant yy. Please find the minimum positive number ll such that SolarPea is ll-stronger than PolarSea.

输入格式

The first line contains two integers nn (2n1052\leq n\leq 10 ^ 5) and mm (1m51\leq m\leq 5).

The ii-th of the next mm lines contains nn intergers ai,1,ai,2,,ai,na_{i, 1}, a_{i, 2}, \ldots, a_{i, n}. It is guaranteed that aia_i is a permutaion of 1,2,,n1,2,\ldots,n.

The next line contains an integer qq (1q1051\leq q\leq 10 ^ 5).

Each of the next qq lines contains two integers xx and yy (1x,yn,xy1 \le x,y \le n, x \neq y), representing a query.

输出格式

For each query, output a number ll representing the answer. If there is no legal ll, output 1-1.

题目大意

题目描述

nn 个选手参加了 mm 场比赛。给出每场比赛的排行榜。第 kk 场比赛的排行榜是一个 nn 阶排列 aka_k,表示选手 ak,ia_{k, i} 的排名为 ii

SolarPea 和 PolarSea 也是选手。SolarPea 想要证明他比 PolarSea 更强。

定义选手 xxll - 强于」选手 yy,当且仅当存在长度为 l+1l + 1 的序列,满足 b1=xb_1 = xbl+1=yb_{l + 1} = y,且对于所有 1il1\leq i\leq l,均有 bib_i 在至少一场比赛中排名小于 bi+1b_{i + 1}

给出 qq 组询问。在第 ii 组询问中,SolarPea 是选手 xx,PolarSea 是选手 yy。求出最小的正整数 ll,使得 xxll - 强于」yy

2n1052\leq n\leq 10 ^ 51q1051\leq q\leq 10 ^ 51m51\leq m\leq 51x,yn1\leq x, y\leq nxyx\neq y

输入格式

第一行两个整数 n,mn, m

接下来 mm 行,每行 nn 个整数,表示第 ii 场比赛的排行榜。保证 aia_i1,2,,n1, 2, \ldots, n 的排列。

接下来一行一个整数 qq

接下来 qq 行,每行两个整数 x,yx, y 表示一组询问。

输出格式

对于每组询问,输出一行一个整数表示答案。若不存在这样的 ll,输出 1-1

6 2
1 3 2 5 4 6
2 1 4 3 6 5
4
1 4
5 3
6 1
5 2

1
2
5
3

提示

Source: The 2022 ICPC Asia Xi'an Regional Contest Problem D.

Author: csy2005.