#15571. [ICPC 2022 Xi'an R] Contests

[ICPC 2022 Xi'an R] Contests

[ICPC 2022 Xi'an R] Contests

题目描述

There are $n$ contestants and they take part in $m$ contests. You are given the ranklist of each contest. The ranklist of the $k$-th contest is a sequence $a_k$, indicating that the $a_{k, i}$-th contestant's rank is $i$.

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 1≤i≤l1\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 $n$ ($2\leq n\leq 10 ^ 5$) and $m$ ($1\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 (1≤q≤1051\leq q\leq 10 ^ 5).

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

输出格式

For each query, output a number $l$ representing the answer. If there is no legal $l$, output $-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. 标签: P9361|倍增|2022|O2优化|ICPC|西安

来源

P9361|[ICPC 2022 Xi'an R] Contests