問題一覧 > 通常問題

No.2288 Somen Sliders

レベル : / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 8
作問者 : みここ / テスター : cureskol 👑 potato167
お気に入りにしたユーザー ProblemId : 9172 / 自分の提出
問題文最終更新日: 2023-04-28 21:20:13
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Show the original
yukicoder contest 386 (順位表) の他の問題:

Problem Statement

You are given internally vertex-disjoint $0(N+1)$ path sets $\mathcal{P} = \{P_1, \cdots, P_X\}, \mathcal{Q} = \{Q_1, \cdots, Q_Y\}$ on a directed graph with $N + 2$ vertices $0, 1, \cdots, N, N + 1$. For $1 \le i \le X$, the number of edges in $P_i$ is $K_i$, and its $j$-th vertex (counting starts from the $0$-th vertex) is $u_{i, j}$. Also, for $1 \le i \le Y$, the number of edges in $Q_i$ is $L_i$, and its $j$-th vertex (counting starts from the $0$-th vertex) is $v_{i, j}$. Here, paths of $\mathcal{P}$ and paths of $\mathcal{Q}$ are assumed to have no common edges. However, parallel edges may exist between paths of $\mathcal{P}$ and paths of $\mathcal{Q}$ (that is, for some $2$ vertices $u, v$, an edge from $u$ to $v$ may be contained in both $\mathcal{P}$ and $\mathcal{Q}$, but these are treated as distinct edges). Furthermore, all paths of $\mathcal{P}, \mathcal{Q}$ are assumed to have length at least $2$.

From these $\mathcal{P}, \mathcal{Q}$, you want to construct a path set $\mathcal{R}$ satisfying the following.

  • $\mathcal{R}$ is an internally vertex-disjoint $0(N+1)$ path set.
  • For each $R \in \mathcal{R}$, there exists a vertex on $R$ denoted by $u$ such that the edges of $R$ before $u$ are contained in $E(\mathcal{P})$ and the edges after $u$ are contained in $E(\mathcal{Q})$.
  • For each $P \in \mathcal{P}$, the first edge of $P$ is contained in $E(\mathcal{R})$.
  • For each $Q \in \mathcal{Q}$, the last edge of $Q$ is contained in $E(\mathcal{R})$.

Here, for a path set $\mathcal{X}$, $E(\mathcal{X})$ denotes the set of all edges contained in some path of $\mathcal{X}$.

Determine whether such an $\mathcal{R}$ exists, and if it exists, find the maximum possible value of $|E(\mathcal{P}) \cap E(\mathcal{R})|$.

What is an internally vertex-disjoint $0(N+1)$ path set? (Click to show the explanation)

When a path set $\mathcal{X}$ satisfies the following, $\mathcal{X}$ is called an internally vertex-disjoint $0(N+1)$ path set.

  • Every $P \in \mathcal{X}$ has start vertex $0$ and end vertex $N + 1$.
  • Every $P \in \mathcal{X}$ contains no cycle.
  • All distinct $P, Q \in \mathcal{X}$ have no common vertices other than $0, N + 1$.

Input

$N \ X \ Y$
$K_1$
$u_{1,0} \ u_{1,1} \ \cdots \ u_{1, K_1}$
$\vdots$
$K_X$
$u_{X,0} \ u_{X,1} \ \cdots \ u_{X, K_X}$
$L_1$
$v_{1,0} \ v_{1,1} \ \cdots \ v_{1, L_1}$
$\vdots$
$L_Y$
$v_{Y,0} \ v_{Y,1} \ \cdots \ v_{Y, L_Y}$
  • All input values are integers
  • $1 \le N \le 10^5$
  • $1 \le X, Y \le N$
  • $2 \le K_i \le N + 1$
  • $u_{i,0} = 0$ and $u_{i,K_i} = N + 1$
  • For $1 \le j < K_i$, $1 \le u_{i,j} \le N$
  • For $1 \le j < K_i, 1 \le j' < K_{i'}$, if $i \neq i'$ or $j \neq j'$, then $u_{i,j} \neq u_{i', j'}$
  • $2 \le L_i \le N + 1$
  • $v_{i,0} = 0$ and $v_{i,L_i} = N + 1$
  • For $1 \le j < L_i$, $1 \le v_{i,j} \le N$
  • For $1 \le j < L_i, 1 \le j' < L_{i'}$, if $i \neq i'$ or $j \neq j'$, then $v_{i,j} \neq v_{i', j'}$

Output

If a path set $\mathcal{R}$ satisfying the conditions exists, output the maximum value of $|E(\mathcal{P}) \cap E(\mathcal{R})|$. Otherwise, output $-1$.

Sample

Sample 1
Input
4 2 2
3
0 3 1 5
3
0 2 4 5
3
0 2 1 5
3
0 3 4 5
Output
4

The path sets satisfying the conditions are $\mathcal{R}_1 = \{0→_\mathcal{P}3→_\mathcal{P}1→_\mathcal{Q}5, 0→_\mathcal{P}2→_\mathcal{P}4→_\mathcal{Q}5\}, \mathcal{R}_2 = \{0→_\mathcal{P}3→_\mathcal{Q}4→_\mathcal{Q}5, 0→_\mathcal{P}2→_\mathcal{Q}1→_\mathcal{Q}5\}$, and $|\mathcal{P} \cap \mathcal{R}_1| = 4, |\mathcal{P} \cap \mathcal{R}_2| = 2$, so the answer is $4$.

Sample 2
Input
2 1 1
2
0 1 3
2
0 2 3
Output
2

The path set satisfying the conditions is $\{0→_\mathcal{P}1→_\mathcal{P}3, 0→_\mathcal{Q}2→_\mathcal{Q}3\}$. Note that $u$ in the second condition may be $0$ or $N + 1$.

Sample 3
Input
4 1 1
5
0 1 2 3 4 5
5
0 1 2 3 4 5
Output
4

Path sets satisfying the conditions include $\{0→_\mathcal{P}1→_\mathcal{P}2→_\mathcal{P}3→_\mathcal{P}4→_\mathcal{Q}5\}$ and $\{0→_\mathcal{P}1→_\mathcal{P}2→_\mathcal{Q}3→_\mathcal{Q}4→_\mathcal{Q}5\}$. Note that $\{0→_\mathcal{P}1→_\mathcal{P}2→_\mathcal{P}3→_\mathcal{P}4→_\mathcal{P}5\}$ does not satisfy the conditions.

Sample 4
Input
19 3 3
4
0 1 2 3 20
8
0 4 5 6 7 8 9 10 20
4
0 11 12 13 20
6
0 14 3 15 8 16 20
5
0 17 2 9 18 20
4
0 19 12 7 20
Output
10

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。